Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wrote this code to handle relative mouse movements,

        public override void Update(GameTime gt)
        {
            _lastmousestate = _mousestate;
            _mousestate = Mouse.GetState();
            if(LockMouse)
                Mouse.SetPosition(EntityGame.Viewport.Width/2,EntityGame.Viewport.Height/2);
            Point distance = new Point(_lastmousestate.X - _mousestate.X, _lastmousestate.Y - _mousestate.Y);
            Cursor.Position = new Point(Cursor.Position.X - distance.X, Cursor.Position.Y - distance.Y);
        }

Cursor is the cursor drawn to the screen to track the mouse. When running this code my mouse sticks to the center of the screen and won't move. However, the X,Y values change for a second before resetting back to the center of the screen.

Any ideas?

EDIT: Setting LockMouse to false causes the mouse and cursor to act perfectly fine although it is not bound to the window.

EDIT2: Here is the MouseHandler code, not much to see here just me setting it up so you can bind mouse buttons easier.

using System;
using EntityEngineV4.Engine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace EntityEngineV4.Input.MouseInput
{
    public class MouseHandler : Service
    {
        public CursorEntity CursorEntity;
        private static MouseState _mousestate;
        public static MouseState MouseState {
            get { return _mousestate;}
        }
        private static MouseState _lastmousestate;
        public static MouseState LastMouseState {
            get { return _lastmousestate;}
        }

        public bool LockMouse = false;

        public MouseHandler (EntityState stateref) : base(stateref)
        {
            CursorEntity = new CursorEntity(stateref, stateref, "CursorEntity", this);
            stateref.AddEntity(CursorEntity);
        }

        public override void Update(GameTime gt)
        {
            _lastmousestate = _mousestate;
            _mousestate = Mouse.GetState();
            if (_lastmousestate == _mousestate) return; //The mouse was the same, no need to do anything.
            if(LockMouse)
                Mouse.SetPosition(EntityGame.Viewport.Width/2,EntityGame.Viewport.Height/2);
            Point distance = new Point(_lastmousestate.X - _mousestate.X, _lastmousestate.Y - _mousestate.Y);
            CursorEntity.Position = new Point(CursorEntity.Position.X - distance.X, CursorEntity.Position.Y - distance.Y);
        }

        public override void Draw(SpriteBatch sb)
        {
        }

        public Point GetPositon()
        {
            return new Point(_mousestate.X, _mousestate.Y);
        }

        public void SetPositon(Point p)
        {
            Mouse.SetPosition(p.X, p.Y);
        }

        public void Flush()
        {
            _lastmousestate = _mousestate;
        }

        public bool IsMouseButtonDown(MouseButton mb)
        {
            switch(mb)
            {
            case MouseButton.LeftButton:
                return _mousestate.LeftButton == ButtonState.Pressed;
            case MouseButton.MiddleButton:
                return _mousestate.MiddleButton == ButtonState.Pressed;
            case MouseButton.RightButton:
                return _mousestate.RightButton == ButtonState.Pressed;
            case MouseButton.XButton1:
                return _mousestate.XButton1 == ButtonState.Pressed;
            case MouseButton.XButton2:
                return _mousestate.XButton2 == ButtonState.Pressed;
            }
            throw new Exception("MouseButton does not exist!");
        }

        public bool IsMouseButtonPressed (MouseButton mb)
        {
            switch (mb) 
            {
            case MouseButton.LeftButton:
                return _mousestate.LeftButton == ButtonState.Pressed && _lastmousestate.LeftButton == ButtonState.Released;
            case MouseButton.RightButton:
                return _mousestate.RightButton == ButtonState.Pressed && _lastmousestate.RightButton == ButtonState.Released;
            case MouseButton.MiddleButton:
                return _mousestate.MiddleButton == ButtonState.Pressed && _lastmousestate.MiddleButton == ButtonState.Released;
            case MouseButton.XButton1:
                return _mousestate.XButton1 == ButtonState.Pressed && _lastmousestate.XButton1 == ButtonState.Released;
            case MouseButton.XButton2:
                return _mousestate.XButton2 == ButtonState.Pressed && _lastmousestate.XButton2 == ButtonState.Released;
            }
            throw new Exception("MouseButton does not exist!");
        }

        public bool IsMouseButtonUp(MouseButton mb)
        {
            switch(mb)
            {
            case MouseButton.LeftButton:
                return _mousestate.LeftButton == ButtonState.Released;
            case MouseButton.MiddleButton:
                return _mousestate.MiddleButton == ButtonState.Released;
            case MouseButton.RightButton:
                return _mousestate.RightButton == ButtonState.Released;
            case MouseButton.XButton1:
                return _mousestate.XButton1 == ButtonState.Released;
            case MouseButton.XButton2:
                return _mousestate.XButton2 == ButtonState.Released;
            }
            throw new Exception("MouseButton does not exist!");
        }

        public bool IsMouseButtonReleased (MouseButton mb)
        {
            switch (mb) 
            {
            case MouseButton.LeftButton:
                return _mousestate.LeftButton == ButtonState.Released && _lastmousestate.LeftButton == ButtonState.Pressed;
            case MouseButton.RightButton:
                return _mousestate.RightButton == ButtonState.Released && _lastmousestate.RightButton == ButtonState.Pressed;
            case MouseButton.MiddleButton:
                return _mousestate.MiddleButton == ButtonState.Released && _lastmousestate.MiddleButton == ButtonState.Pressed;
            case MouseButton.XButton1:
                return _mousestate.XButton1 == ButtonState.Released && _lastmousestate.XButton1 == ButtonState.Pressed;
            case MouseButton.XButton2:
                return _mousestate.XButton2 == ButtonState.Released && _lastmousestate.XButton2 == ButtonState.Pressed;
            }
            throw new Exception("MouseButton does not exist!");
        }
    }

    public enum MouseButton
    {
        LeftButton, RightButton, MiddleButton, XButton1, XButton2
    }
}
share|improve this question
    
