Archived

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

xg0blin

Camera

Recommended Posts

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.
    
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;
}
    
[edited by - xg0blin on January 20, 2003 8:58:04 PM]

Share this post


Link to post
Share on other sites
youre doing it in a way that will never work and is the reason why everybody will tell you to use quaternions. you cant just collect all changes to the angles and then do them all at once without loosing the order in which they were applied. just imagine 45deg or up and rolling to the left and its two completely different results depending on the order.

only chance is to apply all rotations immediately to the matrix and hope that there will never two rotations happen at once or that they at least will be very small.

if your project is meant to show the differences i would leave it like that and when demonstrating it just say ''and thats why euler angles suck'' ,-) (you might want to drop the word gimbal lock too)

Share this post


Link to post
Share on other sites
well, that''s just the thing. I got the other two working and asked him if I could just say that''s why they didn''t work. He pointed out that other games (a flight sim he has actually) work fine with euler angles. He told me make it work basically. It''s been done before, I just can''t find a good example of how.

Share this post


Link to post
Share on other sites