Question

I'm working on a top down Strategy/RPG/Tower Defense XNA Game for Windows, and I'm running into an issue while scaling.

My display mode is 1680 X 1050, and that is what I'm setting the PreferredBackBuffer width and height to.

What I'm finding is that when I use my camera object to zoom in and out (changing the scale on a matrix that is passed to the SpriteBatch.Begin method, it changes the scale of my sprites... but I'm still confined to the BackBuffer (0,0,1680,1050)...

Can I use something like a RenderTarget to extend the actual area that is accessible when zooming/panning across the "map"?

I'm looking at making it something like 5000 X 3125...

Any thoughts/examples of how I should approach this?

I've added a video of what I'm experiencing... http://www.youtube.com/watch?v=jIiAeXbAnec

For the record I have also posted this question on Stack Exchange - Game Development

Was it helpful?

Solution

If I understand you correctly, the coordinates you are getting from Mouse.GetState() always fall within the bounds of the screen (ie: 0 < x < 1680 and 0 < y < 1050).

This is normal.

The transformation matrix you are using is taking coordinates in "world space" and putting them in "client space" (which is the space that SpriteBatch then uses to project sprites onto the screen).

Mouse.GetState() also returns coordinates in client space. What you want is to find what position those client coordinates represent in world space. So what you have to do is reverse the transformation. Here is some (untested) code that shows you how:

MouseState mouseState = Mouse.GetState();
Matrix transform = whatever;
Matrix inverseTransform = Matrix.Invert(transform);
Vector2 clientMouse = new Vector2(mouseState.X, mouseState.Y);
Vector2 worldMouse = Vector2.Transform(clientMouse, inverseTransform);

(Note: If you get NaN errors, make sure you're not scaling your Z coordinate down to zero, see here.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top