I've been working on a Pong game so I could learn how to work with SFML and C++ better. I got SFML to display the score, render a paddle, got a background on it, and now I need a way of getting Collision Detection and Physics for the ball.
I used google and all I found was Box2D and Chipmunk, I'm thinking of going with Box2D since I think Chipmunk is C.
The problem is, I don't know how to implement Box2D. Is it like a library? How do I install it? Will I need to re-write all my code?
Thanks people.
You don't really need a library do to collision detection for pong. Box2d is a physics engine. I think it would be simpler to just make your own collision detection.
If only I had any idea how. Can you give me a push in the right direction?
Pong is probably simple enough you could do the coding yourself if you understand simple geometry and algebra.
Basically all you do is every frame, check how far the ball is away from each paddle. When it gets within a certain range, you go into a more precise check that checks if the edge of the ball is touching the edge of the paddle. This way, you don't bog the cpu down with checking if it's touching both paddles every single frame, since you do a simple distance check until it starts getting close to one.
It's easy to find the edge of the ball, since you probably know it's center point and radius. So to get the distance from the ball to the paddle you do abs((x2-x1)/(y2-y1)) where x1 and y1 are the center of the ball, and x2 and y2 are the center of the paddle.
Then to get the edge of the ball, you just take the center point and add or subtract the radius depending on what side the expected collision will be. (ie. subtract if it's going to hit the left paddle, add if it's the right paddle).
Then the edge of the paddle, you just need to know the height/width of the paddle and it's center. so something like if ball.x is 1/2 paddle.width away from paddle.x it might be touching the paddle, or at least it's on the same y-axis as it. The you just check if it's in the range of paddle.y + 1/2 paddle.height or paddle.y - 1=2 paddle.y to see if it's actually touching the paddle.
Thanks, that's definitely useful but I'm only going to work on it tomorrow because I don't want to mess up this part.
Thanks again.
Sorry, you need to Log In to post a reply to this thread.