Compared to the lookAt() function under transformNode, Quaternion’s FromLookDirection is a bit too complicated.
It has to consider both the left-hand or right-hand coordinate system, and how to give the up axis.
This is confusing for many people, especially those who are weak in math.
Perhaps there is a simpler api?
I have tried to create a function.
It seems to work well in both left-hand and right-hand coordinate systems, and doesn’t require the up axis to be entered.
Is there something like this in babylonjs, or am I missing something?
/**
* @param dir {BABYLON.Vector3}
* @return {BABYLON.Quaternion}
*/
function fromLookDirection(dir) {
let forward = BABYLON.TmpVectors.Vector3[0]
forward.copyFrom(dir).normalize()
let right = BABYLON.TmpVectors.Vector3[1]
BABYLON.Vector3.CrossToRef(forward, BABYLON.Vector3.Up(), right)
right.y = 0
right.normalize()
let up = BABYLON.Vector3.Cross(right, forward).normalize()
return BABYLON.Quaternion.FromLookDirectionRH(forward, up)
}