Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created October 8, 2012 14:00
Show Gist options
  • Save kenpower/3852682 to your computer and use it in GitHub Desktop.
Save kenpower/3852682 to your computer and use it in GitHub Desktop.
Simple collision response between two equally massive objects (Assumed to be spherical)
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