How can I measure the velocity of a Farseer body?

I want to know how fast my body(a circle) is moving. How can I do that? How can I measure the velocity of a body?

In addition, I want to limit the velocity of a body. For example, I want that a body couldn't move with more than 50 km/h. How can I do that?

The speed of a body should be available via the LinearVelocity property.

A simple version of the answer is this:

var km = 1000f;
var hour = 3600f;
if(myBody.LinearVelocity.Length() > (50*km/hour))
{
var direction = Vector2.Normalize(myBody.LinearVelocity);
myBody.LinearVelocity = direction*(50*km/hour);
}


A more advanced but efficient way of doing it is this:

float maxSpeed = 50 / 3.6f; //50km/h -> m/s
float maxSpeedSquared = maxSpeed * maxSpeed;
float len2 = myBody.LinearVelocity.LengthSquared();
if(len2 > maxSpeedSquared)
{
float len = (float)Math.Sqrt(len2);
myBody.LinearVelocity = myBody.LinearVelocity * (1f/len) * maxSpeed;
}


It is more efficient because it do less multiplications and do only one Sqrt, and only when necessary. Usually you shouldn't bother about this, but this second way was added in case someone that stumbles into the question need to apply this to a more critical section.

• Thanx. It works perfectly. I only added (float) in this line: float len = (float)Math.Sqrt(len2); – Homer_Simpson Dec 17 '13 at 21:07