开发者

variable timestep and acceleration

开发者 https://www.devze.com 2023-02-04 05:22 出处:网络
To move objects with a variable time step I just have to do: ship.position += ship.velocity * deltaTime;

To move objects with a variable time step I just have to do:

ship.position += ship.velocity * deltaTime;

But when I try this with:

ship.velocity += ship.power * deltaTime;

I ge开发者_如何学Got different results with different time steps. How can I fix this?

EDIT:

I am modelling an object falling to the ground on one axis with a single fixed force (gravity) acting on it.


ship.position = ship.position + ship.velocity * deltaTime + 0.5 * ship.power * deltaTime ^ 2;
ship.velocity += ship.power * deltaTime;

http://www.ugrad.math.ubc.ca/coursedoc/math101/notes/applications/velocity.html

The velocity part of your equations is correct and they must both be updated at every time step.

This all assumes that you have constant power (acceleration) over the deltaTime as pointed out by belisarius.


What you are doing (mathematically) is evaluating integrals. In the first case, the linear approximation is exact, as you have a linear relationship.

In the second case, you have at least a parabola, so your results are only approximate. You may get better results by using a smaller deltaTime, or by using the real integral equations, if available.

Edit

Brian's answer is right as long as the ship.power remains always constant, and you recalculate ship.velocity at each step. It is indeed the integral equation for a constant accelerated movement.


This is an inherent problem trying to integrate numerically. There will be an error. Lowering delta will give you more accurate results, but more computation is needed. If your power function is integrable, you could try that.


Your simulation is numerically solving the equation of motion for a single mass point. The time discretisation you are using is called "Euler method", and it is possible to show that it does not preserve energy (as the exact solution does in some way). A much better yet simple way of solving equations of motion is the "leapfrog integration".


You can use Verlet integration to calculate position and velocity of object. Acceleration you can calculate from a = m*F where m is mass and F is force. This is one of the easiest algorithm


In your code you use setInterval(moveBoxes,20) to update the boxes, and subsequently you use (new Date()).getTime()) to calculate deltaT. This is somewhat redundant, because you could have used the number 20 to calculate deltaT directly.

It is better write the code so that you use exacly the same value for deltaT during each time step. (In other words deltaT should not depend on the value of (new Date()).getTime())). This way your code becomes reproducible and it is easier for you to write unit tests.

Let us look at a situation where the browser has less CPU-time available for a short time interval. In this situation you want to avoid long term effects on the dynamics. One the lack of CPU-time is over you want the browser to return to a state that is unaffected by the short lack of CPU-time. You can achieve this by using the same value of deltaT in each time step.

By the way. I think that the following code

if(box.x < 0) {
        box.x = 0;
        box.vx *= -1;
    }

Could be replaced with

if(box.x < 0) {
        box.x *= -1 ;
        box.vx *= -1;
    }

Good luck with the project - and please include code samples in the first version of your question next time you ask :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消