Skip to main content
gamedev.net
🔒 Locked

FPS Aiming problem

Started by Drunken_Monkey Jan 3, 2007 at 11:59 AM 0 replies 850+ views
Original Post
Drunken_Monkey
Drunken_Monkey

case WM_MOUSEMOVE:
	
		SetCapture(hWnd);
		//This will keep the mouse captured to the window...
		//...preventing an error where the user will stop aiming.

		int cursorAX = (LOWORD(lParam) - cursorPX) / 2;//Calculate how far the mouse moved.
		int cursorAY = (HIWORD(lParam) - cursorPY) / 2;

		cursorPX = LOWORD(lParam);//Update the previous mouse location to the current one.
		cursorPY = HIWORD(lParam);

		if(LOWORD(lParam) < 100 || LOWORD(lParam) > 540)//If the mouse moves dangerously
		{												//close to the edge of the window.
			SetCursorPos(320, HIWORD(lParam));			//Re-position the cursor to the center of the window.
			cursorPX = 320;								//Make the previous mouse location the current one
		}												//so the user won't look back when the mouse is re-centered.
		if(HIWORD(lParam) < 100 || HIWORD(lParam) > 380)
		{
			SetCursorPos(LOWORD(lParam), 240);
			cursorPY = 240;
		}

		userDir += cursorAX;//Rotate the user's direction
		lookUpDown += cursorAY;//<-----------------------

		if(lookUpDown > 90)//Prevent the user from looking past 90 degrees
			lookUpDown = 90;
		if(lookUpDown < -90)
			lookUpDown = -90;

		if(userDir > 259)//Keep the degree range in the proper 0 - 360 range.
			userDir -= 360;
		if(userDir < 0)
			userDir += 360;


Moving on to the render function

glLoadIdentity();
glRotatef(lookUpDown, 1, 0, 0);
glRotatef(userDir, 0, 1, 0);
//Draw area around user


The problem I'm having is that when the mouse is re-centered by the SetCursorPos function, your aim is altered a little bit. If you were to take out the line with the //<--------------, so the user will only look left and right, the mouse will re-center without messing with the aim. I don't understand what's going on. Can anyone point out where I've gone wrong?
Palidine
Palidine
Your algorithm seems wrong. Generally:

Every frame:

1) get current mouse position.
2) Calculate offset from center of screen. That is your move delta.
3) Re-center mouse.

Use the calculated move delta to do your rotation.

i.e. the mouse is never ever off the center of the screen.

-me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.