• Advertisement
Sign in to follow this  

Help on collision detection in Pong (XNA 3.0)

This topic is 3316 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 I'm trying to do some collision detections in 2D. I have a pong game where I need to check for the collisions between the ball and paddle. I've managed to use the BoundingBox to detect the collisions, but I need to know which sides collided such that I can bounce the ball in the correct direction. How to solve this? I've tried with alot of if-else's but I didnt get it to work. The big problem I have is that when the ball's speed is high it can "enter" the paddle before the collision is detected and then I really dont know how to check if it entered from the top or the side. The if-else's I've used sofar are these:
//This is the check for the "side"-collision and it works perfectly
if (ball.ballPosition.X <= player1.playerPaddle.paddlePosition.X + player1.playerPaddle.paddleTexture.Width 
   && (ball.ballPosition.Y + ball.ballTexture.Height >= player1.playerPaddle.paddlePosition.Y 
   && ball.ballPosition.Y <= player1.playerPaddle.paddlePosition.Y + player1.playerPaddle.paddleTexture.Height))
{
   ball.ballDirection = new Vector2(-ball.ballDirection.X, ball.ballDirection.Y);
   ball.ballSpeed += 0.1f;
}

//This is the check for the top-collision
if (((ball.ballPosition.Y + ball.ballTexture.Height) <= player1.playerPaddle.paddlePosition.Y) 
   && (ball.ballPosition.X <= (player1.playerPaddle.paddlePosition.X + player1.playerPaddle.paddleTexture.Width)) 
   && (ball.ballPosition.Y >= (player1.playerPaddle.paddlePosition.Y - ball.ballTexture.Height)-2))
{
    ball.ballDirection = new Vector2(ball.ballDirection.X, -ball.ballDirection.Y);
}
else if (((ball.ballPosition.Y + ball.ballTexture.Height) <= player1.playerPaddle.paddlePosition.Y) 
   && ((ball.ballPosition.X + ball.ballTexture.Width) >= player1.playerPaddle.paddlePosition.X) 
   && (ball.ballPosition.X <= player1.playerPaddle.paddlePosition.X)
   && (ball.ballPosition.Y >= (player1.playerPaddle.paddlePosition.Y - ball.ballTexture.Height)))
{
    ball.ballDirection = new Vector2(-ball.ballDirection.X, -ball.ballDirection.Y);
}


The checks work as following:
side-collision:
- Checks if the ball hits the paddle while they overlap on both X and Y axis

top-collision:
- Checks if the ball hits the paddle while overlapping on both X and Y axis AND ???

this is where I dont know how to proceed

Sorry if the code is unreadable.. Just ask if there is something that difficult to understand. Just a note.. I'm not looking for a complete solution, just a pointer in the right direction of how to solve this puzzle.. Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
If you're having trouble with objects passing through each other in between frames, thus colliding but never "overlapping" during the frames, then you'll want to check out sweeping tests. The basic idea is that, if you picture a sphere moving through space in a straight line, it carves out a sort of capsule shape, and you can test that capsule against objects, like your paddles, and not only will it tell you whether a collision has occurred, but it will also tell you the precise moment it occurred so, if you wish, you can snap both pieces back to where they were when they first collided and then handle your collision response all you want.

Here is a great article on it:

http://www.gamasutra.com/features/19991018/Gomez_1.htm

Pong is simple enough that you can get away with some assumptions that won't quite work in other games. For instance, all of the surfaces in Pong that the ball can collide with tend to be axis-aligned. That is, they're either straight horizontal or totally vertical. No angled surfaces. So, often times, all you need to do in order to respond to a collision is flip one of the components of your velocity vector. For example, if the ball hits a left or right wall, all you have to do is vel.x = -vel.x, and if the ball hits a top or bottom wall, you just have to vel.y = -vel.y.

Of course, this won't help your first problem unless you can find out which type of surface the ball hit. For the walls around the edge of the arena, this should be pretty easy, because you're just doing a sweeping sphere vs. plane test, one at a time, and once you've happen upon a collision, you already know which plane you're testing against.

The paddles would be a little more difficult, because they move. You could break the paddles down into four planes and do sweeping plane vs. sweeping sphere tests. I don't know how to do that, but it doesn't seem like it would be difficult to derive a formula for this. Just keep in mind that planes are infinite, and you only want to test against a certain segment of the planes.


Share this post


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

  • Advertisement