Movement direction and velocity of geom/body

Topics: Developer Forum
Apr 2, 2008 at 11:53 PM
Hi, I'm running into some trouble figuring out how to determine in which direction my geom is moving. I have sort of figured how to know on what side the geom (a rectangle btw) is lying/standing.
But I would really like to be able to see in which direction it is moving and also how fast it is moving or with how much force it is being moved, as I hoped to use that to add some sort of speed limit to the object.
This is for a game, just to be clear. And kudos for the great and powerful engine.

-Dicki
Apr 3, 2008 at 12:41 AM
Hi Dicki. The attribute LinearVelocity on the Body object gives you a vector that defines the velocity it is moving in X and Y. You can also set it to whatever you want to limit the speed like

if (body.LinearVelocity.X > maxSpeed)
body.LinearVelocity.X = maxSpeed

Hope it helps.
Jul 3, 2008 at 3:27 AM


rgaino wrote:
Hi Dicki. The attribute LinearVelocity on the Body object gives you a vector that defines the velocity it is moving in X and Y. You can also set it to whatever you want to limit the speed like

if (body.LinearVelocity.X > maxSpeed)
body.LinearVelocity.X = maxSpeed

Hope it helps.


I tried your code above and it tells me the return value LinearVelocity cannot be modified because it is not a variable?  How do you go about limiting the velocity if that is the case?
Jul 3, 2008 at 7:40 PM
Edited Jul 3, 2008 at 7:41 PM
hey,

You have to set the LinearVelocity to a new Vector2, not set the properties of the exposed LinearVelocity property independently.

// copy the velocity so it can be modified
Vector2 desiredSpeed = body.LinearVelocity;

// constrain like so ...
if(desiredSpeed.X > maxX)
{
  desiredSpeed.X = maxX;
}

// ... or like so
desiredSpeed.Y = Math.Min(maxY, desiredSpeed.Y);

// apply the restricted velocity
body.LinearVelocity = desiredSpeed;