understanding delta in Engine.update
See original GitHub issueWhile attempting to simulate a box dropping over 5 seconds, I tried to use different time granularity by varying the “delta” argument to the update function. The code is as follows:
function simulate_no_render(delta) {
// create world
var boxA = Bodies.rectangle(400, 0, 80, 80);
World.add(engine.world, [boxA]);
var total_time = 5000
var num_step = total_time / delta
for (var i = 0; i < num_step; i++ ) {
Engine.update(engine, delta)
}
console.log(engine.world.bodies[0].position)
}
I then called this function with different deltas, 2, 5, 10, and 20. Here are the results:
Object {x: 400, y: 960.4000000004701}
Object {x: 400, y: 2252.5106848838022}
Object {x: 400, y: 4016.504778211938}
Object {x: 400, y: 6360.991724002313}
I am sure I missed something, because the y position should be similar as it’s the same setting of a box being dropped over 5 seconds, the different delta should introduce some small error, i.e. bigger values of delta would cause a bigger error due to inaccuracies, but it shouldn’t be this drastic. Any clarification would be great!
If this difference is indeed caused by the different delta values, what is the delta used in Runner.run?
After reading the docs it seems the default delta value is 1000 / 60.
And it seems that changing the delta value actually changes the physics of the world, i.e. the simulation is dependent on the delta value, in that both the force and velocity are measured in units time that is the delta: a setVelocity of 10 in a delta of 10ms is twice as fast as a setVelocity of 10 in a delta of 20ms
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (5 by maintainers)
Top GitHub Comments
I’ve been working on this!
See my pull request #777 which should hopefully resolve these issues around timing and frame-rates. Those interested, if you have any time it would be great if you could experiment with that update in your code and report any issues or success, thanks.
I’m trying to run the simulation slower than 16ms, but a bigger delta time makes the objects move slower. When using correction in the world update (calculated as delta / lastDelta) things move MUCH faster than they should. This happens on an object with friction set to zero.