commonUser
commonUser

Reputation: 639

applyImpulse and applyForce accumulate forces, they don't replace them

I'm using three.js with enable3d (ammo.js)

I have little experience with graphics and physics but I've spent few hours trying to understand how ...body.appyImpulse() works and I think I've figure out that every time this method is called it accumulates the forces.

So, for instance, if I run the following:

sphere.body.applyImpulse({ x: 2, y: 0, z: 0 }, { x: 0.01, y: 0.01, z: 0 });

The sphere starts moving and rotating forward because a force A has been applied to the sphere at a specific point P1 of the sphere.

If I call that method again, the sphere will gain more velocity and rotation because a second force B is applied and summed up to A.

The question is if there is a way of, instead of adding more and more forces, changing the first one that has been applied.

So that, for example, the force A (triggered by the first call to applyImpulse) increases or decreases intensity depending on the distance between the point P1 attached to the sphere (positively charged) and a second point p2 outside the sphere (negatively charged).

Do we have any control/access over the multiple forces applied to the sphere body?

UPDATE: I think I've found a workaround to the problem which is to set, on each update, angularVelocity and velocity to 0 ({x: 0, y: 0, z: 0}) before computing and applying all the impulses.

The question remains whether there is also the possibility of managing the various forces applied through applyImpulse.

Thanks.

Upvotes: 2

Views: 958

Answers (1)

commonUser
commonUser

Reputation: 639

So after few days of testing and playing around with applyImpulse I think I can answer myself that: it is true that applyImpulse accumulates each time it is called but, for a force that lasts over time, you can set angularVelocity and velocity to 0 before using appluImpulse and things work pretty fine.

  update(delta: number): void {

    this.body.setVelocity(0, 0, 0);
    this.body.setAngularVelocity(0, 0, 0);

    this.applyImpulses();

    this.applyFriction();
    this.applyAngularFriction();
  }

Upvotes: 1

Related Questions