Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Zoom to world position

$
0
0

@David_Marsh wrote:

Since github is secure against code contributions I figured I would place this here. At least I can find it later.
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);
        }

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles