liabru / matter-js

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

Simulation doesn't match expected calculation #720

Closed nosenfield closed 5 years ago

nosenfield commented 5 years ago

I have a project where the user creates a parabola to represent the path of a projectile and then can launch a projectile along that curve. I've attempted to reverse engineer the velocity from the parabola, but am running into an issue where the render for projectile doesn't match up with the curve. And when I plug the numbers into the standard motion equations they make no sense.

I created a test project with a projectile and the ground using:

var ball = Matter.Bodies.circle(0, 0, 10, { "frictionAir": 0 });
ball.isStatic = true;
Matter.World.addBody(world, ball);

var ground = Matter.Bodies.rectangle(100, 105, 200, 10);
ground.isStatic = true;
Matter.World.addBody(world, ground);

Then I launch the ball using:

ball.isStatic = false;
Matter.Body.setVelocity(ball, Matter.Vector.create(1, 0));

The collisionStart listener reports the position of the ball as: 25, 90. My understanding is that matterjs uses meters, kilograms, and seconds. If I plug these numbers into the motion equations I get: ∆x = .5at² 90 = .5 9.8 t² 4.3 = t

v = d/t v = 25 / 4.3 v = 5.8

Why does a velocity of 1 translate to a velocity of 5.8 when subbing in actual units? Is there some kind of actual conversion scale I can use?

nosenfield commented 5 years ago

Ok, figured out the problem in my understanding. Velocity is provided on a per step, not per second basis. If i adjust the above to factor in the conversion of stepsPerSecond (100 / 16.666) then it works out.

liabru commented 5 years ago

Exactly, that's correct as of the current version but I am working on an update that provides new Body getter and setter functions that handle this, sorry it isn't all that clear at the moment.

nosenfield commented 5 years ago

No worries. I think some added documentation is all you'd need to clear it up. And in the meantime, hopefully this ticket will help.