Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
psnet opened this issue Mar 16, 2024 · 0 comments
Open

Comments

@psnet
Copy link

psnet commented Mar 16, 2024

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;
}
@psnet psnet changed the title Bug: lookAt and targeTo will not work if position and target are vertical Bug: lookAt and targetTo will not work if position and target are vertical Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant