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 ?
Thank you in advance.
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%.