started collison implementation

This commit is contained in:
Joseph Aquino 2026-04-05 00:47:31 -04:00
parent 54d4f26a4f
commit 5a8c3a4dcb
2 changed files with 55 additions and 14 deletions

View File

@ -38,8 +38,8 @@ struct Ball
sf::Vector2f pos{}; sf::Vector2f pos{};
sf::Vector2f prevPos{}; sf::Vector2f prevPos{};
sf::Vector2f velocity{}; sf::Vector2f velocity{};
float speed{}; float speed{5};
sf::Angle angle{}; sf::Angle angle{sf::degrees(290.f)};
}; };
class Game class Game
@ -67,6 +67,8 @@ public:
void resetGame(); void resetGame();
void resetBall();
private: private:
sf::RenderWindow window {sf::VideoMode({1280u, 720u}), "project-pong"}; sf::RenderWindow window {sf::VideoMode({1280u, 720u}), "project-pong"};
std::chrono::steady_clock clock; std::chrono::steady_clock clock;

View File

@ -5,6 +5,7 @@
#include <chrono> #include <chrono>
#include "util.h" #include "util.h"
#include "Random.h"
void Game::init() void Game::init()
@ -54,8 +55,9 @@ void Game::init()
ball.body.setPosition(ball.pos); ball.body.setPosition(ball.pos);
ball.body.setFillColor(sf::Color::White); ball.body.setFillColor(sf::Color::White);
ball.body.setRadius(5.f); ball.body.setRadius(5.f);
ball.velocity = velocityInDirection(ball.speed, ball.angle);
gameBoundary.setOutlineThickness(5); gameBoundary.setOutlineThickness(-5);
gameBoundary.setOutlineColor(sf::Color::White); gameBoundary.setOutlineColor(sf::Color::White);
gameBoundary.setFillColor(sf::Color::Transparent); gameBoundary.setFillColor(sf::Color::Transparent);
gameBoundary.setPosition({0,100}); gameBoundary.setPosition({0,100});
@ -88,7 +90,7 @@ void Game::run()
TimePoint currentFrameStartPoint = std::chrono::steady_clock::now(); TimePoint currentFrameStartPoint = std::chrono::steady_clock::now();
TimeDuration duration = currentFrameStartPoint - lastFrameStartPoint; TimeDuration duration = currentFrameStartPoint - lastFrameStartPoint;
const double frameTime = duration.count(); const auto frameTime = duration.count();
playTime += duration; playTime += duration;
totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(playTime).count(); totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(playTime).count();
@ -96,7 +98,7 @@ void Game::run()
lastFrameStartPoint = currentFrameStartPoint; lastFrameStartPoint = currentFrameStartPoint;
accumulator += frameTime; accumulator += frameTime;
if (accumulator > 0.25) accumulator = 0.25; if (accumulator > 0.25f) accumulator = 0.25f;
getInput(); getInput();
@ -104,12 +106,12 @@ void Game::run()
{ {
while (accumulator >= physicsDeltaTime) while (accumulator >= physicsDeltaTime)
{ {
updateBall();
updateCpu(); updateCpu();
updatePlayer(); updatePlayer();
updateBall();
collision(); collision();
updateTexts(); updateTexts();
@ -126,7 +128,6 @@ void Game::run()
TimeDuration currentFrameDuration = currentFrameEndPoint - currentFrameStartPoint; TimeDuration currentFrameDuration = currentFrameEndPoint - currentFrameStartPoint;
if (currentFrameDuration.count() < targetFrameTime) if (currentFrameDuration.count() < targetFrameTime)
{ {
TimeDuration sleepNeeded = TimeDuration(targetFrameTime) - currentFrameDuration; TimeDuration sleepNeeded = TimeDuration(targetFrameTime) - currentFrameDuration;
std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(sleepNeeded)); std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(sleepNeeded));
@ -224,29 +225,32 @@ void Game::render()
void Game::updateCpu() void Game::updateCpu()
{ {
const float cpuMidpoint = cpu.pos.y + cpu.body.getSize().y;
if (totalSeconds > 0 and totalSeconds % 10 == 0) if (totalSeconds > 0 and totalSeconds % 10 == 0)
{ {
cpu.speed += .5f; cpu.speed += .3f;
} }
if (ball.pos.y > cpu.pos.y) if (ball.pos.y > cpuMidpoint)
{ {
cpu.velocity = cpu.speed; cpu.velocity = cpu.speed;
} }
if (ball.pos.y < cpu.pos.y) if (ball.pos.y < cpuMidpoint)
{ {
cpu.velocity = cpu.speed * -1.f; cpu.velocity = cpu.speed * -1.f;
} }
if (ball.pos.y == cpu.pos.y) if (ball.pos.y == cpuMidpoint)
{ {
cpu.velocity = 0; cpu.velocity = 0;
} }
cpu.prevPos = cpu.pos; cpu.prevPos = cpu.pos;
cpu.pos.y += cpu.velocity; cpu.pos.y += cpu.velocity;
} }
void Game::updatePlayer() void Game::updatePlayer()
{ {
const int direction = player.down - player.up; const int direction = player.down - player.up;
@ -259,7 +263,41 @@ void Game::updateBall()
ball.prevPos = ball.pos; ball.prevPos = ball.pos;
ball.pos += ball.velocity; ball.pos += ball.velocity;
} }
void Game::collision() {}
void Game::collision()
{
const sf::FloatRect ballBounds = {{ball.pos.x, ball.pos.y}, {5.f, 5.f}};
if (ballBounds.position.x < 0)
{
cpu.score++;
resetBall();
}
if (ballBounds.position.x + ballBounds.size.x > static_cast<float>(window.getSize().x))
{
player.score++;
resetBall();
}
// +/-5 to account for outline size
if ( (ballBounds.position.y < gameBoundary.getPosition().y + 5) or (ballBounds.position.y + ballBounds.size.y > gameBoundary.getPosition().y + gameBoundary.getSize().y - 5) )
{
if (ball.velocity.y < 0)
{
ball.pos.y = gameBoundary.getPosition().y + 5;
}
else if (ball.velocity.y > 0)
{
// use 6 instead of 5 otherwise it detects a collision again
ball.pos.y = gameBoundary.getPosition().y + gameBoundary.getSize().y - 6;
}
ball.speed += .5f;
ball.velocity.y *= -1.f;
}
}
void Game::updateTexts() void Game::updateTexts()
{ {
@ -274,4 +312,5 @@ void Game::updateTexts()
void Game::soundSystem() {} void Game::soundSystem() {}
void Game::resetGame() {} void Game::resetGame() {}
void Game::resetBall() {}