Rotation problem
Hello. I''m trying to write a game where some of the rotation done is for spacecraft. At first I thought just tying the input directly to the rotation would do the trick. An example...
glRotatef(zAngle, 0.0f, 0.0f, 1.0f);
glRotatef(yAngle, 0.0f, 1.0f, 0.0f);
glRotatef(xAngle, 1.0f, 0.0f, 0.0f);
DrawShip(Raptor, 1);
But this didn''t handle even remotely like a real aircraft. So I fiddled with it for a while and got the rotational order X, Y, Z.
This corrected the problem with the spacecraft''s roll only. The user''s inputs (+5 to yAngle and -5 to yAngle) made the ship roll.
Changing X simply angles the ship up or down no matter what Y is. Any suggestions are appreciated.
It''s gimbal lock, you can''t put those in any order to make it work. You need to use matricies or quaternions.
Your problem is that your angles around y and z will be relative to the angle around x, and your z angle will be relative to your y angle (by doing glRotate the first time, you moved your other axis around too, making your other angles false)
You have two options:
a) as one angle changes, modify the two others accordingly
b) define yourself a set of three axis (vRightHandSide, vTop and vFront) which will be oriented as the model is oriented and your three angles will always represent the rotation around these three axis.
Oh yeah, you''ll have to use the formula for rotating a vertex around a random vector (normal):
//rotate angle degrees about a normal
const CVector Rotate(const float angle, CVector& normal) const
{
if(normal.length() != 1) normal.Normalize();
float c=cosfDeg(angle);
float s=sinfDeg(angle);
float t=1-c;
CVector vResult;
float X=normal.x;
float Y=normal.y;
float Z=normal.z;
vResult.x = x*(t*X*X+c) + y*(t*X*Y-s*Z) + z*(t*X*Z+s*Y);
vResult.y = x*(t*X*Y+s*Z) + y*(t*Y*Y+c) + z*(t*Y*Z-s*X);
vResult.z = x*(t*X*Z-s*Y) + y*(t*Y*Z+s*X) + z*(t*Z*Z+c);
return vResult;
}
p.s.: don''t forget to make sure your vector (normal) is normalized (length of 1)
You have two options:
a) as one angle changes, modify the two others accordingly
b) define yourself a set of three axis (vRightHandSide, vTop and vFront) which will be oriented as the model is oriented and your three angles will always represent the rotation around these three axis.
Oh yeah, you''ll have to use the formula for rotating a vertex around a random vector (normal):
//rotate angle degrees about a normal
const CVector Rotate(const float angle, CVector& normal) const
{
if(normal.length() != 1) normal.Normalize();
float c=cosfDeg(angle);
float s=sinfDeg(angle);
float t=1-c;
CVector vResult;
float X=normal.x;
float Y=normal.y;
float Z=normal.z;
vResult.x = x*(t*X*X+c) + y*(t*X*Y-s*Z) + z*(t*X*Z+s*Y);
vResult.y = x*(t*X*Y+s*Z) + y*(t*Y*Y+c) + z*(t*Y*Z-s*X);
vResult.z = x*(t*X*Z-s*Y) + y*(t*Y*Z+s*X) + z*(t*Z*Z+c);
return vResult;
}
p.s.: don''t forget to make sure your vector (normal) is normalized (length of 1)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement