How can you alter a transformation matrix to zoom into a point?

Started by
6 comments, last by DekuTree64 5 years, 11 months ago

I control the camera in my program by changing the transformation matrix (which alters the other underlying matrices). Not this is all in 2D.

The transformation matrix is pretty standard. It is a 4x4 matrix with the diagonal acting as the scale factor (x, y, z) and the right side acting as the offset coordinates (this is ignoring shear and rotation which aren't that important to me though I would like the method to still work with rotation).

So essentially I am currently zooming by altering the x and y scale in the transformation matrix. This seems to act from the top left corner (which is the origin in my coordinate space). I would like the zoom to act towards the mouse. So knowing the x and y position of the mouse when I apply my transformation (every frame)  how should I alter the zoom so that it zooms towards whatever the mouse position was?

I can share code if you would like but I am writing this in Rust. Also let me know if you need more information I am happy to provide I just didn't want to over share.

Advertisement

It could have something to do with the origin, if it's not the 'wanted' center, you could either change the origin or use the position/translation part of the matrix to simulate the zoom effect (instead of scaling).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I can't change the origin it is too central to everything else in the engine. I generally want it to zoom to the mouse position. Which I can get each frame or during each "apply"

I'm not sure how you could use the translation part to simulate zooming?

 

Ok. Unless you want to Render both the zoomed in and not zoomed in scene, moving the camera closer to your target is the same as zooming.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Oh Okay I see what you mean. That actually won't work in my case. While the transformation matrix works like a general 3D one editing the z translation won't actually change anything about the view. 

Translation doesn't simulate zooming, it aligns zooming with the intended target. Changing screen space X and Y scaling (X to kX and Y to kY) is a formally correct zoom, but it zooms "around" X=0 and Y=0 (the point of the screen, but also the line in 3D, which remains unchanged).

Instead, if the mouse is located at Xm and Ym in screen space and you want the point under the mouse to remain in the same place, the transformed screen space coordinates should be k(X-Xm)+Xm and k(Y-Ym)+Ym.

Depending on the purpose, you might want to further modify the transform by moving the point under the mouse towards the center of the viewport instead; adjusting with rotations the world space to screen space transform that represents the "camera" in 3D to look at an appropriate mouse controlled point might be necessary (commonly done in FPS games, if you want examples).

Omae Wa Mou Shindeiru

The basic idea is to translate so the zoom center is at the origin, then scale, and then translate back.

But that should be equivalent to scaling and then translating by zoomCenter*scale-zoomCenter, or something like that.

This topic is closed to new replies.

Advertisement