🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Need help changing Quaternion Camera Class
I've been looking for some algorithm for camera movements and i came across the nehe's quaternion camera class. I've read the tutorial and played with the code and i've noticed that the orientation is:
x-axis is left to right, y-axis is top to bottom, and z-axis is front to back.
But the orientation i need is:
x-axis is left to right, y-axis is front to back, and z-axis is top to bottom.
I thought the fix would be simple, so i changed the
CreateFromAxisAngle(&cam->m_qHeading,0.0f, 1.0f, 0.0f, cam->m_HeadingDegrees);
to
CreateFromAxisAngle(&cam->m_qHeading,0.0f, 0.0f, 1.0f, cam->m_HeadingDegrees);
but i can't find the correct direction vector from the algorithm. I've been trying for a day and i've pretty much in dispair. Can anyone look at my code and give me any type of advice or some kind of insight?
void SetPrespective(glCamera *cam)
{
GLfloat Matrix[16];
glQuaternion q;
// Make the Quaternions that will represent our rotations
CreateFromAxisAngle(&cam->m_qPitch,1.0f, 0.0f, 0.0f, cam->m_PitchDegrees);
CreateFromAxisAngle(&cam->m_qHeading,0.0f, 0.0f, 1.0f, cam->m_HeadingDegrees);
// Combine the pitch and heading rotations and store the results in q
q = QuaternionMultiply(&cam->m_qPitch,&cam->m_qHeading);
CreateMatrix(&q,Matrix);
// Let OpenGL set our new prespective on the world!
glMultMatrixf(Matrix);
// Create a matrix from the pitch Quaternion and get the j vector
// for our direction.
CreateMatrix(&cam->m_qPitch,Matrix);
cam->m_DirectionVector.j = Matrix[9];
// Combine the heading and pitch rotations and make a matrix to get
// the i and j vectors for our direction.
q = QuaternionMultiply(&cam->m_qHeading,&cam->m_qPitch);
CreateMatrix(&q,Matrix);
cam->m_DirectionVector.i = Matrix[8];
cam->m_DirectionVector.k = Matrix[10];
// Scale the direction by our speed.
cam->m_DirectionVector.i *= cam->m_ZoomVelocity;
cam->m_DirectionVector.j *= cam->m_ZoomVelocity;
cam->m_DirectionVector.k *= cam->m_ZoomVelocity;
// Increment our position by the vector
cam->m_Position.x += cam->m_DirectionVector.i;
cam->m_Position.y += cam->m_DirectionVector.j;
cam->m_Position.z += cam->m_DirectionVector.k;
cam->m_ZoomVelocity = 0.f;
if(cam->m_Position.y<boundary3DHeight){
AdjustElevation(cam);
}
glTranslatef(-cam->m_Position.x, -cam->m_Position.y, -cam->m_Position.z);
}
//Quaternion Section
void CreateFromAxisAngle(glQuaternion *quat,GLfloat x, GLfloat y, GLfloat z, GLfloat degrees){
// First we want to convert the degrees to radians
// since the angle is assumed to be in radians
GLfloat angle = (GLfloat)((degrees / 180.0f) * PI);
// Here we calculate the sin( theta / 2) once for optimization
GLfloat result = (GLfloat)sin( angle / 2.0f );
// Calcualte the w value by cos( theta / 2 )
quat->w = (GLfloat)cos( angle / 2.0f );
// Calculate the x, y and z of the quaternion
quat->x = (GLfloat)(x * result);
quat->y = (GLfloat)(y * result);
quat->z = (GLfloat)(z * result);
}
void CreateMatrix(glQuaternion *quat, GLfloat *pMatrix)
{
// Make sure the matrix has allocated memory to store the rotation data
if(!pMatrix) return;
// First row
pMatrix[ 0] = 1.0f - 2.0f * ( quat->y * quat->y + quat->z * quat->z );
pMatrix[ 1] = 2.0f * (quat->x * quat->y + quat->z * quat->w);
pMatrix[ 2] = 2.0f * (quat->x * quat->z - quat->y * quat->w);
pMatrix[ 3] = 0.0f;
// Second row
pMatrix[ 4] = 2.0f * ( quat->x * quat->y - quat->z * quat->w );
pMatrix[ 5] = 1.0f - 2.0f * ( quat->x * quat->x + quat->z * quat->z );
pMatrix[ 6] = 2.0f * (quat->z * quat->y + quat->x * quat->w );
pMatrix[ 7] = 0.0f;
// Third row
pMatrix[ 8] = 2.0f * ( quat->x * quat->z + quat->y * quat->w );
pMatrix[ 9] = 2.0f * ( quat->y * quat->z - quat->x * quat->w );
pMatrix[10] = 1.0f - 2.0f * ( quat->x * quat->x + quat->y * quat->y );
pMatrix[11] = 0.0f;
// Fourth row
pMatrix[12] = 0;
pMatrix[13] = 0;
pMatrix[14] = 0;
pMatrix[15] = 1.0f;
// Now pMatrix[] is a 4x4 homogeneous matrix that can be applied to an OpenGL Matrix
}
glQuaternion QuaternionMultiply(const glQuaternion *q1, const glQuaternion *q2)
{
glQuaternion result;
result.w = q1->w*q2->w - q1->x*q2->x - q1->y*q2->y - q1->z*q2->z;
result.x = q1->w*q2->x + q1->x*q2->w + q1->y*q2->z - q1->z*q2->y;
result.y = q1->w*q2->y + q1->y*q2->w + q1->z*q2->x - q1->x*q2->z;
result.z = q1->w*q2->z + q1->z*q2->w + q1->x*q2->y - q1->y*q2->x;
return result;
}
Thanks in advance
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement