# physics - applyImpulse

## Recommended Posts

Not sure I understand this well.

As per my understanding applyImpulse takes two variables

The first variable is the direction and amount of impulse to apply. The second is where on the body itself the force will be applied.

This as per documentation here

Now consider the following example

here I am applying a small impulse at the center of sphere and it moves slowly as expected.

But now comment line 31 and un-comment line 32 and run.

In line 32 I am applying a force at a point which is not on the sphere at all.

You would think the sphere would not move at all  but instead it does move and moves with high speed.

How come?

##### Share on other sites

See the applyImpulse method in cannon.js:

``````Body.prototype.applyImpulse = function(impulse, relativePoint){
if(this.type !== Body.DYNAMIC){
return;
}

// Compute point position relative to the body center
var r = relativePoint;

// Compute produced central impulse velocity
var velo = Body_applyImpulse_velo;
velo.copy(impulse);
velo.mult(this.invMass,velo);

// Add linear impulse

// Compute produced rotational impulse velocity
var rotVelo = Body_applyImpulse_rotVelo;
r.cross(impulse,rotVelo);

/*
rotVelo.x *= this.invInertia.x;
rotVelo.y *= this.invInertia.y;
rotVelo.z *= this.invInertia.z;
*/
this.invInertiaWorld.vmult(rotVelo,rotVelo);

// Add rotational Impulse
};``````

The problem is the engine doesn't check if the 'relativePoint' (your second parameter) is on the body (sphere) or not, instead it just simply treat it as a valid contact point. So in your line 32, your 'relativePoint' is too far away from the center of the sphere, which leads to a huge angular velocity，and the angular velocity is just at the right angle which makes the sphere rolling on the ground. So finally you see the sphere moving fast on the ground.

##### Share on other sites

Yea.. If I want to make sure the contact point touches the mesh, I'd wrap the applyImpulse call with a mesh.intersectsPoint(contactPoint) check

##### Share on other sites

Just wanted to add to @heyzxz's wonderful answer, that it will add a lot of overhead to actually test that. Maybe, before forcing an intersects call, try seeing if you can make sure it is always the case in some other way.

I don't know how you calculate the relative point, so I can't really help with that.

##### Share on other sites

Thanks @heyzxz

Makes sense though I am not sure if  the location of impulse is relative.

The example in the document gives a different impression.

it shows

impostor.applyImpulse(new BABYLON.Vector3(10, 10, 0), sphere.getAbsolutePosition());

why getAbsoluteLocation()?

##### Share on other sites

I guess in the document, it just wants to show you how to apply an impulse on the center of the sphere, since in most case the sphere doesn't have a parent, which means the  'getAbsolutePosition()' and 'sphere.position' should be the same thing. But using 'getAbsolutePosition()' makes sure the position(or I should say the 'absolute position') is up to date.

But in the case if the sphere has a parent or if you want to apply impulse NOT on the sphere center, you may not use 'getAbsolutePosition()', instead you have your own way to calculate the location of the impulse (based on your requirement).

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.