josephg / Chipmunk-js

Port of slembcke/Chipmunk-Physics to Javascript
http://dl.dropbox.com/u/2494815/demo/Joints.html
536 stars 59 forks source link

Body.applyForce is mis-implemented as Body.applyImpulse #45

Open SHI-Yu-Zhe opened 2 years ago

SHI-Yu-Zhe commented 2 years ago

This issue serves as a report about a potential bug in the function cp.Body.applyForce. I believe that it is mis-implemented as the function cp.Body.applyForce.

According to Chipmunk documentations, this function applys force $$F$$ to the body, in time $$dt$$. It should be

f=F(t)

However, in line 1767, cp.js, the function is implemented as

Body.prototype.applyForce = function(force, r)
{
    this.activate();
    this.f = vadd(this.f, force);
    this.t += vcross(r, force);
};

This leads to add $$F(t)$$ cumulatively in every $$dt$$, thus calculating the integration of $F$ over time domain, and that is essentially calculating impulse of the force from the beginning to now

I=\int F(t)dt

To apply force, the code should be

Body.prototype.applyForce = function(force, r)
{
    this.activate();
    // this.f = vadd(this.f, force);
    this.f = force;
    this.t += vcross(r, force);
};

I hope the team could check and fix this bug. Thanks for your attention.