Jump to content
  • Advertisement
Sign in to follow this  
ProjectinMatrix

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!