Hi @Matthias22
The model is facing west, but moving north. You need both orientation and velocity.
Orientation can be calculated as either
Model originally has facing direction (0,0,1). Extract RotaionMatrix from model’s world matrix and apply it to the original facing direction:
const originalFacing = new BABYLON.Vector3(0, 0, 1);
const facing = BABYLON.Vector3.TransformCoordinates(originalFacing, dude.getWorldMatrix().getRotationMatrix());
facing.normalize();
console.log(facing);
Or using @Necips 's code.
var originalOrientation = new BABYLON.Vector3(0, 0, 1);
var orientation = dude.getDirection(originalOrientation);
orientation.normalize();
console.log(orientation);
Both should give the same result.
You also need the velocity vector, which is the moving direction of your model. Because the PG moves the model towards north (dude.position.z-= 0.005;
), so the velocity is hardcoded to the following in the PG.
const velocity = new BABYLON.Vector3(0, 0, -1);
velocity.normalize();
If you can obtain velocity from physicsImpostor.getLinearVelocity(). Use it instead.
You can then calculate the angle between the orientation and velocity.
const angleRadian = BABYLON.Vector3.GetAngleBetweenVectors(orientation, velocity, new BABYLON.Vector3(0, 1, 0));
const angleDegree = BABYLON.Angle.FromRadians(angleRadian).degrees();
console.log(angleDegree);
It will give angle in degree = 270. Which means your model is moving right.