toji / gl-matrix

Javascript Matrix and Vector library for High Performance WebGL apps
glmatrix.net
MIT License
5.37k stars 721 forks source link

Question regarding mat4.targetTo #378

Closed matthewcpp closed 1 year ago

matthewcpp commented 4 years ago

Hello, I apologize if I am missing something blatantly obvious here, but I am having a hard time understanding values I am getting from a piece of code I have written that is making use of this function even after reading related issues to it.

here is some trivial example code illustrating the crux of what I am trying to do:

let itemPosition = vec3.fromValues(0.0, 0.0, 10.0);
let itemTarget = vec3.fromValues(0.0, 0.0, 0.0);
let itemUp = vec3.fromValues(0.0, 1.0, 0.0);

let targetToMatrix = mat4.create();
mat4.targetTo(targetToMatrix, itemPosition, itemTarget, itemUp);

let rotationQuat = quat.create();
mat4.getRotation(rotationQuat, targetToMatrix);

let forwardVector = vec3.fromValues(0.0, 0.0, 1.0);
vec3.transformQuat(forwardVector, forwardVector, rotationQuat);

At this point when inspecting the values of forwardVector I am seeing: [0, 0, 1]. I would expect this to have been [0, 0, -1] . In all of the other cases I try, the resulting forward seems to be the negated vector of what I would expect as well.

I assume that I am missing something fundamental about the math here, but I would appreciate it if someone could shed some light on how i could use this function correctly to compute a forward vector from the object i am trying to rotate to its target.

I really appreciate your time! Matthew

magcius commented 3 years ago

To get from the position (0, 0, 10) to the target (0, 0, 0), one must move themselves by (0, 0, -10), or take 10 steps along the (0, 0, -1) axis.