I trying to understand how to make a 2D Camera Matrix. The problem now is when I zoom the world shifts horizontally. The Camera need to zoom on the current Position.
public class MyCamera
{
public Vector2 Position;
public float Zoom;
public Matrix2D Transform = Matrix2D.identity;
public void Update()
{
Vector2 screen = new Vector2(Screen.width, Screen.height);
Transform = Matrix2D.Translate(-Position) *
Matrix2D.Scale(new Vector2(Zoom, Zoom)) *
Matrix2D.Translate(screen / 2);
}
}
public class MyGame
{
MyCamera Camera = new MyCamera();
Sprite TestImage;
float Zoom;
float ScrollSpeed = 0.2f;
public void Update()
{
Vector2 size = new Vector2(Screen.width, Screen.height);
Zoom += Input.mouseScrollDelta.y * ScrollSpeed;
Camera.Zoom = Zoom;
Camera.Position = Input.mousePosition;
Camera.Update();
//Update my sprite
TestImage.Position = Camera.Transform.MultiplyPoint(Vector2.zero);
TestImage.Size = Camera.Transform.MultiplyVector(new Vector2(512, 512));
}
}