Smooth mouse movement.

956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Aug 24, 2008 at 19:21

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.

6 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 24, 2008 at 19:56

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);
956bff9d106b3c4411887b3d32d140c8
0
netwalker 101 Aug 25, 2008 at 11:16

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.

17ba6d8b7ba3b6d82970a7bbba71a6de
0
vrnunes 102 Aug 25, 2008 at 15:07

– 8< – — – 8< –

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 25, 2008 at 16:38

@netwalker

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?

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

No, I run it in the main thread each frame.

500367065665a05a847242a39a0bc69e
0
mmakrzem 101 Aug 26, 2008 at 19:00

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?

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 101 Aug 26, 2008 at 21:55

Or maybe you are not scaling the movement with the frametime. That would certainly give uneven motion like you described.