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

[Solved] Moving body a distance in certain time?

Topics: User Forum
Dec 6, 2012 at 8:50 PM


I have a Body which i want to move a certain distance in 1 sec.. 

For example:
My vehicle is digging into the ground. I want to smoothly move the body a Ground Blocks height in one second. How can i achieve that?

I guess it has something to do with the Update method..

My Ground Block is 46 pixels in height.

Hope someone can give me an answer :) 

Dec 6, 2012 at 10:54 PM

Well, this is physics, which means you will have to factor in time, acceleration and several physics properties.

A body has a velocity, that velocity can be controlled using two methods: ApplyForce() and ApplyLinearImpulse(). Basically you can think of them as a constant pressure over time (force) and an immediate change in velocity (impulse). Then there are damping and gravity to counteract forces, so to get a body from (X,Y) to (Z,V) within one second will require some calculations.

With that being said, depending on what behavior you would like, the simplest way is to set the Body.Position property, but that might result in unrealistic physics behavior and might also create other problems depending on the distance you need to move your body.

If you need your body to move 46 pixels (remember to use meters - would perhaps be 4.6 meters), and you need it in one second. Then it will be 0.0046 meters pr. ms, and since and update at 100 FPS is 10 ms, you need to move it 0.046 meters on each update for 100 updates to make it move 4.6 meters in one second.

Dec 15, 2012 at 9:09 AM

It works! thank you.. i couldn't get it to work because i didn't convert to sim units.. thank you!