Jump to content
  • Advertisement
Sign in to follow this  
chilun

collision detection help for Pong game in C++

This topic is 2107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

 

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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   

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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

Edited by chilun

Share this post


Link to post
Share on other sites

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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!