BabylonJS/Babylon.js · error · Error

Matrix is not invertible

Error message

Matrix is not invertible

What it means

FlowGraphMatrix2D.inverse() computes the 2x2 determinant and refuses to invert a singular matrix (det === 0) since the inverse would divide by zero. The matrix is degenerate — it collapses 2D space onto a line or point.

Source

Thrown at packages/dev/core/src/FlowGraph/CustomTypes/flowGraphMatrix.ts:299

    public subtract(other: FlowGraphMatrix2D): FlowGraphMatrix2D {
        return this.subtractToRef(other, new FlowGraphMatrix2D());
    }

    public transpose(): FlowGraphMatrix2D {
        const m = this._m;
        return new FlowGraphMatrix2D([m[0], m[2], m[1], m[3]]);
    }

    public determinant(): number {
        const m = this._m;
        return m[0] * m[3] - m[1] * m[2];
    }

    public inverse(): FlowGraphMatrix2D {
        const det = this.determinant();
        if (det === 0) {
            throw new Error("Matrix is not invertible");
        }
        const m = this._m;
        const invDet = 1 / det;
        return new FlowGraphMatrix2D([m[3] * invDet, -m[1] * invDet, -m[2] * invDet, m[0] * invDet]);
    }

    public equals(other: IFlowGraphMatrix<Vector2>, epsilon: number = 0): boolean {
        const m = this._m;
        const o = other.m;
        if (epsilon === 0) {
            return m[0] === o[0] && m[1] === o[1] && m[2] === o[2] && m[3] === o[3];
        }
        return Math.abs(m[0] - o[0]) < epsilon && Math.abs(m[1] - o[1]) < epsilon && Math.abs(m[2] - o[2]) < epsilon && Math.abs(m[3] - o[3]) < epsilon;
    }

    public getClassName(): string {
        return "FlowGraphMatrix2D";
    }

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Check the determinant before inverting and handle the singular case (identity fallback or error path)
  2. Fix the upstream transform data producing the zero determinant (non-zero scale, non-collinear basis)
  3. If a near-singular matrix is expected, use a regularized/pseudo-inverse approach instead of exact inverse

Example fix

// before
const inv = m.inverse(); // throws when det===0
// after
const inv = m.determinant() !== 0 ? m.inverse() : FlowGraphMatrix2D.Identity();
Defensive patterns

Strategy: validation

Validate before calling

if (m.determinant() === 0) { /* use identity or abort */ }
const inv = m.determinant() !== 0 ? m.inverse() : FlowGraphMatrix2D.Identity();

Type guard

const isInvertible2D = (m: FlowGraphMatrix2D): boolean => m.determinant() !== 0;

Try / catch

try { inv = m.inverse(); } catch (e) { if (e.message === 'Matrix is not invertible') { inv = FlowGraphMatrix2D.Identity(); } else throw e; }

Prevention

When it happens

Trigger: Calling inverse() on a matrix whose determinant is exactly 0, e.g. one built from scale (0,0), collinear basis vectors, or accumulated degenerate transforms.

Common situations: Animated scale interpolating through 0; matrices constructed from empty/degenerate input data; default/uninitialized matrices filled with zeros; computing UV/2D transforms from geometry with zero area.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/af2f6de525bc2955. Report an issue: GitHub.