Jump to content
  • Advertisement
Sign in to follow this  

Pong Collision (In Theory)

This topic is 3384 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'm working on porting my 2D Pong game from PC to Xbox (XNA) and so far everything is going pretty good. I've updated lots of the code to run more efficiently and look cleaner. One issue I've been struggling with recently though is collision of the ball against the paddle. I'm capable of determining the code to write, though I just can't seem to grasp the checks that will be needed, I'm hoping maybe you can help me. Here's some background: The ball and paddle both have bounding boxes that can determine when they collide. The ball needs to be able to bounce off the top, bottom, and 'inner side' (the side facing the other paddle). The ball currently has a direction value of (+/- 1.0, +/- 1.0) so it moves towards one of the corners when the game starts. I have been able to detect collision on the 'inner side' of the paddle by simply flipping the X-direction (which is incorrect when hitting the top) but cannot determine how to say 'if ball.collidesWith(topOfPaddle)'. Any help is appreciated!

Share this post

Link to post
Share on other sites
You can detect where the line at the top of the paddle intersects the circle for the ball.

Test whether the y-value of the center of the circle is within r (the radius) of the y-value of the line at the top of the paddle. If not, the circle can't be intersecting the line. (and the sqrt below with be negative, not a nice thing to do to your FPP).

The formula for the circle is:

x = xc +/- sqrt( r*r - (y-yc)*(y-yc) );

where xc and yc are the coordinates of the center of the circle and r is the circle's radius. Those are known values.

Pick y = y-value of the top of the paddle and solve the sqrt value. You'll also need to know the x-values for the top-left corner and the top-right corner of the paddle - call them xt1 and xt2.

If the sqrt() expression = 0 then the circle is just tangent to the line at the top of the paddle. Test if that x is between xt1 and xt2. If so, then the circle is just touching the top of the paddle.

If the sqrt() expression > 0, then you'll get 2 values for x (xc+sqrt and xc-sqrt). Those are the values of x where the line crosses the circle - call them x1 and x2.

Here you need to make several checks with x1 and x2, and the x-values of the top 2 corners - xt1 and xt2.

1. is either x1 or x2 between xt1 and xt2?

2. is either xt1 or xt2 between x1 and x2?

If the answer to any of those questions is "yes." the circle of the ball intersects the top of the paddle.

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!