Jump to content
  • Advertisement
Sign in to follow this  
Maverick Programmer

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

This topic is 3940 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!