Jump to content


Smooth mouse movement.


6 replies to this topic

#1 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 24 August 2008 - 07:21 PM

Hi All,

I'm having a problem in trying to get nice smooth mouse movement in my simple game engine. In fact my current implementation is awful. It jerks all over the place and I look at games like Serious Sam and UT and they have REALLY smooth movement. How on earth is this achieved?. Currently I'm using the standard mouse functions of GetCursorPos/SetCursorPos to do the movement and like I say it looks pretty poor (and above all unresponsive). So any advice/suggestions on how to implement such would be really appreciated.

Many Thanks.

netwalker.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5311 posts
  • LocationSanta Clara, CA

Posted 24 August 2008 - 07:56 PM

Hmm, I'm not sure what could be causing the problem but I've always gotten quite satisfactory mouse movement using Get/SetCursorPos.

Here is the code I use to process mouse movement:
// somewhere in the global state...
static POINT mousepos;

// run every frame (or on WM_MOUSEMOVE)
POINT pt;
int deltax, deltay;
bool wrap = false;

// Get current mouse position
GetCursorPos(&pt);

// Calculate deltas
deltax = pt.x - mousepos.x;
deltay = pt.y - mousepos.y;

// Wrap mouse position if needed
if (pt.x == GetSystemMetrics(SM_CXSCREEN) - 1)
	pt.x = 0, wrap = true;
else if (pt.x == 0)
	pt.x = GetSystemMetrics(SM_CXSCREEN) - 1, wrap = true;
if (pt.y == GetSystemMetrics(SM_CYSCREEN) - 1)
	pt.y = 0, wrap = true;
else if (pt.y == 0)
	pt.y = GetSystemMetrics(SM_CYSCREEN) - 1, wrap = true;
if (wrap)
	SetCursorPos(pt.x, pt.y);

// Save off new mouse position
mousepos = pt;

// Inform the game that a mouse movement has occurred
g_pGame->MouseMoved(deltax, deltay, (wParam & MK_LBUTTON) > 0, (wParam & MK_RBUTTON) > 0);

reedbeta.com - developer blog, OpenGL demos, and other projects

#3 netwalker

    Member

  • Members
  • PipPip
  • 72 posts

Posted 25 August 2008 - 11:16 AM

Yes Reedbeta. My implementation is pretty much the same, and as result, the outcome is the same. Just out of interest, if you run your application and then strafe left or right and whilst doing this move the mouse left or right, what does the screen look like. Since on mine in looks as if there are two images (one getting erased really quickly). It just doesn't LOOK the same as those that you get in real games. How often is data actually read from the mouse device? is it 60 times p/s?, what if my game runs at 120fps? or 30fps?. I'm at a loss here.

BTW. Is that implementation of yours running in a separate thread?.

Thanks.

#4 imerso

    Senior Member

  • Members
  • PipPipPipPip
  • 431 posts
  • LocationBrasil

Posted 25 August 2008 - 03:07 PM

-- 8< -- --- -- 8< --

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 5311 posts
  • LocationSanta Clara, CA

Posted 25 August 2008 - 04:38 PM

netwalker said:

Just out of interest, if you run your application and then strafe left or right and whilst doing this move the mouse left or right, what does the screen look like. Since on mine in looks as if there are two images (one getting erased really quickly).

I haven't got my app running on this computer, so I can't check, but I can't remember having seen something like what you describe.

The "two images" thing sounds like the tearing due to vsync being turned off. Is it possible that's the problem? Or is this something totally different?

Quote

BTW. Is that implementation of yours running in a separate thread?

No, I run it in the main thread each frame.
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 MarekKnows.com

    Valued Member

  • Members
  • PipPipPip
  • 190 posts
  • LocationOntario, Canada

Posted 26 August 2008 - 07:00 PM

It sounds like you are getting tearing as Reedbeta suggested.

One other thing that I would like to ask is how many degrees do you rotate your camera when you get motion from the mouse? Could it be that you are moving too much?
C++, 3D OpenGL and Game Programming video tutorials:
www.MarekKnows.com
Play my free games: Ghost Toast, Zing

#7 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 26 August 2008 - 09:55 PM

Or maybe you are not scaling the movement with the frametime. That would certainly give uneven motion like you described.
"Stupid bug! You go squish now!!" - Homer Simpson





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users