I’ve made a few games in XNA before and I’m about to start a new project. One thing I’d like to do is have the mouse movement.
Just to clarify (as I’ve seen some similar questions lead to confusion) I want to get the movement of the mouse. Not the position of the cursor or the change in position from one frame to the next. I would simply like data about how the mouse has been moved.
In my previous game I simply reset the (hidden) cursor to the middle of the viewport and looked at the change in position. However this seems at little bit of a cheat and leads to some messy looking calculations in my code.
So is there any way to have the mouse movement returned to the program?
Thanks,
Matt
Edit:
In response to first comment. The position of the cursor I’m refering to in this case is the position onscreen of the mouse pointer. The movement of the mouse I’m refering to is pysically moving the mouse.
XNA seems to have the word mouse to be synonymous with pointer (or cursor).
The problems I’m having is that despite the mouse moving left, I can’t get that movement in the program as the cursor is at the edge of the screen.
The MouseState class includes only the position of the mouse cursor, among other things. This position will, as you mentioned, clip to the edges of the screen. There is no way to determine whether the user moved the mouse other than through the MouseState class. In particular, there is no MouseDirection, MouseTangent, or MouseAngle property that indicates which direction the mouse was moved.
As a workaround, you can call SetPosition to force the cursor at the middle of the screen, so you can always know which direction the mouse has moved. In fact, SetPosition even recommends this approach: