I need some simple math for this

Started by
4 comments, last by LostBoy 21 years, 2 months ago
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]
Advertisement
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.
Keys to success: Ability, ambition and opportunity.
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)
MSN: stupidbackup@hotmail.com (not actual e-mail)ICQ: 26469254(my site)
Thanks for the help guys, but could you please explain to me what dot product and cross product are ?
Try Cross Product and Dot Product.
Keys to success: Ability, ambition and opportunity.
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.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement