• Advertisement
Sign in to follow this  

Best way to implement zooming...?

This topic is 1684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I pass matrices to my renderables, which they then modify to position themselves correctly according to their position.

 

To my TerrainView class,i also pass my camera matrix.

 

Now, since the terrainview needs to know how much of it is visible (to show the right amount of terrain), it has a separate "zoom" variable (2=2x magnification and so on)

 

However, i feel this doesnt make sense for lets say my sprites (i dont want a zoom value for each sprite ._.)

 

Although my game is 2D orthographic, i still use 4*4 matrices for the camera and rendering. So, to solve my above problem and not have each renderable have their own zoom value, i decided to utilize the Z component of the position of the camera matrix. In my renderables, i get my zoom value by doing 1/camMatrix.z (so for 2x magnification, camMatrix.z must be 0.5), which is somewhat logical since the world is zoomed in when the camera goes closer to the xy plane.

 

That is ugly though, because in my renderables i have to extract the Z coordinate of the camera to calculate the zoom and then scale the positioning matrix to zoom.

 

What would be a more elegant solution?

 

I was thinking of using perspective projection, which would give me proper zoom for the objects that dont need to know wether theyre visible or not and how much zoom there is (currently, of course ill need to cull stuff later...) but i dont know how to best extract the level of zoom from the matrix.

 

So, if i have the camera matrix and the projection matrix, i should somehow be able to find out the zoom (where 2=2x magnification). It only needs to work when the camera is looking down the z axis, and the objects lie on z=0 on the xy plane.

 

The zoom should be taken from the horizontal width of the screen and the horizontal scaling of the world (height of screen should be ignored)

 

Should i multiply the camera and projection matrix together, and what component(s) of the resulting matrix can i use to get the zoom (or the inverse of zoom)?

Share this post


Link to post
Share on other sites
Advertisement
I THINK if i extract the w component of the resulting matrixs last row i will get the zoom.

But i will try and make a general solution that works for any distance from the camera.

Share this post


Link to post
Share on other sites
since it's a 2D Game, you can modify your ortho matrix instead, for example:

Vectro3 CameraPos; //CameraPos.x/y = center of screen, z = zoom value.
float Width = Window.Width*0.5f/CameraPos.z;
float Height= Window.Height*0.5f/CameraPos.z;
Matrix4 Ortho = Matrix4.Ortho(CameraPos.x-Width, CameraPos.x+Width, CameraPos.y-Height, CameraPos.y+Height, 0.0f, 1.0f);
you'll probably want two ortho matrixs, one for the ui, and one for the game.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement