[GLUT] Moving Camera

Hello :),

i am writing a small game engine with GLUT and am trying to move the camera with “wasd” like in some EGO-SHOOTERs.

My first try was like in this tutorial:http://www.lighthouse3d.com/tutorials/glut-tutorial/the-code-so-far-vii/

In this tutorial they increment a variable each time when the renderscene is rendered. The problem is, if you have a fast computer with more fps, you run faster than on a slower computer.

How can i avoid this?

My first idea is to make a glutTimerFunc which calls every 10ms a function to move the camera.

Is this a good and right way to do this?

Thank you :slight_smile:

Hi,

You can increment the variable with a value proportional to the difference between the value of glutGet(GLUT_ELAPSED_TIME) stored at the previous frame and the value of glutGet(GLUT_ELAPSED_TIME) at the current frame.


    currentTime = glutGet(GLUT_ELAPSED_TIME);
    timeInterval = currentTime - previousTime;
    previousTime = currentTime;

Note that timeInterval is in milli-seconds, so you have certainly to scale down it if you want to increment the variable with “something that isn’t too big”

Thank You, this works very good for me.

I have to move the camera now. This is my first try:


	void CameraView::mousePosition(int x, int y)
	{
		if(mouseWarp)
		{
			mouseDirectionX += (x - this->windowWidth/2) * 0.002f;
			mouseDirectionY += (y - this->windowHeight/2) * 0.002f;
		}
	}

	void CameraView::renderCameraView()
	{
	    int currentTime = glutGet(GLUT_ELAPSED_TIME);
	    int timeInterval = currentTime - previousTime;
	    if(forward)	 	eyeZ -= timeInterval * moveforward_backward / 1000;
	    if(backward) 	eyeZ += timeInterval * moveforward_backward / 1000;
	    if(left)		eyeX -= timeInterval * moveSiteward / 1000;
	    if(right)		eyeX += timeInterval * moveSiteward / 1000;

	    //Mausbewegung =>

	    previousTime = currentTime;

	    GLdouble tmpEyeX, tmpEyeY, tmpEyeZ;
	    //Left Right rotation
	    tmpEyeX = eyeX + sin(mouseDirectionX);
	    tmpEyeZ = eyeZ -cos(mouseDirectionX);
	    //looking up/down
	    tmpEyeY = 1.85 - mouseDirectionY;

		// Reset transformations
		glLoadIdentity();
		gluLookAt(	eyeX, eyeY, eyeZ,
					tmpEyeX, tmpEyeY, tmpEyeZ ,
					0.0f, 10.0f,  0.0f);


		if(mouseWarp)
			glutWarpPointer(this->windowWidth / 2, this->windowHeight / 2);
	}

Rotating to the left or right works fine, but for looking up and down i need some help. How can i do this?

Hi,

I think that a modification has to be make here


            GLdouble tmpEyeX, tmpEyeY, tmpEyeZ;

	    //Left Right rotation
	    tmpEyeX = eyeX + sin(mouseDirectionX);
	    tmpEyeZ = eyeZ -cos(mouseDirectionX);

	    //looking up/down
	    tmpEyeY = 1.85 - mouseDirectionY;

The mouseDirectionY has certainly to be handled via a cos/sin fonction into the tmpEyeY calculation (cf. such as mouseDirectionX into tmpEyeX and tmpEyeZ calculations)

Note than the tmpEyeX and tmpEyeZ has certainly to be ehanced by another ±sin/cos(mouseDirectionY) multiplication too …

I’m using the spherical coordinate system now to solve my problem.

That works perfectly now :slight_smile:

Thank you! :slight_smile: