Jump to content

physics - applyImpulse


satguru
 Share

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

http://doc.babylonjs.com/overviews/Using_The_Physics_Engine#further-functionality-of-the-impostor-class

Now consider the following example

http://www.babylonjs-playground.com/#26LQEZ#32

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?

 

Link to comment
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
    this.velocity.vadd(velo, this.velocity);

    // 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
    this.angularVelocity.vadd(rotVelo, this.angularVelocity);
};

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.

Link to comment
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()?

 

Link to comment
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.

https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.abstractMesh.ts#L391-L394

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).

 

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...