Scaling camera from center

Hello everyone!

I have a camera class (below) that is attached to an object, and runs the “Update” method every frame. I need to scale the camera from the center to create a zooming in effect, but I don’t want to have to change the game’s resolution or render more things than I need to. Right now I have the below matrix being passed into the SpriteBatch’s transformationMatrix, with the pivot at Vec2(0.5, 0.5) and the scale at Vec2(1, 1), rotation at 0 and the position at Vec2(0, 0). But I can’t manage to scale the camera from the center, even though it does work for Spritebatch.Draw()

    public override void Update()
    {
        Matrix = Matrix.CreateTranslation(LinkedObject.Pivot.X, LinkedObject.Pivot.Y, 0) * Matrix.CreateScale(new Vector3(LinkedObject.Size.X, LinkedObject.Size.Y, 1)) * Matrix.CreateRotationZ(MathHelper.ToRadians(LinkedObject.Rotation));
        Matrix.Translation = -new Vector3(LinkedObject.Position.X - (Settings.Window.GameResolution.X * LinkedObject.Pivot.X), LinkedObject.Position.Y - (Settings.Window.GameResolution.Y * LinkedObject.Pivot.Y), 0);
    }

Any help would be appreciated!

Just at a glance, you’re multiplying your matrices in the wrong order. Do this:

Matrix = Scale * Rotation * Translation;

I’m also not 100% what you’re doing with Matrix.Translation = …

This should give you what you want:

private Matrix GetView() {
    int width = GraphicsDevice.Viewport.Width;
    int height = GraphicsDevice.Viewport.Height;
    Vector2 origin = new Vector2(width / 2f, height / 2f);

    return
        Matrix.CreateTranslation(-_xy.X, -_xy.Y, 0f) *
        Matrix.CreateRotationZ(_rotation) *
        Matrix.CreateScale(_scale, _scale, 1f) *
        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
}

Hey! That worked, thank you!

Awesome!