Archived

This topic is now archived and is closed to further replies.

I need some simple math for this

This topic is 5803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm doing the collision response for a simple mini golf game that I am making (2D), and I need some math to give the ball a proper re-direction when it hit's a 45 degree angle. Picture this :
                 ___________________
/
/
/      <---------- pretend it's a 45
/
/
/   ^
|    |
|    |
|    |
|    |
|    |
|    O the ball is here and traveling upward.

It doesn't need to be anything fancy, as my game is very simple. Thanks [edited by - LostBoy on January 17, 2003 4:50:49 PM]

Share on other sites
You can decompose the velocity vector, direction and speed, of the ball into two vectors where one is perpendicular to the wall and the other is parallel to it. So call the perpendicular one O for orthogonal, P for parallel and V for velocity. So O+P=V which means P=V-O. A reflection of V across the wall is given by V=P-2O. Given a referance vector N then O=(V.N)/|N|*N/|N|=(V.N)/N.N*N where V.N is the dot product of vector V and N and |N| is the magnitude of the vector N. If N is a unit vector then it has a magnitude of 1 and the equation reduces to (V.N)*N. That is parallel to N so if you want O to be the component orthogonal, perpendicular, to the wall then N needs to be orthogonal to the wall.

Overall the component of the velocity parallel to the wall is unchanged by the impact with the wall. The component orthogonal to the wall is reversed. That is why you subtract two times it, i.e. 1-2=-1.

Share on other sites
What I would do:

I''d get the normal of the line, find the dot product of this and the velocity vector. Multiply the normal by 2 times the dot product, and subtract this vector from the velocity.

You can get the normal of the line by taking the cross product of two 3-dimensional vectors. One vector being (0, 0, 1) and the other being (x2 - x1, y2 - y1, 1)

This would give you a 3-dimensional vector with a z-component of zero.

This is what I did in my mini-golf game.

"If you gaze long into an abyss, the abyss will gaze back into you."
- Friedrich Nietzsche (1844-1900)

(my site)

Share on other sites
Thanks for the help guys, but could you please explain to me what dot product and cross product are ?

Share on other sites
There''s a way to do this without dot products, etc., as long as you''re dealing with 0, 45, or 90 degree walls, due to symmetry. It requires some logic, but at least the math is simpler. I won''t explain it in terms of angles, but in terms of velocities. I''m also going to assume there is no friction (which slows the ball along the direction of the wall) or coefficient of restitution (which makes the bounce normal to the wall imperfect). The symmetry breaks down if there is friction or coefficient of restitution, and you do need to introduce the dot product at least in this case.

Assume the ball has a velocity (Vx, Vy) to begin with. Again, perfect, lossless collisions.

If the ball hits a vertical wall, the velocity after the hit will be (-Vx, Vy).

If the ball hits a horizontal wall, the velocity after the hit will be (Vx, -Vy).

If the ball hits a 45 degree angle wall that is angled from the lower-left to the upper-right (e.g., upper-left and lower-right corner walls), the velocity after the hit will be (Vy, Vx). The velocity components just switch positions. For example, if the ball is moving vertically upward (Vx, 0) towards the 45 degree wall in the upper-left corner of the area (the wall itself is oriented from the lower-left to the upper-right) then afterwards the ball will be moving at (0, Vx), horizontally to the right.

If the ball hits a 45 degree angle wall that is angled from the upper-left to the lower-right (e.g., lower-left and upper-right corner walls), the velocity after the hit will be (-Vx, -Vy). Kind of like the other 45 degree wall case, but the velocity components are negated in additioned to flipped.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633655
• Total Posts
3013177
×