Have you debugged? What is the value of the boolean LockMouse? Are you sure it isn't evaluational to true when you don't want it to? –  Grey May 25 '13 at 19:30
    
It's true by default, and when I turn it off the mouse works perfect. it's just not bound to the screen. ;__; –  redcodefinal May 25 '13 at 19:37
    
When you say not bound to the screen... what exactly do you mean? You want the cursor to stay within the screen? –  Grey May 25 '13 at 19:49
    
I want it to stay in the center of the screen and each frame calculate it's distance from the original spot, and then move back. This way, the mouse always stays inside the window, and I can just add the distance it moved each frame to the cursor's position. –  redcodefinal May 25 '13 at 19:50
    
@CodeAssassin Here is an artcle explaining the idea -> en.sfml-dev.org/forums/index.php?topic=9370.0 –  redcodefinal May 25 '13 at 19:53

1 Answer 1

up vote 0 down vote accepted

Figured it out.

You have to ignore the last mouse state when the mouse is locked and just set it to the center of the screen like so.

            public override void Update(GameTime gt)
        {
            Flush();
            _mousestate = Mouse.GetState();

            if (LockMouse)
            {
                int lockx = EntityGame.Viewport.Width/2;
                int locky = EntityGame.Viewport.Height/2;

                //After we get our states, we can reset the Mouse with no problems!
                SetPosition(lockx, locky);
                Point distance = new Point(lockx - _mousestate.X, locky - _mousestate.Y);
                CursorEntity.Position = new Point(CursorEntity.Position.X - distance.X, CursorEntity.Position.Y - distance.Y);
            }
            else
            {
                //Calc our distance and add it to our cursor.
                Point distance = new Point(_lastmousestate.X - _mousestate.X, _lastmousestate.Y - _mousestate.Y);
                CursorEntity.Position = new Point(CursorEntity.Position.X - distance.X, CursorEntity.Position.Y - distance.Y);
            }
            CursorEntity.Update(gt);
        }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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