I am struggling with the following problem. Im working with bone animation, and I want (i.e.) the player’s head to follow another object in space. My axis is above + Z, my front axis is + Y, and the quaternion value is in W. I tried to use the mesa code for gluLookAt and use the 3x3 matrix to convert to quaternion, but it doesn’t work properly so I'm going in the other direction ...
So far I have received the following code that almost “works”, at least the player’s head rotates (however, the angle X apparently affects the rotation axis Y) in a good direction, but instead he looks directly at the next object on floor about 65 degrees:
qt LookRotation( v3 lookAt, v3 upDirection ) { qt t; v3 forward = lookAt; v3 up = upDirection; OrthoNormalize( &forward, &up ); v3 right = v3_cross( up, forward ); mat3 m = mat3_make( right.x, up.x, forward.x, right.y, up.y, forward.y, right.z, up.z, forward.z ); tw = sqrtf( 1.0f + mr[ 0 ].x + mr[ 1 ].y + mr[ 2 ].z ) * 0.5f; float w4_recip = 1.0f / ( 4.0f * tw ); tx = ( mr[ 2 ].y - mr[ 1 ].z ) * w4_recip; ty = ( mr[ 0 ].z - mr[ 2 ].x ) * w4_recip; tz = ( mr[ 1 ].x - mr[ 0 ].y ) * w4_recip; t = qt_normalize( t ); return t; }
... ... ...
v3 v = v3_sub( vec4_to_v3( transform.world.r[ 3 ] ), skeleton->final_pose.location[ i ] ); v = v3_normalize( v ); qt q = LookRotation( v, v3_make( 0.0f, 0.0f, 1.0f ) );
Can someone help me sort this out ... I am a little new with quaternions and don't know where I could get mixed up. After quite some research, basically what I want to do is something like the Unity API: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html
Mcbob source share