Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to figure out a way to determine the relative velocity of a body colliding with another in a 2D environment.

For example if one body is moving at (1,0) and another traveling behind it collides with it from behind at (2,0) the velocity of the impact relative to the first body was (1,0).

I need a method which takes in two velocities, one velocity belonging to the body the velocity is being measured against, and the other for the impacting body and return the relative velocity.

share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

This is a simple operation of vector difference. In XNA you don't even need to create a specific method for it - vectors already support subtraction.

If you want the velocity of Body B from the perspective of Body A, you subtract Body A's velocity from Body B.

So in C# with XNA, using your example:

Vector2 MovingBody = new Vector2(1, 0);
Vector2 Impact = new Vector2(2, 0);
Vector2 PerceivedImpact = Impact - MovingBody;
// PerceivedImpact equals (1, 0) in this scenario
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.