Babylon.js icon indicating copy to clipboard operation
Babylon.js copied to clipboard

Coordinate Handedness Inversion in `LookDirectionLHToRef` and `LookDirectionRHToRef`

Open codeCraft-Ritik opened this issue 2 months ago • 0 comments

Issue : Coordinate Handedness Inversion in LookDirectionLHToRef and LookDirectionRHToRef

  • Package: @babylonjs/core
  • Subsystem: Linear Algebra / 3D Coordinate System Transformations
  • Affected File: packages/dev/core/src/Maths/math.vector.pure.ts (Lines 8140–8182 & Lines 5820–5825)
  • Status: Mathematical bug on master branch

1. Problem Description

Babylon.js provides utility functions to construct view and orientation matrices from direction vectors:

  • Matrix.LookDirectionLHToRef(forward, up, result) (Left-Handed)
  • Matrix.LookDirectionRHToRef(forward, up, result) (Right-Handed)

Due to an inversion in the mathematical formulations, LookDirectionLHToRef implements the Right-Handed (OpenGL) formula, while LookDirectionRHToRef implements the Left-Handed (DirectX) formula.

Because Quaternion.FromLookDirectionLHToRef directly delegates to Matrix.LookDirectionLHToRef, all quaternions generated to look towards a given forward direction in Left-Handed space face 180 degrees backward.

This bug is so pervasive that existing code within the repository had to work around it by passing backward vectors (e.g. in packages/dev/gui/src/3D/controls/holographicSlate.ts:483):

// Workaround in HolographicSlate because FromLookDirectionLH inverts the direction:
this.node.rotationQuaternion = Quaternion.FromLookDirectionLH(backward, new Vector3(0, 1, 0));

2. Root Cause Analysis

Compare LookAtLHToRef and LookAtRHToRef (lines 8026–8095) with LookDirectionLHToRef and LookDirectionRHToRef (lines 8140–8182):

Standard LookAt Implementations (Reference):

  • Left-Handed (LookAtLHToRef): Eye looks down $+Z$. $$\text{zAxis} = \text{target} - \text{eye} = +\text{forward}$$ $$\text{xAxis} = \text{cross}(\text{up}, \text{zAxis})$$ $$\text{yAxis} = \text{cross}(\text{zAxis}, \text{xAxis})$$
  • Right-Handed (LookAtRHToRef): Eye looks down $-Z$. $$\text{zAxis} = \text{eye} - \text{target} = -\text{forward} = \text{back}$$ $$\text{xAxis} = \text{cross}(\text{up}, \text{zAxis})$$ $$\text{yAxis} = \text{cross}(\text{zAxis}, \text{xAxis})$$

LookDirection Implementations in math.vector.pure.ts:

// Line 8140: LookDirectionLHToRef (SUPPOSED TO BE LEFT-HANDED)
public static LookDirectionLHToRef<T extends Matrix>(forward: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: T): T {
    const back = MathTmp.Vector3[0];
    back.copyFrom(forward);
    back.scaleInPlace(-1); // <-- NEGATING FORWARD IS THE RIGHT-HANDED FORMULA!
    const left = MathTmp.Vector3[1];
    Vector3.CrossToRef(up, back, left);

    Matrix.FromValuesToRef(left._x, left._y, left._z, 0.0, up._x, up._y, up._z, 0.0, back._x, back._y, back._z, 0.0, 0, 0, 0, 1.0, result);
    return result;
}

// Line 8175: LookDirectionRHToRef (SUPPOSED TO BE RIGHT-HANDED)
public static LookDirectionRHToRef<T extends Matrix>(forward: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: T): T {
    const right = MathTmp.Vector3[2];
    Vector3.CrossToRef(up, forward, right); // <-- KEEPING FORWARD POSITIVE IS THE LEFT-HANDED FORMULA!

    Matrix.FromValuesToRef(right._x, right._y, right._z, 0.0, up._x, up._y, up._z, 0.0, forward._x, forward._y, forward._z, 0.0, 0, 0, 0, 1.0, result);
    return result;
}

The mathematical assignments of LH and RH are completely swapped.

3. Step-by-Step Reproduction

import { Vector3, Matrix, Quaternion } from "@babylonjs/core/Maths/math.vector";

const forward = new Vector3(0, 0, 1);
const up = new Vector3(0, 1, 0);

// 1. In LH space, looking down (0, 0, 1) with up (0, 1, 0) should yield an Identity orientation
const matrixLH = Matrix.LookDirectionLH(forward, up);
console.log("LH Matrix m[10] (Z forward):", matrixLH.m[10]);
// Actual: -1 (Inverted / Looking backward)
// Expected: +1 (Identity)

// 2. Quaternion derived from LH direction
const quat = Quaternion.FromLookDirectionLH(forward, up);
const rotatedForward = Vector3.Forward().rotateByQuaternionToRef(quat, new Vector3());
console.log("Rotated forward vector:", rotatedForward);
// Actual: (0, 0, -1) — points opposite to input!
// Expected: (0, 0, 1)

4. Code Fix & Patch

--- a/packages/dev/core/src/Maths/math.vector.pure.ts
+++ b/packages/dev/core/src/Maths/math.vector.pure.ts
@@ -8140,13 +8140,11 @@ export class Matrix {
     public static LookDirectionLHToRef<T extends Matrix>(forward: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: T): T {
-        const back = MathTmp.Vector3[0];
-        back.copyFrom(forward);
-        back.scaleInPlace(-1);
-        const left = MathTmp.Vector3[1];
-        Vector3.CrossToRef(up, back, left);
+        const right = MathTmp.Vector3[1];
+        Vector3.CrossToRef(up, forward, right);
 
         // Generate the rotation matrix.
-        Matrix.FromValuesToRef(left._x, left._y, left._z, 0.0, up._x, up._y, up._z, 0.0, back._x, back._y, back._z, 0.0, 0, 0, 0, 1.0, result);
+        Matrix.FromValuesToRef(right._x, right._y, right._z, 0.0, up._x, up._y, up._z, 0.0, forward._x, forward._y, forward._z, 0.0, 0, 0, 0, 1.0, result);
         return result;
     }
 
@@ -8175,9 +8173,12 @@ export class Matrix {
     public static LookDirectionRHToRef<T extends Matrix>(forward: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: T): T {
-        const right = MathTmp.Vector3[2];
-        Vector3.CrossToRef(up, forward, right);
+        const back = MathTmp.Vector3[0];
+        back.copyFrom(forward);
+        back.scaleInPlace(-1);
+        const left = MathTmp.Vector3[2];
+        Vector3.CrossToRef(up, back, left);
 
         // Generate the rotation matrix.
-        Matrix.FromValuesToRef(right._x, right._y, right._z, 0.0, up._x, up._y, up._z, 0.0, forward._x, forward._y, forward._z, 0.0, 0, 0, 0, 1.0, result);
+        Matrix.FromValuesToRef(left._x, left._y, left._z, 0.0, up._x, up._y, up._z, 0.0, back._x, back._y, back._z, 0.0, 0, 0, 0, 1.0, result);
         return result;
     }

5. Contributor Value & Profile Impact

  • Why it matters: Handedness conventions are fundamental to 3D engines. Fixing this eliminates subtle bugs and removes hacky workarounds across downstream UI, physics, and camera controls.
  • Skills demonstrated: Mastery of 3D transformation matrices, quaternion representations, and left-vs-right handed coordinate geometry.

codeCraft-Ritik avatar Sep 18 '26 04:09 codeCraft-Ritik