Advertisement

Calculating ricochet in blasterball-like game

Started by February 23, 2004 10:48 PM
2 comments, last by Leroy1891 21 years ago
I coding a game like wild tangent''s blasterball game. I am having trouble calculating the new direction the ball should go when it hits a boundary. Right now the technique I use is- 1. Get the inverse of the ball''s direction before the collision 2. Calculate the angle between this vector & the line being hit 3. Calculate how much this must be rotated to get the new direction: NewAngle=180-((180-OldAngle)*2) 4. Rotate the vector by this amount to get the new direction This process is for when the angle is > 90 Unfortunately it only calculates right some of the time. I was wondering if anyone knows another technique or if anyone can spot an error in the algorithm. Thanx in advance.
I assume you are working in 2D..

since that is the case, you will need the normal of the boundary the ball is hitting.
This can be got by simply swaping the x/y direction of the bondary and negating the x.

then, the reflection is just:

reflection = ball_direction - 2 * normal * dot(normal,ball_direction);

'normal' must be unit length.

where dot is a.x*b.x + a.y*b.y

[edit]

learn about dots products. You will never need to convert to angles ever again.

| - My project website - | - email me - |

[edited by - RipTorn on February 24, 2004 5:47:41 AM]
Advertisement
this all of course assumes a frictionless ball/surface, but I assume you don''t want to get into the physics of rotational & translational momentum with dampening.
Thanks. It''s working perfect now.

This topic is closed to new replies.

Advertisement