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

View File

@ -5,6 +5,7 @@
#include <chrono>
#include "util.h"
#include "Random.h"
void Game::init()
@ -54,8 +55,9 @@ void Game::init()
ball.body.setPosition(ball.pos);
ball.body.setFillColor(sf::Color::White);
ball.body.setRadius(5.f);
ball.velocity = velocityInDirection(ball.speed, ball.angle);
gameBoundary.setOutlineThickness(5);
gameBoundary.setOutlineThickness(-5);
gameBoundary.setOutlineColor(sf::Color::White);
gameBoundary.setFillColor(sf::Color::Transparent);
gameBoundary.setPosition({0,100});
@ -88,7 +90,7 @@ void Game::run()
TimePoint currentFrameStartPoint = std::chrono::steady_clock::now();
TimeDuration duration = currentFrameStartPoint - lastFrameStartPoint;
const double frameTime = duration.count();
const auto frameTime = duration.count();
playTime += duration;
totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(playTime).count();
@ -96,7 +98,7 @@ void Game::run()
lastFrameStartPoint = currentFrameStartPoint;
accumulator += frameTime;
if (accumulator > 0.25) accumulator = 0.25;
if (accumulator > 0.25f) accumulator = 0.25f;
getInput();
@ -104,12 +106,12 @@ void Game::run()
{
while (accumulator >= physicsDeltaTime)
{
updateBall();
updateCpu();
updatePlayer();
updateBall();
collision();
updateTexts();
@ -126,7 +128,6 @@ void Game::run()
TimeDuration currentFrameDuration = currentFrameEndPoint - currentFrameStartPoint;
if (currentFrameDuration.count() < targetFrameTime)
{
TimeDuration sleepNeeded = TimeDuration(targetFrameTime) - currentFrameDuration;
std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(sleepNeeded));
@ -224,29 +225,32 @@ void Game::render()
void Game::updateCpu()
{
const float cpuMidpoint = cpu.pos.y + cpu.body.getSize().y;
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;
}
if (ball.pos.y < cpu.pos.y)
if (ball.pos.y < cpuMidpoint)
{
cpu.velocity = cpu.speed * -1.f;
}
if (ball.pos.y == cpu.pos.y)
if (ball.pos.y == cpuMidpoint)
{
cpu.velocity = 0;
}
cpu.prevPos = cpu.pos;
cpu.pos.y += cpu.velocity;
}
void Game::updatePlayer()
{
const int direction = player.down - player.up;
@ -259,7 +263,41 @@ void Game::updateBall()
ball.prevPos = ball.pos;
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()
{
@ -274,4 +312,5 @@ void Game::updateTexts()
void Game::soundSystem() {}
void Game::resetGame() {}
void Game::resetBall() {}