# Unity3d - zooming with orthogonal camera like in Google maps

I am implementing 2d orthogonal zoom.

As far as I know - in Unity3d there are no translations like in OpenGL. Taking this into consideration, I work with :

• camera size
• camera position (center)
• real world mouse position

I have no issues implementing simple zoom in/ zoom out operations, combined with camera position changes ( drag'n'drop )

But I have some issues calculating camera zoom shift in this case :

• camera size = (4,4)
• camera position = (0,0)
• real world mouse position = (1,0)

A double zoom in (2x) is applied, as a result -

• camera size = (2,2)
• camera position = (0.5,0)
• real world mouse position = (1,0)

User shifts real mouse position to (1.5,0) and applies a double zoom out ( 0.5 ). In this case :

• camera size is again (4,4)
• camera position = ???????
• real world mouse position = (1.5,0)

What should be the camera shift so the mouse doesn't visually move ? What is the formula for calculation in this case ?

P.S. Any suggestion regarding current method of handling zoom are welcome

Update

So - I resolved this issue with Blue's help. The basic idea about shifting is right. The main reason it didn't work was incorrect zoom factor.

Example :

Initial camera size is 5. User zooms in by 1, camera size is now 4 ( it displays less content on the same 'screen'). User changes mouse position and zooms out by 1. Camera size is 5 again, but the amount of zoom is actually different :

• initially the change was 20% ( 1/5 )
• in the second time, the change was 25% ( 1/4 )

My fault was that, when zooming out, I tried to restore position by 20%, not 25%.

In this case you need the difference between your camera center and your real world mouse point. You can then apply your scale factor to this and offset your camera to make it "zoom" on the mouse position.

Semi-pseudo code:

float scaleFactor;
Vector2 mousePosition;
Vector2 cameraCenter;

Vector2 offset = (mousePosition - cameraCenter) / scaleFactor;
cameraCenter += offset;


If you are scaling a percentage this would be relative to the current zoom level. so it would be 0.2, then 0.16 and so on. This makes the mouse pointer be the focus point no matter the scale factor.

• This code doesn't seem to handle incremental zooming on the same position : for example, the camera is (1,0), the mouse is (2,0). The zooming for +20%, +40% ... doesn't give the constant value 0.2, for which the camera should be shifted in this case. Apr 24, 2014 at 18:36
• If you are scaling a percentage this would be relative to the current zoom level. so it would be 0.2, then 0.16 and so on. This makes the mouse pointer be the focus point no matter the scale factor. Apr 25, 2014 at 9:22
• Thank you! Small note - your answer without your comment doesn't help a lot. Apr 26, 2014 at 19:35