satguru Posted December 7, 2016 Share Posted December 7, 2016 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? Quote Link to comment Share on other sites More sharing options...
heyzxz Posted December 7, 2016 Share Posted December 7, 2016 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. satguru and RaananW 2 Quote Link to comment Share on other sites More sharing options...
digitspro Posted December 7, 2016 Share Posted December 7, 2016 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 satguru 1 Quote Link to comment Share on other sites More sharing options...
RaananW Posted December 7, 2016 Share Posted December 7, 2016 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. satguru 1 Quote Link to comment Share on other sites More sharing options...
satguru Posted December 8, 2016 Author Share Posted December 8, 2016 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()? Quote Link to comment Share on other sites More sharing options...
heyzxz Posted December 8, 2016 Share Posted December 8, 2016 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.