How to force control angularVelocity for physicsImpostor?

Tried to force control angular velocity to get no rotation to my object but I failed.
Even if I set angularVelocity two times (before render and after), the object still rotates. How to stop it from rotating?

    engine.runRenderLoop( () => {
        if ( player.physicsImpostor ) {
            player.physicsImpostor.setAngularVelocity( 
                new BABYLON.Vector3( 0, 0, 0 ) 
            );
        }
        scene.render();
        if ( player.physicsImpostor ) {
            player.physicsImpostor.setAngularVelocity( 
                new BABYLON.Vector3( 0, 0, 0 ) 
            );
        }
    } );

Also tried this approach but it also doesn’t allow me to limit the angular velocity:

scene.registerBeforeRender( () => {
	if ( player.physicsImpostor ) {
		player.physicsImpostor.setAngularVelocity( new Vector3( 0, 0, 0 ) );
	}
} );
scene.registerAfterRender( () => {
	if ( player.physicsImpostor ) {
		player.physicsImpostor.setAngularVelocity( new Vector3( 0, 0, 0 ) );
	}
} );
engine.runRenderLoop( () => {
	scene.render();
} );

Example: Playground

Let’s say we are making a physics humanoid character controller that presented as a capsule impostor (or something like this) and we should prevent it from fall to its side (I mean not to lay down to its side). So we have two possible options here: freeze rotation (don’t know how to do it :grinning: ) and slightly control the rotation to always have almost the same direction of a capsule (more natural behaviour).

Adding @Cedric our physix Guru

1 Like

Found some approach to fix the rotation:

box1.physicsImpostor.registerBeforePhysicsStep( ( impostor: PhysicsImpostor ) => {
	impostor.setAngularVelocity( Vector3.Zero() );
} );

It was stupid to try limit a rotation through the renderLoop() so it’s not synchronized to physics steps.

Question:
Is there a way to call registerBeforePhysicsStep() from some centralized place for all physics objects? Not to register a function per each controlled physics object

I would have tried to do it onBeforeRenderObservable. IIRC, it’s one of the first observable available in a frame.

1 Like

do we have a guide about all the register and onBefore … functions this sounds interesting.

We have this doc page: Observables | Babylon.js Documentation (babylonjs.com) but it doesn’t list all the observables

1 Like

Maybe this is not the best solution, but using “lookAt” on your player on your Render Loop you´ll force the player´s direction

1 Like