collision detection help for Pong game in C++

Started by
5 comments, last by Petrov_VA 10 years, 6 months ago

Hi all;

I'm looking to implement some basic collision detection for my pong game. I followed a few basic tutorials on how to create all the classes to write a pong game - I now have a somewhat playable version of the game, which I've flipped the view so instead of the traditional horizontal position, you play in a vertical position so the player paddle is at the bottom of the window and the AI paddle is at the top. I've attached all the files necessary for everything to work. I'm thinking about adding some sort of check in the 'PlayerPaddle' to detect whether the paddle has touched the window boundary on either side and if this has been detected, don't allow the paddle to continue to move out of the window into oblivion.

I'm sure there are experts who know exactly what the problem is and how to remedy this problem. I'm pulling my hair out as this is my last major problem to sort out. I'd like to thank anyone in advance for providing on help on this conundrum. For some reason I can't attach cpp and header files so I've included the following link to all the files:

https://www.dropbox.com/sh/cr04j36vleat56y/sCTKdg0XVH

Any help or advice will be greatly appreciated.

Chilun

Advertisement

Pseudocode:

offscreen check:

if(paddle.x < 0 || paddle.x + paddle.width > window.width) offscreen = true;

This is probably more useful though:

limit movement on left/right side:

if(paddle.x < 0) paddle.x = 0;

if(paddle.x + paddle.width > window.width) paddle.x = window.width - paddle.width;

If there is a border area around the window you need to take this into account (use border.width instead of 0 and use (window.width - border.width) instead of window.width.

But if that is the only part you are struggling with how did you do the paddle/ball and ball/window collision?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Hi Paradigm Shifter;

Its quite a strange one - most of the logic is either in the Ball or PlayerPaddle cpp files. The tutorial I followed (http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-Part-7.aspx) goes into depth regarding the speed of the ball and the origin of where the ball came from would determine the angle of how the ball will deflect off either the AIPaddle or the left/right wall. Sadly the person who wrote the tutorial didn't finish off what he started so I'm scratching my head as to how I could finish this off. I will try to bash out some code that would do the boundary checking. Thanks for your help

Hi Chilun

I am the author of the tutorial series. What specifically are you have trouble with? The code as it stands ( by the end of the series ) should prevent the paddles from going off into space. For example, this is the code performing just that action for the ai paddle:


sf::Vector2f pos = this->GetPosition();

if(pos.x <= GetSprite().GetSize().x/2
|| pos.x >= (Game::SCREEN_WIDTH - GetSprite().GetSize().x/2))
{
_velocity = -_velocity; // Bounce by current velocity in opposite direction
}

This is doing basically what Paradigm Shifter described. Or am I misunderstanding your question?

Hi Serapth;

Thank you for your response. I think once the game initially loads, its loads up fine, but after a few seconds - either the AI or the player paddle seems to fly off into eternity.

Would you know why this is the case? I've change the speed of the player paddle to 500.0f so that the movement of the paddle from left to right (vice versa) is smoother, upon releasing the key, the velocity resets back to 0 - otherwise the paddle becomes uncontrollable. Essentially, if I continue to hold the left or right key continuously, the paddle with just disappear out of the window and there is currently no checking to see if this is the case, reset the paddle back at the bottom of the screen.

Thanks very much.

Chilun

I've figured this out...

Split the conditional statement into 2 in order to handle with the paddle - wall contact (prevent the paddle flying off) and also modified the maxVelocity value to 500.0f.

This now works for me biggrin.png

Thanks everyone for your help.

Chilun

...

sf::Vector2f pos = this->GetPosition();
// MAKE SURE PADDLE DOESN'T DISAPPEAR OFF INTO SPACE WHEN IT REACHES THE WINDOW BOUNDARY (LEFT AND RIGHT SIDE)
if(pos.x < GetSprite().GetSize().x/2) {
this->SetPosition(GetSprite().GetSize().x/2, this->GetPosition().y);
}
if(pos.x > (Game::SCREEN_WIDTH - GetSprite().GetSize().x/2)){
this->SetPosition(Game::SCREEN_WIDTH - GetSprite().GetSize().x/2, this->GetPosition().y);
}

Good sample: http://nehe.gamedev.net/tutorial/collision_detection/17005/

C++ sample: http://nehe.gamedev.net/data/lessons/vc/lesson30.zip

MS VS 2010 sample: lesson 30 in http://pmg.org.ru/nehe/LessonsCode_2010.7z

This topic is closed to new replies.

Advertisement