Changing mouse sensitivity

BlitzMax Forums/BlitzMax Programming/Changing mouse sensitivity

I'm wanting to change the sensitivity of the mouse in a game. In other words, lowering the sensitivity will require bigger mouse movement to move the pointer the same amount and raising the sensitivity will require less mouse movement to move the pointer. I have come up with a program posted here.
Strict

Const Width:Int = 800
Const Height:Int = 600
Const CenterX:Int = Width / 2
Const CenterY:Int = Height / 2

Graphics Width,Height,32
InitMouse()
Repeat
 Cls
 DrawText "UP/DOWN arrows to change sensitivity.", 10,10
 DrawText GetMouseSensitivity(),10,30
 DrawOval GetMouseX()-8,GetMouseY()-8,16,16
 If KeyHit(KEY_UP) SetMouseSensitivity(GetMouseSensitivity() + .1)
 If KeyHit(KEY_DOWN) SetMouseSensitivity(GetMouseSensitivity() - .1)
 Flip 1
Until KeyHit(KEY_ESCAPE)

 

Global MousePosX:Int
Global MousePosY:Int
Global MouseSensitivity:Float


Function InitMouse()
 MousePosX = CenterX
 MousePosY = CenterY
 HideMouse()
 MoveMouse(CenterX,CenterY)
 MouseSensitivity = 1.0
End Function

Function SetMouseSensitivity(Sensitivity:Float)
 MouseSensitivity = Sensitivity
End Function

Function GetMouseSensitivity:Float()
 Return MouseSensitivity
End Function

Function UpdateMouse()
 MousePosX :+ (MouseX() - CenterX) * MouseSensitivity
 MousePosY :+ (MouseY() - CenterY) * MouseSensitivity
 If MousePosX < 0 Then MousePosX = 0
 If MousePosX > Width - 1 Then MousePosX = Width - 1
 If MousePosY < 0 Then MousePosY = 0
 If MousePosY > Height - 1 Then MousePosY = Height - 1
 MoveMouse(CenterX,CenterY)
End Function

Function GetMouseX:Int()
 UpdateMouse()
 Return MousePosX
End Function

Function GetMouseY:Int()
 UpdateMouse()
 Return MousePosY
End Function
 

This will work. It'll even reverse the mouse movement if you set the sensitivity to a negative value. Is this a good way to do it or is there a better way?

I've done this. The problem IIRC ( and it has been a while ) is that Blitz's mouselag problem causes some input to get dropped altogether. Meaning that after some time, the mouse gets out of sync with where you really are, and it reaches a point where it thinks the edge of the screen is somewhere less than the edge of the screen.

This *may* not still be an issue, but it's a consideration. If it *is* still a problem, you need to integrate a gentle move back towards the true mouse position without interfering with the mouse input too much. It can be tricky to do this without the user feeling like the mouse has a mind of it's own.

Ideally, BRL would implement a proper system for this so we can set the mouse sensitivity directly. Most similar languages/engines seem to do that.

If you center the mouse every time you go through your mainloop, you'll avoid any problems re: screen edge.

Yes, my example centers the mouse every time GetMouseX() or GetMouseY() is called, which is called each time through the loop.
I did notice one problem though. If you have a high mouse sensitivity setting, the movement becomes more coarse. For example, a sensitivity of 4 will cause the mouse to move in groups of 4 pixels at a time. This is because the final mouse movement is calculated as a multiple of the true mouse movement. So the true mouse moves one pixel and the final mouse moves 4 pixels.