Capturing the mouse

Hi,

I control my model with mouse. The problem is that when the mouse reaches the bottom of the screen, the model stops moving. How can I capture the mouse to make the rotation infinite (while I move the mouse to one direction, the model keeps rotating)

Here is the code of the current mouse control:

MouseState mouseState = Mouse.GetState();
x = mouseState.X;
y = mouseState.Y;

if (y > previous_y)
            {
                modelRotationPitch += 0.01f;
                previous_y = y;
            }
else if (y < previous_y)
            {
                modelRotationPitch -= 0.01f;
                previous_y = y;
            }



Answer this question

Capturing the mouse

  • Michael Chancey

    Hi,

    Thanks for all the hints, they were very useful. I mushed together the solutions and created the one I needed.


  • ooper

    I found the same in another language, when using a mouse for a powerbar, basically, I moved the mouse to the centre of the screen, say 320,240, then in the loop, checked where the mouse was, if it was higher/lower, than 320 or 240, I knew the direction the mouse was moving.

    At the end of the loop, I would re-set the mouse to 320,240 again.

    Here is an example of it in my old language, Blitz+, which should be easy to figure out:-

    http://www.syntaxbomb.com/forum/index.php topic=303.0

    I would probably do the same in XNA!

    - Dabz



  • Alejandro.avenger

    Do you mean when the mouse reaches the edge of your playing area you want it to keep on moving Just adding in a check to see if that is a true condition and if so, rotate in the direction you want.

    So if the mouseState.Y is greater than or equal to the bounds of the playing area, increase your pictch and if it's less than or equal to the lower bound of your playing area (0) then decrease your pitch.

    Then as long as that condition is true, the model will continue to rotate.

    I hope that's what you were asking.


  • Sumit Chawla

    In your game initialization:

    Mouse.WindowHandle = this.Window.Handle;

    In your Update loop:

    bool active = IsActive;

    #if !XBOX360
    if (active)
    {
    MouseState ms = Mouse.GetState();
    int cx = Window.ClientBounds.Width / 2;
    int cy = Window.ClientBounds.Height / 2;
    if (ms.X != cx)
    {
    simInput_.RightX = Clamp(32 * (ms.X - cx) / (float)cx, -1, 1);
    }
    if (ms.Y != cy)
    {
    simInput_.RightY = Clamp(32 * (ms.Y - cy) / (float)cy, -1, 1);
    }
    Mouse.SetPosition(cx, cy);
    }
    #endif

    "Clamp" is just my function that clamps the input value in a given range, to avoid too large control outputs. You can do whatever you want with "ms.X - cx" and "ms.Y - cy".

    Another improvement is to ignore the first frame after you've gone active from being inactive, as there will be a jump based on where the cursor is when you re-gain active-ness.



  • Capturing the mouse