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 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

share|improve this question
    
You mention a camera. How are you implementing this? –  RobCurr Mar 1 '13 at 19:44
    
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. –  Tucker Morgan Mar 1 '13 at 19:56

1 Answer 1

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

share|improve this answer
    
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. –  Tucker Morgan Mar 1 '13 at 20:07
    
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. –  SpartanDonut Mar 1 '13 at 20:18
    
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 –  Tucker Morgan Mar 1 '13 at 20:41
    
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 –  SpartanDonut Mar 1 '13 at 20:46
    
Great illustration! –  user1306322 Mar 1 '13 at 23:34

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.