handmade.network » Forums » Mouse questions for games
DanZaidan
Dan Zaidan
27 posts / 1 project

22-year-old game programmer. Creator of Eliosi's Hunt. I'm live streaming the creation of a game from scratch using C.

#21252 Mouse questions for games
1 month, 1 week ago Edited by Dan Zaidan on July 11, 2019, 5:11 p.m.

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
mrmixer
Simon Anciaux
651 posts
#21253 Mouse questions for games
1 month, 1 week ago

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.
mmozeiko
Mārtiņš Možeiko
1963 posts / 1 project
#21260 Mouse questions for games
1 month, 1 week ago

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.
DanZaidan
Dan Zaidan
27 posts / 1 project

22-year-old game programmer. Creator of Eliosi's Hunt. I'm live streaming the creation of a game from scratch using C.

#21266 Mouse questions for games
1 month, 1 week ago

Thanks guys! I'm going to implement the center mouse solution later today on twitch! :D