Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dalik

Need help with collision

This topic is 5823 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

I am making a pong game and its just about done but the collision of the paddles and the ball. I am not great at math but I have an alg book with me, havent looked at it though beause I am not sure what to look for. But I would like a simple algorithm for making the ball bounce off the paddle and the wall(which should be the same I think..) Also to make things easier for myself and not having to ask another question could you display it in programming code. I dont think I need to tell you what the values I have but I will put in the alg in the code. There doesnt need to be checking for what side the ball hit the paddle I just want a mirror rebound on the ball. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
If the Ball get close to the wall/paddle just reverse the x/y increaser.

xred = value to reduce/increase x by
yred = value to reduce/increase y by

assuming 0,0 in lower left corner

if (ball.xmax >= wall.xmax || ball.xmin <= wall.xmin) {
// rebounce
xred *= -1.0f;
}

if (ball.ymax >= wall.ymax || (ball.xmax >= paddle.xmin && ball.xmin <= paddle.xmax)) {
// rebounce
yred *= -1.0f;
}

x = x + xred;
y = y + yred;

if (y < 0) GAME_OVER;

hope this helps you

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!