123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|194|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Collision check!!

Wed, 11 Feb 2009, 12:59
Afr0
How would you check that the ball has collided with a paddle in a game of Pong and find the angle of impact so you can send the ball off in the right direction?
And whilst I'm at it, how do you make the computer's paddle follow the ball movement?

Edit: So far, I've found this...

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Wed, 11 Feb 2009, 13:54
Nolan
If it's left-to-right pong, you'd just switch the X Velocity of the ball. No need to find the angle of impact. Also, if the ball is perfectly round and the paddle is rectangular, just use rectangle collisions.

-=-=-
nolandc.com
Wed, 11 Feb 2009, 15:17
Afr0
Thanks guys, I think I sorted it.
Wed, 11 Feb 2009, 15:46
HoboBen
And whilst I'm at it, how do you make the computer's paddle follow the ball movement?


The usual way to do it is (assuming it's horizontal pong for this example) if the ball is lower than the padle, set the paddle direction to down. If the ball is higher, set the direction to up. You then move the paddle in that direction by a certain speed, so if the ball is going too fast at an angle the paddle won't be able to reach it in time and the human scores. If the ball is going slow enough then the paddle will be able to catch up.

-=-=-
blog | work | code | more code
Wed, 11 Feb 2009, 17:53
JL235
Nolan If it's left-to-right pong, you'd just switch the X Velocity of the ball. No need to find the angle of impact.
If you do that then you end up with the ball repeatedly following the same path. It's even possible on some boards to set the ball up to follow the exact same path, continuously.

What you want is a system to allow the player to control where the ball will go once it's bounced. Essentially that if it hits at the top or bottom of the paddle it'll fly off at the balls maximum angle (i.e. -/+ 45 degrees). If it hits right in the centre it'll fly off at 0 degrees (moving directly ahead of the paddle). I believe this is called 'english'.

You first find the distance between the centre of the paddle and the ball along one axis. The axis used is the at 90 degrees to the paddle, i.e. the y-axis in a traditional horizontal game. You then turn this into a percentage between the centre of the paddle and one edge.

For example if the paddle is 100 high, then the centre is at 50. If the ball hits at 25 then that is 50%, or 0.5. If it hits a 75 then it's -0.5 (half way in the other direction). Note that the 0.5 and -0.5 might be the other way around, I just know that one of them will be negative.

You then multiply this against the maximum angle you allow to find the balls angle. You can then sin and cos that angle and multiply against the balls set speed to find both it's new x and y speed.

One idea to make it a little more interesting is to also calculate the percentage using a sin/cos wave. This will make the angle dramatically increase near the battons edges, which could make the game more interesting.
Thu, 12 Feb 2009, 04:00
Jayenkai
Damn, you don't half come up with complex answers, DD.. Think ZX Spectrum, DD.. Think ZX Spectrum!!


When the ball hits the paddle, find distance from paddle's center.

(assuming paddle is 64 high)
YHit=abs(PaddleY-32)

Then, instead of 32-0-32, make it a more even set of numbers.
YHit=(YHit/8)+1

Which gives us 5-1-5
Then ensure one goes up, and other goes down..
If PaddleY<32 then YHit=-YHit

and set the ball to that speed.
BallY=YHit

That's pretty much the answer DD gave, but less frightening!

-=-=-
''Load, Next List!''
Thu, 12 Feb 2009, 04:43
JL235
What's important is not the algorithm, but that the player can pick the bounce angle.

Off the top of my head the code for my algorithm is something like:

Note: that the y location refers to the centre of the paddle and ball not their top-left corner. This also does not take into account the direction the ball was travelling in (i.e. if the ball was comming from the left then the xSpeed should be negative so the ball travels towards the left).