Hmmm. So I now have my functions working (new ones I stole borrowed developed :whistle: ) but strange things are happening. It’s hard to describe exactly but It seems that every time I change the matrix (move the mouse or rotate the ship) the world shears and rotates and scales in all different manners. I think I have reversed the row-column stuff. Here’s my code:
//glRotatef(pitch, 1.0f, 0.0f, 0.0f);
//glRotatef(heading, 0.0f, 1.0f, 0.0f);
xaxis.x = 1.0f;
yaxis.y = 1.0f;
zaxis.z = 1.0f;
q1 = quatMult( eulerToQuat(xaxis, pitch), eulerToQuat(yaxis, heading));
q1 = quatMult( eulerToQuat(zaxis, roll) , q1);
//quatMultiply(q1, q2, q2);
shipMatrix = quatToMatrix(q1);
glMultMatrixf( shipMatrix.m );
glTranslatef(-xp,0.0f,0.0f);
glTranslatef(0.0f,-yp,0.0f);
glTranslatef(0.0f,0.0f,-zp);
drawSpace();
And the respective functions:
quaternion eulerToQuat(vec3D axis, float angle)
{
quaternion quat;
float sinAngle;
angle *= 0.5f;
normVec3D(&axis);
sinAngle = sin(angle);
quat.x = (axis.x * sinAngle);
quat.y = (axis.y * sinAngle);
quat.z = (axis.z * sinAngle);
quat.w = cos(angle);
return quat;
}
quaternion quatMult(quaternion quat2, quaternion quat3)
{
vec3D vector1, vector2, cross;
quaternion quat1;
float angle;
vector1.x = quat1.x;
vector1.y = quat1.y;
vector1.z = quat1.z;
vector2.x = quat2.x;
vector2.y = quat2.y;
vector2.z = quat2.z;
angle = (quat1.w * quat2.w) - dotprodVec3D(vector1, vector2);
cross = crossprodVec3D(vector1, vector2);
vector1.x *= quat2.w;
vector1.y *= quat2.w;
vector1.z *= quat2.w;
vector2.x *= quat1.w;
vector2.y *= quat1.w;
vector2.z *= quat1.w;
quat1.x = vector1.x + vector2.x + cross.x;
quat1.y = vector1.y + vector2.y + cross.y;
quat1.z = vector1.z + vector2.z + cross.z;
quat1.w = angle;
return quat1;
}
matrix quatToMatrix(quaternion quat) {
matrix qmatrix;
qmatrix.m[0] = (1.0f - (2.0f * ((quat.y * quat.y) + (quat.z * quat.z))));
qmatrix.m[1] = (2.0f * ((quat.x * quat.y) + (quat.z * quat.w)));
qmatrix.m[2] = (2.0f * ((quat.x * quat.z) - (quat.y * quat.w)));
qmatrix.m[3] = 0.0f;
qmatrix.m[4] = (2.0f * ((quat.x * quat.y) - (quat.z * quat.w)));
qmatrix.m[5] = (1.0f - (2.0f * ((quat.x * quat.x) + (quat.z * quat.z))));
qmatrix.m[6] = (2.0f * ((quat.y * quat.z) + (quat.x * quat.w)));
qmatrix.m[7] = 0.0f;
qmatrix.m[8] = (2.0f * ((quat.x * quat.z) + (quat.y * quat.w)));
qmatrix.m[9] = (2.0f * ((quat.y * quat.z) - (quat.x * quat.w)));
qmatrix.m[10] = (1.0f - (2.0f * ((quat.x * quat.x) + (quat.y * quat.y))));
qmatrix.m[11] = 0.0f;
qmatrix.m[12] = 0.0f;
qmatrix.m[13] = 0.0f;
qmatrix.m[14] = 0.0f;
qmatrix.m[15] = 1.0f;
return qmatrix;
}
I have a feeling I am almost there, but while I’m here, I might as well ask if you think my translation functions are OK:
xp += cos(DEG_TO_RAD * (heading - 90.0f ) ) * airspeed * dt;
zp += sin(DEG_TO_RAD * (heading - 90.0f) ) * airspeed * dt;
yp += sin(DEG_TO_RAD * -pitch ) * airspeed * dt;