Quaternion LookRotation(lookAt, up)

This topic is 2607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

For a reference implementation, see float3x3::LookAt in MathGeoLib. (Click on the 22 lines of code label on the page for the implementation)

If you have any questions about the math, feel free to ask.

Share on other sites
I must admit the local / global stuff in the code sample confuses me. Following what you gave me, along side with this to convert a matrix to a quaternion i came up with the following function. Does it look legit? (I won't be able to test it for a little while...)

 Quaternion Quaternion::LookRotation(Vector& lookAt, Vector& up) { /*Vector forward = lookAt.Normalized(); Vector right = Vector::Cross(up.Normalized(), forward); Vector up = Vector::Cross(forward, right);*/ Vector forward = lookAt.Normalized(); Vector::OrthoNormalize(&up, &forward); // Keeps up the same, make forward orthogonal to up Vector right = Vector::Cross(up, forward); Quaternion ret; ret.w = sqrtf(1.0f + right.x + up.y + forward.z) * 0.5f; float w4_recip = 1.0f / (4.0f * ret.w); ret.x = (forward.y - up.z) * w4_recip; ret.y = (right.z - forward.x) * w4_recip; ret.z = (up.x - right.y) * w4_recip; return ret; } 

Share on other sites
Here is the code I use to convert a matrix to a quaternion: http://clb.demon.fi/MathGeoLib/docs/Quat.cpp_code.html#315

 Vector::OrthoNormalize(&up, &forward); // Keeps up the same, make forward orthogonal to up 

Assuming that line does what the comment says, don't you want to do the opposite? i.e. keep the forward vector the same, and orthogonalize up against the forward vector.

Share on other sites
I'm like 90% sure that the way Unity does it is it keeps up constant and adjusts forward to be orthonormal to up... But if that's the only thing you saw wrong i'm pretty happy! I was expecting something a LOT more complicated than creating a new vector space...

Share on other sites

I'm like 90% sure that the way Unity does it is it keeps up constant and adjusts forward to be orthonormal to up... But if that's the only thing you saw wrong i'm pretty happy! I was expecting something a LOT more complicated than creating a new vector space...

If you constrained the local up vector to stay fixed & aligned to the world up vector, then you couldn't do a pitch movement with the LookAt rotation (i.e. tilt your head up or down). When computing the source and target bases for the rotation, the forward direction is "the most important" direction, i.e. if you say you want to look towards the direction D, the function needs to compute the matrix that gives a LookAt towards D (and not D adjusted towards slightly something else). So, the order of building the direction vectors is as follows:

1. first take the direction you want to look at, and keep that fixed. Map the local front direction to this direction. Note that you see a difference in the API in my and Unity's code. Unity doesn't take as input the localForward direction, because it makes a hardcoded assumption that local forward is always +Z (0,0,1). (These kind of hardcoded assumptions can be both a blessing and a curse)

2. Then try to orient the model local up vector to align as closely towards the world up vector, so that the "head" of the model stays upright, and it doesn't look at its target in some odd slanted/rolled way. Note again, that my API expects as input the direction vector that is the local up axis, Unity has the hardcoded assumption that local up is +Y (0,1,0).

3. Then, to complete an orthonormal basis, the third vector (right) is computed. This doesn't really have any role, it just "falls in place", and is computed in the manner that the LookAt rotation does not accidentally do a mirroring (produce a LookAt rotation matrix with a determinant of -1).

To try to explain the mathematics better, I added more documentation into the code. See the updated float3x3::LookAt (click the header line for the implementation).

Share on other sites
Ah, i see what you mean! Thanks for that explanation! It cleared things up!

Share on other sites
For anyone having this problem:

 Quaternion Quaternion::LookRotation(Vector& lookAt, Vector& upDirection) { Vector forward = lookAt; Vector up = upDirection; Vector::OrthoNormalize(&forward, &up); Vector right = Vector::Cross(up, forward); #define m00 right.x #define m01 up.x #define m02 forward.x #define m10 right.y #define m11 up.y #define m12 forward.y #define m20 right.z #define m21 up.z #define m22 forward.z Quaternion ret; ret.w = sqrtf(1.0f + m00 + m11 + m22) * 0.5f; float w4_recip = 1.0f / (4.0f * ret.w); ret.x = (m21 - m12) * w4_recip; ret.y = (m02 - m20) * w4_recip; ret.z = (m10 - m01) * w4_recip; #undef m00 #undef m01 #undef m02 #undef m10 #undef m11 #undef m12 #undef m20 #undef m21 #undef m22 return ret; }

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633722
• Total Posts
3013540
×