I have written a Custom mouse, with a Custom sprite, using MouseState, and getting the X and Y variables. However i also have a camera that i wrote, and this allows me to leave the initial box. One sprite that can freely move this area, but my mouse gets stuck in a box that seems proportional to my screen resolution some times.

  //This is where i create the Texture, Vector and MouseState of the Custom Cursor 
Texture2D Mouse_texture;
MouseState mouse_state;    
Vector2 Mouse_pos;

  // This is my Resolution and i have IsFullScreen = true
graphics.PreferredBackBufferWidth = 1600;
graphics.PreferredBackBufferHeight = 900;

  // Loading the Texture
Mouse_texture = Content.Load<Texture2D>("mouse"); 

  // Getting the current mouse State this is in the update function 
mouse_state = Mouse.GetState();

  // How i set the mouse position Simple Vector2 stuff and i never do anything else
  //   to handle the the position of the mouse 
Mouse_pos = new Vector2(mouse_state.X, mouse_state.Y);

  // and this is me Drawing it
spriteBatch.Draw(Mouse_texture, Mouse_pos, Color.White);

If you need any more code let me know

  • \$\begingroup\$ You mention a camera. How are you implementing this? \$\endgroup\$ – RobCurr Mar 1 '13 at 19:44
  • \$\begingroup\$ I have a class, which i create an instance of and control its movement with this cam.Pos = camera_movement; and that is controlled by player input at the moment. \$\endgroup\$ – Tucker Morgan Mar 1 '13 at 19:56

This sounds like an issue of not translating mouse coordinates / screen coordinates into world coordinates.

The easiest way you could solve this that also comes with its own limitations is to calculate your mouse draw position based on the camera position and the mouse state. Something like:

Mouse_pos = new Vector2(mouse_state.X + Camera.Position.X, mouse_state.Y + Camera.Position.Y);

The exact code for your implementation will likely be slightly different but you should be able to get the gist of it from this snippet.

There are much more versatile solutions than this which include projecting screen coordinates into world coordinates and get away from using pixels as the units for your world altogether. This is going to be a much more elegant solution that you may want to look into, and perhaps someone else could post an answer on, but to keep things simple you can definitely start with what I've shown above.

This solution should also solve the problem if you are shifting the camera negatively along the axis instead of positively.

Here's an image to help better illustrate the problem and the solution: Mouse Coordinates

  • \$\begingroup\$ I change the place where my camera was started, and it started to sync up with my mouse position, however the cursor still cannot leave a certain box, which i am not sure if i that has to do with my resolution. Would i have to shift, i don't know how to put it, i guess screens X and y position. How would you do that because certain things i can with my camera is zoom in and out, and move. \$\endgroup\$ – Tucker Morgan Mar 1 '13 at 20:07
  • \$\begingroup\$ Mouse_pos = new Vector2(mouse_state.X, mouse_state.Y); - this I believe is your mouse position in screen coordinates which is why you are seeing it contained to your screen's resolution. If your world is all based on pixel coordinates and your camera is located at X,Y = 500,300 then you would want your mouse coordinates to be mouse_state.X + 500 and mouse_state.Y + 300. \$\endgroup\$ – SpartanDonut Mar 1 '13 at 20:18
  • \$\begingroup\$ I got the camera to now start at 0,0, and adding a static value to the Mouse_pos Vector2 point would not do anything, and it offsets the mouse. Using the Vanilla Mouse_pos = new Vector2(mouse_state.X,mouse_state.Y); i was able to get the mouse to match the cursor on screen, the problem is that i guess the mouse is bound to have to stay within the resolution starting at 0,0, and the only time i was able to get it to not follow that was when i turned fullscreen off \$\endgroup\$ – Tucker Morgan Mar 1 '13 at 20:41
  • \$\begingroup\$ It's possible you aren't explaining the problem quite right because I don't see why that wouldn't work. Also you do not want add a static value to your mouse position you want to add the cameras position. This is a dynamic value and so in your update method which is where you said you update the mouses position, you'll want to get the mouse state and add the camera position to that. If you want to discuss in chat let's do that - chat.stackexchange.com/rooms/19/game-development \$\endgroup\$ – SpartanDonut Mar 1 '13 at 20:46
  • \$\begingroup\$ Great illustration! \$\endgroup\$ – user1306322 Mar 1 '13 at 23:34

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.