PDA

View Full Version : Detecting the speed of the mouse



twilightsupport
08-28-2004, 04:18 AM
Does anyone know a way to detect the speed of the mouse? By that I mean: how quickly is the mouse being moved at a given moment in the X and Y directions.

Of course it sounds simple... take the current position minus last position, take the elapsed time, divide the two and you're done. The problem with that is that the mouse will go out of the window eventually or hit the side of the screen and stop moving. So it seems that you need to keep re-centering it's position.

I've tried setting the mouse position on every frame back to the screen center and seem to be getting random or inconsistent results. Sometimes, even though I'm moving the mouse left, I get an occasional glitch where it appears to have moved right. And vice versa. I think this may be because I'm setting the mouse position too often. Not sure.

Is there an easy way to just get the speed of the mouse without having to make guesses and reset the position continuoulsy?

Thanks

Linusson
08-28-2004, 05:39 AM
Have you tried to read and reset only 5-10 times a second instead of every frame? If you read every frame you might be reading more often than the computer reads the input from the mouse.

What about the mouse acceleration, does the different settings on that affect your speed-check?

lakibuk
08-28-2004, 12:09 PM
In directx you can read the relative mouse movement.
Something like:


Mouse->GetDeviceState(sizeof(DIMOUSESTATE),&dims); //read mouse
mousex=mousex+dims.lX // relative x movement
mousey=mousey+dims.lY; // relative y movement

twilightsupport
08-28-2004, 05:25 PM
Have you tried to read and reset only 5-10 times a second instead of every frame? If you read every frame you might be reading more often than the computer reads the input from the mouse.

What about the mouse acceleration, does the different settings on that affect your speed-check?

I have something like that actually; it's not really every frame, but every 20 milliseconds (or more) that I read. I've tried different values for how often I read and found 20 to give slightly better results than say, 50. I've also tried averaging over 5 reads discarding what appears to be random noise. Results are better, but still seem to be inconsistent.

Haven't tried it with accceleration on.

- Alan

twilightsupport
08-28-2004, 05:30 PM
The Mouselook (http://www.gamedev.net/community/forums/topic.asp?topic_id=259034) thread in the GameDev Forum (http://www.gamedev.net/community/forums/) may answer your question, depending on what OS/API you're using.

Thanks, I'll have a look at that.

Edited to add:
I did have a look at that and discovered something interesting...

As usual, simply ignore the result from "GetTickCount" and assume a constant amount of time between calls. That, plus the averaging and discarding of noise, seems to smooth it out perfectly!

- Alan

oNyx
08-28-2004, 06:48 PM
The polling intervall differs.

serial: 40hz
usb: 125hz
ps2: 60, 80, 100 or 200hz (these are the common rates)

I just mention it because not knowing that could lead to a somewhat odd behaviour of your programm.