XNA 2d camera with arbitrary zoom center

Posted by blooop on Stack Overflow See other posts from Stack Overflow or by blooop
Published on 2012-02-19T11:47:16Z Indexed on 2012/10/31 17:00 UTC
Read the original article Hit count: 214

Filed under:
|
|
|

I have a working 2D camera in XNA with these guts:

ms = Mouse.GetState();
msv = new Vector2(ms.X, ms.Y);  //screenspace mouse vecor
pos = new Vector2(0, 0); //camera center of view
zoom_center = cursor; //I would like to be able to define the zoom center in world coords
offset = new Vector2(scrnwidth / 2, scrnheight / 2);
transmatrix = Matrix.CreateTranslation(-pos.X, -pos.Y, 0)
    * Matrix.CreateScale(scale, scale, 1)
    * Matrix.CreateTranslation(offset.X, offset.Y, 0);
inverse = Matrix.Invert(transmatrix);
cursor = Vector2.Transform(msv, inverse);  //the mouse position in world coords      

I can move the camera position around and change the zoom level (with other code that I have not pasted here for brevity). The camera always zooms around the center of the screen, but I would like to be able to zoom about an arbitrary zoom point (the cursor in this case), like the indie game dyson http://www.youtube.com/watch?v=YiwjjCMqnpg&feature=player_detailpage#t=144s

I have tried all the combinations that make sense to me, but am completely stuck.

© Stack Overflow or respective owner

Related posts about XNA

Related posts about camera