Archived

This topic is now archived and is closed to further replies.

Quaternion camera movement

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

Recommended Posts

I'm implementing a quaternion camera, but I've got an anoying problem I can't figure out. I can rotate my camera around with the mouse and it works fine, but when I try to move along the direction of the camera, the Y and X components aren't correct unless I'm looking down the +ve Z-axis. As I turn, the Y component of the direction goes from being correct when looking down +ve Z, to 0 when looking either +ve or -ve X, then to -ve of what it should be when looking down -ve Z. The net result is that if you look forwards and up, you actually move forwards and down. Also, if you look to the side, you move in the -ve sideways direction of what you should. Here's some of the code I'm using:
//------------------------

// Function : Sets the quaternion's values

//

// Parameters : Angle, axis

//

// Returns : -

//------------------------

void CQuaternion::Set(float angle, float x, float y, float z)
{

angle *= (3.141592654f / 180.0f);

// Normalize the axis.

float mag = sqrtf((x * x) + (y * y) + (z * z));
x /= mag;
y /= mag;
z /= mag;

// Calculate the first value

m_fQuat[0] = cosf(angle / 2.0f);

float sinHA = sinf(angle / 2.0f);
m_fQuat[1] = x * sinHA;
m_fQuat[2] = y * sinHA;
m_fQuat[3] = z * sinHA;
}

//------------------------

// Function : Generates a 4x4 matrix from the quaternion

//

// Parameters : Pointer to a 4x4 matrix

//

// Returns : -

//------------------------

void CQuaternion::GetMatrix(float * mat)
{
// Normalise the quaternion

Normalise();

// Get local, easier to read copies of the values

float x = m_fQuat[1];
float y = m_fQuat[2];
float z = m_fQuat[3];
float w = m_fQuat[0];

float xx = x * x;
float yy = y * y;
float zz = z * z;

// Set the matrix

mat[0] = 1.0f - 2.0f * (yy + zz);
mat[4] = 2.0f * ((x * y) + (w * z));
mat[8] = 2.0f * ((x * z) - (w * y));
mat[12] = 0.0f;

mat[1] = 2.0f * ((x * y) - (w * z));
mat[5] = 1.0f - 2.0f * (xx + zz);
mat[9] = 2.0f * ((y * z) + (w * x));
mat[13] = 0.0f;

mat[2] = 2.0f * ((x * z) + (w * y));
mat[6] = 2.0f * ((y * z) - (w * x));
mat[10] = 1.0f - 2.0f * (xx + yy);
mat[14] = 0.0f;

mat[3] = 0.0f;
mat[7] = 0.0f;
mat[11] = 0.0f;
mat[15] = 1.0f;
}

This is how I'm getting my direction for the camera. What I'm doing is multiplying M * V where M is the matrix from the quaternion and V is a vector looking down -ve Z (0, 0, -1). Hence, it simplifies down to -1 * the third column of the matrix.
//------------------------

// Function : Converts a rotation to a direction

//

// Parameters : Rotation quaternion

//

// Returns : Direction vector

//------------------------

SVector DirectionFromRotation(CQuaternion &q)
{
SVector out;
float mat[16];

q.GetMatrix(mat);
out.x = -mat[8];
out.y = -mat[9];
out.z = -mat[10];

return out;
}

EDIT: Source tags --------------------------------------- Let's struggle for our dream of Game! http://andrewporritt.4t.com [edited by - f8k8 on April 10, 2004 9:48:53 AM]

Share on other sites
I got it. It was the DirectionFromRotation function if anyone''s interrested.

I''ve only just learned Quaternions, this is how it should be:

	CQuaternion vecq;	vecq.SetVals(0, 0, 0, -1);	CQuaternion conjq = q;	conjq.Inverse();	vecq.PostMultiply(q);	vecq.PreMultiply(conjq);	return vecq.GetAxis();

---------------------------------------

Let''s struggle for our dream of Game!

http://andrewporritt.4t.com