Joshua Belden
Joshua Belden

Reputation: 10503

XNA - Global Zoom

I'm trying to allow zoom with the mouse scroll and pass a zoom value into each of my drawable objects. Each drawable object takes the zoom value and uses the scale parameter of the SpriteBatch.Draw method. Zooming each individual item though doesn't work in relation to all other objects. Is there some sort of canvas I can draw everything to, and then zoom that so everything zooms appropriately?

Upvotes: 1

Views: 1263

Answers (2)

Andrew Russell
Andrew Russell

Reputation: 27215

Use the SpriteBatch.Begin method that takes a Matrix parameter. Pass a scaling matrix:

Matrix scaleMatrix = Matrix.CreateScale(myScale);
sb.Begin(SpriteSortMode.Deferred, null, null, null, null, null, scaleMatrix);

You should use this parameter for all "global" transformations of your sprites. So you could combine (multiply) a translation and a scale matrix to move about and zoom your 2D scene.

Upvotes: 3

BlackBear
BlackBear

Reputation: 22979

In the camera, you have to re-create the view , changing the field of view to a smaller value:

Matrix.CreatePerspectiveFieldOfView(
    MathHelper.PiOver4,                    // change this to, say 20°
    aspectRatio,
    0.0001f,
    1000.0f,
    out projection
);

Now this change is global since you use camera's projection in your effects

Upvotes: 1

Related Questions