craftworkgames / MonoGame.Extended

Extensions to make MonoGame more awesome
http://www.monogameextended.net/
Other
1.44k stars 323 forks source link

New Zoom functions for the Camera class #625

Open craftworkgames opened 5 years ago

craftworkgames commented 5 years ago

This issue came from a post on the community forum

These are new zoom functions for the MonoGame.Extended OrthographicCamera class

    public void ZoomIn(float deltaZoom, Vector2 zoomCenter)
    {
        float pastZoom = Zoom;
        ClampZoom(Zoom + deltaZoom);
        Position += (zoomCenter - Origin - Position) * ((Zoom - pastZoom) / Zoom);
    }

    public void ZoomOut(float deltaZoom, Vector2 zoomCenter)
    {
        float pastZoom = Zoom;
        ClampZoom(Zoom - deltaZoom);
        Position += (zoomCenter - Origin - Position) * ((Zoom - pastZoom) / Zoom);
    }

They can be called like this.

        _worldPosition = _camera.ScreenToWorld(mouseState.Position.ToVector2());

        // zoom targeting the mouse
        int previousMouseWheelValue = currentMouseWheelValue;
        currentMouseWheelValue = Mouse.GetState().ScrollWheelValue;
        if (currentMouseWheelValue > previousMouseWheelValue)
        {
            _camera.ZoomIn(1 / 12f, _worldPosition);
        }
        if (currentMouseWheelValue < previousMouseWheelValue)
        {
            _camera.ZoomOut(1 / 12f, _worldPosition);
        }
lithiumtoast commented 4 years ago

This is pushed back to v4 as I want to modify more of the surrounding code in its entirety before making changes.