FPS: Mouse problem

Started by
4 comments, last by JamesKilton 17 years, 5 months ago

bool mouseFlag = true;
//Continue to the WinProc function...
case WM_MOUSEMOVE:
{
	if(mouseFlag)
	{
		cursorX = LOWORD(lParam);//Get the current X, Y mouse coord
		cursorY = HIWORD(lParam);

		cursorAX = (cursorX - cursorPX) / 2;//Calculate how far the mouse moved
		cursorAY = (cursorY - cursorPY) / 2;

		test->xRot += cursorAY;//Move the user's camera
		test->yRot -= cursorAX;
		test->zDir = test->yRot;

		if(test->xRot >= 90)//Keep the user from bending backwards and looking through his legs
			test->xRot = 90;
		if(test->xRot <= -90)
			test->xRot = -90;

		cursorPX = cursorX;
		cursorPY = cursorY;
	}
	break;
}

Okay, this section of code controls how the user looks around with the mouse. It works fine but there's a problem: if the mouse goes outside the window, the person stops looking. So my solution to this problem is to put the mouse in the center of the window each time the program gets to the end of the loop. So I added this section of code at the end of the rendering loop:

mouseFlag = false;
SetCursorPos(320, 240);
mouseFlag = true;

But this only keeps you from looking around. Does anyone see the problem with my code?
Advertisement
X is usually your yaw and you should be able to rotate your head/player/camera all the way around if it's a FPS.

I think you are looking to stop Y at 90 degrees so your head doesn't tumble. Y is pitch.

Mostly what I've seen is you keep your mouse position at the middle of the screen by resetting it at the top of a user input loop so that was a good idea there.

If it deviates from that point then you know the mouse is either yawing or pitching and you can do your rotations from that.

You need to rotate your view vector by using a matrix to do the rotation.

There are a few posts on here about how to make a FPS camera and you might want to take a peek/search for some.
When manually moving the mouse back to the centre of the screen, you need to assign your PX/PY 'last position' variables too. Otherwise the next mouse event undoes the last look.
Fixing the mouse in the middle of the screen is a horrible kludge, if you ask me, and your user doesn't deserve to be treated like that.

Windowed mode isn't suitable for first-person views, because it feels unnatural to have both relative and absolute mouse control simultaneously. Making the mouse cursor invisible or gluing it to your own window is against so many of the rules of GUI design, and serves to frustrate the user. My advice is to go with full-screen mode, in which exclusive mouse access is expected.

You could use WM_INPUT to pick up all mouse events whether they directly pertain to your window or not, meaning there's no need to hijack the cursor, but this will likely result in the user clicking all over the screen inadvertently and end up doing god-knows-what.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
TheAdmiral: I disagree, as long as the behaviour is turned off when you're not in an action 'mlook' part of the game, it is perfectly fine to capture the mouse in this way. Pretty much every FPS ever does so.
I can see where TheAdmiral is coming from, but FPS needs to hide the mouse from the user, in full or windowed mode. As long as the player can Alt-Tab and get control of the mouse back, then all is good. If that gets constrained too, then it's just frustration.

This topic is closed to new replies.

Advertisement