Sign in to follow this  
MYoung

How do I get relative mouse movement?

Recommended Posts

I'm a newb to Windows programming. I'm trying to get relative mouse input. I tried a search but didn't find any code. I can get the mouse coordinates, but for moving my camera with the mouse, when the mouse pointer hits the edges of the screen it cannot move any further so that when I'm calculating the delta mouse position it's 0 and thus, no camera movement. Maybe there's a way to get the mouse to wrap around the screen when it tried to go off the screen edges? I'm using VC++ 6.0. Here is a snippet of my code...
LRESULT CALLBACK WndProc(HWND hWnd,			// Handle For This Window
			UINT	uMsg,			// Message For This Window
			WPARAM	wParam,			// Additional Message Information
			LPARAM	lParam)			// Additional Message Information
{
	switch (uMsg) {					// Check For Windows Messages
	
		case WM_MOUSEMOVE: {

			// Here's a quick break down of the information we can get from this
			// message

			int flags = wParam; 	// "flags" contains flags that say whether certain
					   	// virtual keys (such as the CTRL key) are being pushed or not
						// More info on this is supplied at the bottom of this file

			int xPos = LOWORD(lParam);  	// This gives us the horizontal (the X position) of
							// the cursor

			int yPos = HIWORD(lParam);  	// This gives us the vertical (the Y position) of
							// the cursor
...

Share this post


Link to post
Share on other sites
Maybe the answer is to use SetCursorPos to reset the cursor position after checking the delta movement upon cursor movement? I'll have to give that a shot.

[Edited by - MYoung on August 18, 2004 4:31:34 PM]

Share this post


Link to post
Share on other sites
Thats what I do.


// App init
int ScreenCenterX = 320;
int ScreenCenterY = 240;

int OldMouseX = ScreenCenterX;
int OldMouseY = ScreenCenterY;
...
//WndProc
int MouseDeltaX = 0;
int MouseDeltaY = 0;

case WM_MOUSEMOVE:
MouseDeltaX = ScreenCenterX - LOWORD(lParam);
MouseDeltaY = ScreenCenterY - HIWORD(lParam);
SetCursorPos(ScreenCenterX, ScreenCenterY);
break;




Just make sure you set ScreenCenter* to within your window.

Alan

Share this post


Link to post
Share on other sites
yep, first hide the cursor then put it in the center of the screen. each frame get the delta from the center and reset it to the center. if you need the cursor visible (like a right-click to mouselook scheme) you get better visual results by still hiding it on right-click and drawing a bitmap of the mouse in the place where it was when the user clicked. there's wierd edge cases (literally) with that last scheme so i prefer the just hiding and moving to the center one, it's also how a few MMOs do things so you won't catch anyone by surprise.

-me

Share this post


Link to post
Share on other sites
Hmm, it seemed to work alright in full screen mode. I still gotta play with it in windowed mode. I do notice an extremely slight jerk to the mouse movement on occasion that I don't get with keyboard movement (when rotating in a 3D world). I guess I'll just have to keep messing with it. Thanks for the info guys!

- Mike Young
http://www.mikesshorts.com

Share this post


Link to post
Share on other sites
Doing a little reading about DirectInput, that seems like a better avenue for getting solid mouse input that won't lag. Any cons to using DirectInput? My app's gfx are in OpenGL, though if I use DirectInput I'd be relying on DirectX, correct? Though I guess DirectX is pretty standard on Windows machines. Thoughts of using DirectInput over just standard Windows event based input? Thanks again!

Share this post


Link to post
Share on other sites
If you use DirectInput you can still use OpenGL for graphics. I will have to agree about using Directinput over the windows api because it returns relative mouse movements by default.

The only con of using DirectInput would be the extra code you would have to type up to use it. Other than that DirectInput will work much better at collecting relative mouse movement than the windows api.

-TOmcAT

Share this post


Link to post
Share on other sites
Wow, I cut and pasted the proper DirectInput code from my dusty Windows Game Programming for Dummies CD-ROM and damn! Clearly a better way to get mouse input. Yes, I get props for stating the obvious! :) Thanks again for the info!

- Mike Young
http://www.mikesshorts.com

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this