Handmade Network»Forums
Dan Zaidan
45 posts / 1 project
Game programmer. Creator of Eliosi's Hunt. I share the love I have for making games on my Youtube channel: http://youtube.com/danzaidan
Mouse questions for games
Edited by Dan Zaidan on
Hello all.

I've been experimenting with some mouse movement for my game (being made on a live stream!) and a few things came to mind.

First off, there's the problem of the mouse reaching the end of the screen. If the game uses the mouse delta to do some movement, for example, the mouse gets stuck on that side.

I did some research and apparently "wrapping" the mouse to the other side seemed like a nice solution. But I think it may give wrong results in this case:

----------------------|
p1------p2-------p3-| Edge
----------------------|

In this case, we could wrap the mouse back to the left but that frame's delta (betwen p3 and "p4", assuming a constant velocity) will be smaller that what the player actually moved, because "p4" it got clamped in the right-most mouse region.

Another solution that came to mind was to "stick" the mouse back to the center every frame, so that we only get the mouse delta and set its position back to where the player is free to move it.
That sounds nice but I'm not sure how robust is this (in the case of alt-tabbing a lot, for instance), and a bug would render the mouse useless.


So I guess my question is: how is this actually done in most games? Is sticking-the-mouse-to-the-center solution good?

Thanks, everybody!
Dan Zaidan
Simon Anciaux
1337 posts
Mouse questions for games
I thing using raw input you can have the movement of the mouse even when it doesn't move visually (lLastX and lLastY). But I've never used that so I don't know for sure.
Mārtiņš Možeiko
2559 posts / 2 projects
Mouse questions for games
Raw input for sure is way to go. If you can afford to loose mouse acceleration/speed settings. Raw input will give you raw mouse movement values regardless of user settings in Windows for mouse acceleration/speed. Because different mices are different and people have different hands, the mouse movement usually is faster/slower than raw value.

Alternative is do what you suggested - reposition mouse cursor in center of window. This is pretty normal solution. Many games do this and it works well. You just need to handle correctly loosing/gaining focus so mouse does not jump large amount when it enters window. Debug it carefully and it will work well.
Dan Zaidan
45 posts / 1 project
Game programmer. Creator of Eliosi's Hunt. I share the love I have for making games on my Youtube channel: http://youtube.com/danzaidan
Mouse questions for games
Thanks guys! I'm going to implement the center mouse solution later today on twitch! :D