I've had this problem for a couple of weeks now, I hope someone can help.
I'm making an FPS type game, so I need to get the mouse delta for camera rotation.
I've tried many different tutorials for both XNA and monogame, but all of them have the same problem for me.
My input processing is as follows: pretty simple
keystate = Keyboard.GetState(); //get state of input now
mousestate = Mouse.GetState();
Point mouseRelativeToCenter = new Point(mousestate.X - windowCenterX,
mousestate.Y - windowCenterY);
mouseDelta = mouseRelativeToCenter; //how much the mouse moved this frame
if (mousestate.Position != new Point(windowCenterX, windowCenterY))
SetMouseToCenter(); //set to center of screen
keystatePrevious = keystate; //for other stuff
mousestatePrevious = mousestate;
There's some weird issue with this, mouse.getstate doesn't seem to return right values.
The issue is more prominent at lower framerates, it's just rarer at higher framerates.
Code works like this:
Get current state for mouse. (let's call this mouse position "X" for now)
Get mouse position relative to center of screen, that's mouse movement/delta this frame.
Set mouse back to center (so next frame we can get how much the mouse moved away from the center)
Update everything else for the game etc.
Now done, next frame is the same:
Get current state for mouse
Get mouse position relative to center of screen, that's mouse movement this frame
now there is a very likely chance (~50%-ish at 60 fps) that instead of returning the expected
position of the mouse the position returned is "X", the position last frame, before it was set to the center. even if the mouse is not moved at all!
This causes jerky movements because the movement is effectively doubled seemingly randomly over a couple of frames.
...and of course we set the mouse back to the center now and update everything else etc...
But this is a massive issue since I can't continue doing anything else while such a basic thing is broken.