toji/gl-matrix

Bug: `lookAt` and `targetTo` will not work if `position` and `target` are vertical

Opened this issue · 0 comments

psnet commented

Problem

If vectors are vertical - they are parallel to up vector so cross product is zero, need to add check for lookAt and targetTo methods of mat4 if x and z of position and target are strictly equal then add small offset (ex. 0.000000001) to x or z component of position or target vectors.

Code to test

const {mat4, glMatrix} = require("gl-matrix")

glMatrix.setMatrixArrayType(Array);  // for easier test

const m1 = mat4.create();

console.log(mat4.lookAt(m1, [10, 38, 2], [10, 8, 2], [0, 1, 0]));                 // doesnt work
console.log(mat4.lookAt(m1, [10, 38, 2], [10, 8, 2 + 0.000000001], [0, 1, 0]));   // slight offset fixes this

const m2 = mat4.create();

console.log(mat4.targetTo(m2, [10, 38, 2], [10, 8, 2], [0, 1, 0]));               // doesnt work
console.log(mat4.targetTo(m2, [10, 38, 2], [10, 8, 2 + 0.000000001], [0, 1, 0])); // slight offset fixes this

Possible fix

Looks like:

// x and z are strictly equal
if (position[0] === target[0] && position[2] === target[2]) {
  target[2] += 0.000000001;
}