Created
October 8, 2012 14:00
-
-
Save kenpower/3852682 to your computer and use it in GitHub Desktop.
Simple collision response between two equally massive objects (Assumed to be spherical)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static void bounce(Shape shape1, Shape shape2) | |
{ | |
Vector2 displacement = shape1._position - shape2._position; | |
Vector2 closingspeed = shape1._velocity - shape2._velocity; | |
//check if they are already moving apart | |
if(Vector2.Dot(displacement,closingspeed)>=0){ | |
return;// if moving apart, do nothing | |
} | |
displacement.Normalize(); | |
Vector2 bounce; | |
//reverse the velocity vector in the direction of displacement | |
bounce= Vector2.Dot(shape1._velocity, displacement) * displacement; | |
shape1._velocity -= 2 * bounce; | |
bounce = Vector2.Dot(shape2._velocity, displacement) * displacement; | |
shape2._velocity -= 2 * bounce; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment