NeXEkho
05-26-2010, 02:49 AM
I've looked around and the general consensus is "make a lookat matrix, convert to quaternion". But this just doesn't seem to work. I get a rotation but it's not one that correlates properly to the direction vector. I'm no maths whizz and I barely understand quaternions (though other parts of the program that manipulate quaternions are working great) and so I can't determine the problem.
//Normalize velocity vector.
float fVecX = tsMyPhysics->fVelX, fVecY = tsMyPhysics->fVelY, fVecZ = tsMyPhysics->fVelZ;
float fScalar = sqrt( ( fVecX * fVecX ) + ( fVecY * fVecY ) + ( fVecZ * fVecZ ) );
fVecX /= fScalar;
fVecY /= fScalar;
fVecZ /= fScalar;
GLfloat glfLookAtMe[ 3 ][ 3 ] = { { fVecX, fVecY, fVecZ }, { 0.0f, 1.0f, 0.0f }, { fVecX, fVecY, fVecZ } };
//Convert to 3x3 matrix.
tsMyObject->fAW = sqrt( 1.0f + glfLookAtMe[ 0 ][ 0 ] + glfLookAtMe[ 1 ][ 1 ] + glfLookAtMe[ 2 ][ 2 ] / 2.0f );
//Convert to quartenion.
float fDivisor = 4.0f * tsMyObject->fAW;
tsMyObject->fAX = ( glfLookAtMe[ 1 ][ 2 ]- glfLookAtMe[ 2 ][ 1 ] ) / fDivisor;
tsMyObject->fAY = ( glfLookAtMe[ 2 ][ 0 ]- glfLookAtMe[ 0 ][ 2 ] ) / fDivisor;
tsMyObject->fAZ = ( glfLookAtMe[ 0 ][ 1 ]- glfLookAtMe[ 1 ][ 0 ] ) / fDivisor;
Thinking it might be a problem to do with left vs. right handed matrices, I've tried swapping the column and row numbers but it had no effect. It's not perpendicular to what I am after or inverted on X or anything; it's just pointing in a seemingly random direction.
//Normalize velocity vector.
float fVecX = tsMyPhysics->fVelX, fVecY = tsMyPhysics->fVelY, fVecZ = tsMyPhysics->fVelZ;
float fScalar = sqrt( ( fVecX * fVecX ) + ( fVecY * fVecY ) + ( fVecZ * fVecZ ) );
fVecX /= fScalar;
fVecY /= fScalar;
fVecZ /= fScalar;
GLfloat glfLookAtMe[ 3 ][ 3 ] = { { fVecX, fVecY, fVecZ }, { 0.0f, 1.0f, 0.0f }, { fVecX, fVecY, fVecZ } };
//Convert to 3x3 matrix.
tsMyObject->fAW = sqrt( 1.0f + glfLookAtMe[ 0 ][ 0 ] + glfLookAtMe[ 1 ][ 1 ] + glfLookAtMe[ 2 ][ 2 ] / 2.0f );
//Convert to quartenion.
float fDivisor = 4.0f * tsMyObject->fAW;
tsMyObject->fAX = ( glfLookAtMe[ 1 ][ 2 ]- glfLookAtMe[ 2 ][ 1 ] ) / fDivisor;
tsMyObject->fAY = ( glfLookAtMe[ 2 ][ 0 ]- glfLookAtMe[ 0 ][ 2 ] ) / fDivisor;
tsMyObject->fAZ = ( glfLookAtMe[ 0 ][ 1 ]- glfLookAtMe[ 1 ][ 0 ] ) / fDivisor;
Thinking it might be a problem to do with left vs. right handed matrices, I've tried swapping the column and row numbers but it had no effect. It's not perpendicular to what I am after or inverted on X or anything; it's just pointing in a seemingly random direction.