GLUT Cursor Problem

Hi. I'm new to the forum, but I have been programming for about three years. I was making an OpenGL game and recently came across a problem.

I am making a first person shooter as a fun project and so I can get the hang of programming games. In most first person shooters, as you may know, when you move the mouse the camera/player in the game would look around. I accomplish this with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void mouseMovement(int x, int y) {
	if (!((x == glutGet(GLUT_WINDOW_WIDTH) / 2)
			and (y == glutGet(GLUT_WINDOW_HEIGHT) / 2))) {
		int diffx, diffy;
		diffx = x - m_lastx;
		diffy = y - m_lasty;

		m_lastx = x;
		m_lasty = y;

		c_xrot += diffx;
		c_yrot += diffy;
		c_xrot = fmod(c_xrot + 3600, 360.0f);
		c_yrot = fmod(c_yrot + 3600, 360.0f);

		std::cout << diffx << " | " << c_xrot << " | " << count << std::endl;

		glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2,
				glutGet(GLUT_WINDOW_HEIGHT) / 2);

		count++;
	}
}


This function would be called by GLUT.

glutPassiveMotionFunc(mouseMovement);

m_lastx - Mouse's last x
m_lasty - Mouse's last y
c_xrot - Camera's x rotation (side to side)
c_yrot - Camera's y rotation (up and down)

When this is called, the x rotation of the camera hovers around 110 when it should be going up or down depending on which direction (side to side) I move the mouse. I do not actually check the y rotation, but I am absolutely sure it hovers around some number.

I calculate the difference between the previous x and y of the mouse, and change the x and y camera rotation depending on how the position of the mouse's cursor changed. This should work fine, except that I am calling glutWarpPointer. Since I am calling this, I check to see if the mouse is not in the center of the screen (since if the mouse is in the center, there is no possible way that the player could have moved it.)

When I remove the glutWarpPointer, everything is fixed, although it is possible to move the mouse out of the screen.

How do I fix this problem?

If you didn't really understand the problem, I need a way to move where the person is looking when you move the mouse without the mouse moving out of the screen if you move it too far in any direction.
try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void mouseMovement(int x, int y) {
		int diffx = x - (glutGet(GLUT_WINDOW_WIDTH) / 2);
		int diffy = y - (glutGet(GLUT_WINDOW_HEIGHT) / 2);

		c_xrot += diffx;
		c_yrot += diffy;
		c_xrot = fmod(c_xrot + 3600, 360.0f);
		c_yrot = fmod(c_yrot + 3600, 360.0f);

		std::cout << diffx << " | " << c_xrot << " | " << count << std::endl;

		glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2,
				glutGet(GLUT_WINDOW_HEIGHT) / 2);

		count++;
}


But I dont "like" the way you handle your perspective. Read something about Matrix, Rotations and Vectors and implement a Camera Class.
Last edited on
Topic archived. No new replies allowed.