Sign in to follow this  

Why is my camera all jumpy? What can I do to fix it?

This topic is 3752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a strange problem. My code for my camera works fine, but after a while, it 'jumps' around when I try to move the view. Here is are the primary functions:
void RotateCamera(float newAngle) {

	_lookX = -sin(newAngle);
	_lookZ = cos(newAngle);
	glLoadIdentity();
	gluLookAt(x, y, z, x + _lookX, y + _lookY, z + _lookZ, 0.0f,1.0f,0.0f);
}

void LookCamera(float newAngle) {

	_lookY = -sin(newAngle);
	_lookZ = cos(newAngle);

	glLoadIdentity();
    gluLookAt(x, y, z, x + _lookX, y + _lookY, z + _lookZ, 0.0f,1.0f,0.0f);
  
}


void MoveCamera(int newDir) {

	x = x + newDir*(_lookX)*SPEED_AMOUNT;
	z = z + newDir*(_lookZ)*SPEED_AMOUNT;
	glLoadIdentity();
	gluLookAt(x, y, z, x + _lookX, y + _lookY, z + _lookZ, 0.0f,1.0f,0.0f);
}

void StrafeCamera(int newDir)
{
	x = x + newDir*(-_lookZ)*SPEED_AMOUNT;
	z = z + newDir*(_lookX)*SPEED_AMOUNT;
	glLoadIdentity();
	gluLookAt(x, y, z, x + _lookX, y + _lookY, z + _lookZ, 0.0f,1.0f,0.0f);
}

void HandleMouse(int x, int y)
{
	if(x < mouse_x)
	{

			Angle=TURN_AMOUNT;
			glutWarpPointer(window_w/2, window_h/2);//}
	}
	else if(x > mouse_x)
	{

			Angle=-TURN_AMOUNT;
			glutWarpPointer(window_w/2, window_h/2);//}
	}
	if(y < mouse_y)
	{
			CamRot=TURN_AMOUNT;
			glutWarpPointer(window_w/2, window_h/2);//}
	}
	else if(y > mouse_y)
	{
			CamRot=-TURN_AMOUNT;
			glutWarpPointer(window_w/2, window_h/2);
	}

	mouse_x =x;
	mouse_y =y;

}

Here is the main cycle:
void update(int value) {

	if(Angle!=0&&!isStrafing)
	{
		_angle+=Angle;
		RotateCamera(_angle);
		Angle=0;
	}

	if(CamRot!=0)
	{
		_camRot+=CamRot;
		LookCamera(_camRot);
		CamRot=0;
	}

	if(Direction!=0)
	{
		MoveCamera(Direction);
	}

	if(StrafeDirection!=0&&isStrafing)
	{
		StrafeCamera(StrafeDirection);
	}


	glutPostRedisplay();
	glutTimerFunc(25, update, 0);
}

I'm sure it has something to do with the math. Thus, I posted it here. Thanks in advanced.

Share this post


Link to post
Share on other sites
Clarify "jumps". A jump could be anything between a twitch and a 180.

Share this post


Link to post
Share on other sites
I think I may have solved it, but I'm not sure. By 'jumps' I meant ti will work fine, then it will start 'twitching' when you go move it, then it starts reversing directions. I think it was because I called _lookZ on both functions, confusing it. I took away _lookZ in the LookCamera function. It's working now, but I haven't tried it long enough to be sure.

Share this post


Link to post
Share on other sites

This topic is 3752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this