liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.62k stars 1.96k forks source link

deltaTime parameter in Body#update does not work as expected. #1180

Open Andrek25 opened 1 year ago

Andrek25 commented 1 year ago

I'm doing this:

const vector = Vector.create(400, 0);
Body.setVelocity(body, vector);
const deltaTime = 500;
Body.update(body, deltaTime, 1, 1); // body, deltaTime, scaleTime, correction;

I'm assuming that passing 500ms in the deltaTime my body will get 200px moved to the right, but it does not matter if i use 1 billion ms or -1ms, it keep moving 400px exactly, so i'm interpreting that parameter badly?

Body.update(body, 16.6, 1, 1); image image

Body.update(body, 500, 1, 1); image image

liabru commented 1 year ago

Up to and including the release 0.18.0 velocity and speed units are (effectively) pixels per deltaTime ms which I think is what you're seeing, for these versions: normalise the result if not using a fixed deltaTime.

Note: this is due to become built in among other changes as part of #777 so please track that and keep an eye on release notes.

Andrek25 commented 1 year ago

Sorry i don't get it 😅 If the movement is effectvely why the deltaTime literally do anything:

// If i'm passing a higher number in the `deltaTime` parameter.
console.log(Number.MAX_SAFE_INTEGER); // => 9007199254740991

// WHY this is moving the body how it's running at `1000 / 60`?
Body.update(circle, Number.MAX_SAFE_INTEGER, 1.0, 1.0);

So i don't understand why MatterJS is fixing internally the deltaTime or whatever is happening with the Verlet integration.

Maybe i need to use Engine.update but i just want to move a body that was in the past to the present, so i need to simulate it for a higher deltaTime and i don't want to call multiple times the update function in 1 tick, can you help me with a simple example of this if it's not hard please 🙏🏻.

liabru commented 1 year ago

I'm not quite sure but worth trying out the latest release 0.19.0 as that includes #777, which has some changes related to this topic that might help.