How to use getDirection with applyImpulse(or applyForce)?

Hi, Babylon.js

I’m progressively learning how to use Havok physics in Babylon.js but now stuck on figuring out how to applyImpulse(or applyForce) once I’ve rotated my mesh. There are multiple good examples in the forum on how to use getDirection to retrieve a local mesh’s axis once the mesh has been rotated and use that axis as a direction to translate that mesh along a certain local axis direction (e.g.:

However, I’d like to do something slightly different: applyImpulse(or applyForce) along a local axis after my mesh has been rotated (e.g. using the Havok physic aggregate template:

      new BABYLON.Vector3(0,0,1),

*Yet, instead of using “new BABYLON.Vector3(0,0,1)” which will always move my mesh 1 unit in the +Z axis of the World
space, I want to move 1 unit forward in relation to my mesh’s Local space in order to be able to move my mesh in the new direction it might be facing after it has been rotated.

I tried doing:


I would appreciate any suggestions.


Hi @SugarRayLua

I believe it should work with getDirection. and you should get the same value with Babylon.js docs
do you have a PG with impulse not working?

Thanks, @Cedric, I did finally manage to get it partly working with getDirection() so am now trying to get it fully working how I want it to work. I realized that actually I could also just have done:


as well.

Along the way, I realized another complexity: when I used getDirection() to determine where to apply the force, it kept coming out 180 degrees opposite of where I wanted to apply it. For some reason, my mesh’s rotation kept changing from where I set it initially. After searching the forum, I found a post which indicated that when you must pass the mesh’s rotation as a parameter into the PhysicsAggregate constructor if one uses that (which I was) or else the mesh looses its rotation after that command (and even more nuanced is that the PhysicsAggregate constructor takes a quarternion so I’ve got to use mesh(rotate) not mesh.rotation in my project)

Sorry, I didn’t post a PG on this one; I use Babylon.JS in microStudio which is configured a bit differently than the PG environment so need to convert what I’m working on back to a PG project. I’ll do so and re-post if I can’t further figure it out.

Have a great upcoming week.

1 Like


I am still having trouble with the getDirection() idea. I was able to get getDirection to work in applyImpulse() but my problem now seems to be that the direction is wrong (i.e. direction is not accounting for the rotation of the mesh I used to make the PhysicsAggregate). I searched the forum and tried many things and from what I gather, one should try and pass in the rotation as a quaternion when constructing the PhysicsAggregate. That did work in my project in terms of preserving my meshes rotation (in terms of viewing its rotationQuaternion and the PhysicsAggregate being properly rotated in the scene); however, when I did that, getDirection() didn’t seem to acknowledge that my mesh or PhysicsAggregate was rotated (i.e. returned direction of what my mesh’s rotation would be if non-rotated). I suspected that getDirection() therefore relies on the mesh.rotation property rather than the mesh.rotationQuaternion property.

Thus, I tried a new strategy: rotate the PhysicsAggregate (orMesh) after the PhysicsAggregate is created to see if getDirection might work better. However, as evidenced in the playground below, it doesn’t seem I can even rotate the PhysicsAggregate after it is created (i.e. keeping or commenting out the second to the last line where I rotate the PhysicsAggregate doesn’t seem to do anything):

Any thoughts or suggestions? The basic thing I am trying to do with this information is to create a physicsAggregate (e.g. representing a player’s character) and move it with physics forces in Havok. Doing so works fine if my mesh/physicsAggregate isn’t rotated; however, once I rotate the mesh/physicsAggregate, I need to apply a forward force in the new forward direction that the mesh/physicsAggregate is rotated towards.


disablePreStep must be false if you need to change transformNode TRS

Perfect, thank you very much, @Cedric!

1 Like


Sorry to keep following up on this but seems important; I can now definitely rotate the physicsAggregate appropriately, but getDirection doesn’t seem to take into account the rotation:

(Note getDirection still seems to think the physicsAggregate’s Z axis in the Z axis in the world space and hence the box tumbles instead of slides when try apply an impulse along what should be its Z axis)

I tried using sphereAggregate.transformNode.rotation to do the rotation instead but getDirection still didn’t seem to recognize that the sphereAggregate rotated.

Any thoughts?

Thanks :blush:

get direction is using world matrix. it’s updated before rendering in the chain of update during frame rendering.
if you change the rotation, you need to update the matrix like this:

another solution, without a matrix update is to get the quaternion and transform the vector directly like with this method:

Perfect, @Cedric, that works-- thanks, again!

I suspect others will find this information useful, too :+1::blush:

1 Like