0

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.

2
  • What's the difference between movement and change in position? Please clarify your question! Do you mean "gesture"?
    – user703016
    Aug 8, 2011 at 10:44
  • Well, if there's no inbuilt method to do so, just create a little mouseMoveHandler class that does all the calculation for you.
    – Frosty840
    Aug 8, 2011 at 10:45

3 Answers 3

4

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:

When using this method to take relative input, such as in a first-person game, set the position of the mouse to the center of your game window each frame. This will allow you to read mouse movement on both axes with the greatest amount of granularity.

1
  • Thanks, that's the method I had planned on using. I just checking to see if there was a simpler or just nicer way to do it.
    – Matt
    Aug 8, 2011 at 12:12
0

My understanding of XNA is very limited, as game development isn't my main line of development.

That being said, for what I understand of the bejavior of XNA, you really need to calculate the variation of position between frames to see how much the mouse has moved.

With the variation in the X-Y coordinate, you can get all kind of data based on this simple measure.

1
  • 1
    Yes, but when the cursor has hit the far left or right of the screen you cannot get any change in the x coordinate with the method you're describing. The same happens with the y coordinate when the cursor hits the top or bottom of the screen.
    – Matt
    Aug 8, 2011 at 10:54
-3

If you want the acctual movement of the mouse you can just create a array or list of the mouse last movements that resets on a given event, ex. elapsedtime > 1f or IsMouseReleased. it would look something like this:

List<Vector2> Movement = new List<Vector2>();    

public override void Update(GameTime gameTime)
{
    MouseState pms;
    MouseState ms = Mouse.GetState();

    pms = ms;

    Movement.Add(pms.X - ms.X, pms.Y - ms.Y);

    if(ms.LeftButton == ButtonState.Released){ Movement.Clear(); }

    base.Update(gameTime);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.