This project has moved. For the latest updates, please go here.

Moving body with constant velocity on non-flat ground

Topics: User Forum
Jan 17, 2011 at 3:02 PM
Edited Jan 17, 2011 at 3:04 PM


I have in my game map like this:


Let's assume that this rectangle is some kind of unit. It should travel with constant velocity(additionally it would be nice if the ground angle affect the velocity, but the velocity, after modification, will be const(eg. angle 40*("upward") - +20%, angle -40*("downward") - -20%)) to the right side of the map colliding only with a ground but it also should adjust its rotation. As a workaround I set the friction of rectangle and the ground to 1, forbade the rectangle to rotate and I'm manually modifying the position, pseudo-code:


ground.Friction = 1f;
rectangle.Friction = 1f;
rectangle.FixedRotation = true;

rectangle.Position += delta * velocity;


It works but I'm not satisfied(rotation...).


PS. Sorry for my english.

Jan 17, 2011 at 3:50 PM

If you want correct collision reaction instead of just detection, it would be better to move your rectangle with forces. Use rectangle.body.ApplyForce(). If you use a x-axis aligned vector scaled with the velocity you should get different speeds uphill and downhill automatically because of gravity and friction. In order to get a correct rotation you can add a fixedAngleJoint to the rectangle. When it collides with the ground you can get the collision normal via the contactManifold provided by the OnCollision delegate for instance. Then you could calculate the angle between the normal and the y-axis and set the fixedAngleJoint to that angle.

Jan 18, 2011 at 6:12 PM
Edited Jan 18, 2011 at 6:12 PM

Thanks for the reply. FixedAngleJoint works great.

But still I can't force the rectangle to move with constant velocity. Probably I don't know how to use ApplyForce properly. It should move always with const velocity, from the beginning of the move till it reaches the end. After applying small forces it slowly accelerate, which isn't correct for this type of games. After applying very big forces - it moves very fast, even when I use VelocityLimitController with small values. Moreover, there will be many types of units and they should move with different velocities but constant per type.

Any ideas how to do this?

Jan 18, 2011 at 6:19 PM

There are several ways of doing that.

One is to use forces and then apply damping until you reach terminal velocity. With forces everything looks real.

The other method is to use LinearVelocity directly. Setting it to a constant value will result in a constant velocity. However, this is not physically correct and can create weird behavior.

Jan 21, 2011 at 1:01 PM

After a couple of battles with forces, damping etc. I decided to change a moving model - units can accelerate, velocity is limited by controller and everything works nice. Thanks for advices.