"FromLookDirectionLH" is a bit too complicated. Maybe a simpler api should be provided?

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?

Translated with DeepL

/**
 * @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)
}

Babylon.js Playground (babylonjs.com)

I agree that the existing FromLookDirection methods are a bit cumbersome to use, yours is better.

I think you should simplify it a bit by assuming the input vector is normalized.

Here’s how I would update the function:

Also, I did not understand why you set right.y=0?

1 Like

The right.y = 0 is used to project the right vector into the xz plane, to ensure that there is no roll rotation.

However, if the value of the up axis is vector3.Up, the calculated right.y always seems to be 0. So, it might be redundant here.

I thought about it again, and if I actually changed y it could result in several vectors not being perpendicular, so this could be a mistake.