Calculating ricochet in blasterball-like game
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]
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

[edited by - RipTorn on February 24, 2004 5:47:41 AM]
February 24, 2004 09:13 AM
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement