From 5a8c3a4dcb3f1ba14ee9facec07e9cb0652fe3c1 Mon Sep 17 00:00:00 2001 From: Joseph Aquino Date: Sun, 5 Apr 2026 00:47:31 -0400 Subject: [PATCH] started collison implementation --- include/Game.h | 6 +++-- src/Game.cpp | 63 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/include/Game.h b/include/Game.h index 84ed701..cc69ef7 100644 --- a/include/Game.h +++ b/include/Game.h @@ -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; diff --git a/src/Game.cpp b/src/Game.cpp index bcdaa6e..de06238 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -5,6 +5,7 @@ #include #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(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(sleepNeeded)); @@ -194,7 +195,7 @@ void Game::getInput() void Game::render() { window.clear(); - window.draw(gameBoundary); + window.draw(gameBoundary) ; window.draw(midline); @@ -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(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() {}