Original Post
I've been working on a camera project, showing the different methods of rotation (axis-angle, quaternion, and euler angles). I've completed the axis-angle, and quaternion cameras. The euler camera seems to be the hardest one though. When I get near an axis (not even right on it) my yaw locks up. When I change quadrants sometimes my pitch goes inverted. I can't figure it out. Here's some of the code.
[edited by - xg0blin on January 20, 2003 8:58:04 PM]
void Camera::SetViewByMouse(void)
{
int MouseX, MouseY;
int CenterX = SCREENWIDTH / 2;
int CenterY = SCREENHEIGHT / 2;
static double TotalRotationX = 0.0;
SDL_GetMouseState(&MouseX, &MouseY);
if(MouseX == CenterX && MouseY == CenterY)
return;
SDL_WarpMouse(CenterX, CenterY);
Yaw += (CenterX - MouseX)/MouseSensitivity;
Pitch -= (CenterY - MouseY)/MouseSensitivity;
TotalRotationX -= Pitch;
if(TotalRotationX > 1.0)
{
TotalRotationX = 1.0;
Yaw = Pitch = Roll = 0;
return;
}
if(TotalRotationX < -1.0)
{
TotalRotationX = -1.0;
Yaw = Pitch = Roll = 0;
return;
}
else
{
RotateCamera();
Yaw = Pitch = Roll = 0;
}
}
void Camera::RotateCamera(void)
{
Vector NewView, OriginView = View - Position;
double SinYaw = sin(Yaw);
double CosYaw = cos(Yaw);
double SinPitch = sin(Pitch);
double CosPitch = cos(Pitch);
double SinRoll = sin(Roll);
double CosRoll = cos(Roll);
NewView.x = (CosRoll * CosYaw) * OriginView.x;
NewView.x += (-SinRoll * CosPitch + CosRoll * SinYaw * SinPitch) * OriginView.y;
NewView.x += (-SinRoll * -SinPitch + CosRoll * SinYaw * CosPitch) * OriginView.z;
NewView.y = (SinRoll * CosYaw) * OriginView.x;
NewView.y += (CosRoll * CosPitch + SinRoll * SinYaw * SinPitch) * OriginView.y;
NewView.y += (CosRoll * -SinPitch + SinRoll * SinYaw * CosPitch) * OriginView.z;
NewView.z = -SinYaw * OriginView.x;
NewView.z += (CosYaw * SinPitch) * OriginView.y;
NewView.z += (CosYaw * CosPitch) * OriginView.z;
View = Position + NewView;
}