Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Camera
- {
- public const float ZoomThreshold = 0.01f;
- public const float ZoomSpeed = 0.01f;
- public const float RotationSpeed = 2f;
- public const float MovementSpeed = 5f;
- public float ZoomLevel
- {
- get
- {
- return zoom;
- }
- set
- {
- zoom = Math.Max(value, Camera.ZoomThreshold);
- this.RecomputeMatrix();
- }
- }
- public float Rotation
- {
- get
- {
- return rotation;
- }
- set
- {
- rotation = value % 360;
- this.RecomputeMatrix();
- }
- }
- public Vector2 Position
- {
- get
- {
- return pos;
- }
- set
- {
- pos = value;
- this.RecomputeMatrix();
- }
- }
- public void Zoom(float amount = Camera.ZoomSpeed)
- {
- this.ZoomLevel = ZoomLevel + amount;
- }
- public void Rotate(float amount = Camera.RotationSpeed)
- {
- this.Rotation = Rotation + amount;
- }
- public void Move(Vector2? direction = null)
- {
- if (direction == null)
- direction = new Vector2(Camera.MovementSpeed);
- this.Position = this.Position - direction.Value;
- }
- private Vector2 pos = Vector2.Zero;
- private float rotation = 0f;
- private float zoom = 100f;
- public Matrix TransformMatrix { get; private set; }
- public Camera(Vector2 position, float rotation = 0f, float zoom = 1f)
- {
- Position = position;
- Rotation = rotation;
- ZoomLevel = zoom;
- }
- private void RecomputeMatrix()
- {
- Vector2 centre = new Vector2(Engine.VriskaEngine.GraphicsDevice.Viewport.Width / 2,
- Engine.VriskaEngine.GraphicsDevice.Viewport.Height / 2);
- Matrix rotationMatrix = Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation));
- //Translate to the centre
- Matrix positionTranslation = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));
- Matrix negPositionTranslation = Matrix.CreateTranslation(new Vector3(Position, 0));
- Matrix zoomMatrix = Matrix.CreateScale(new Vector3(zoom, zoom, 1));
- Matrix translateBackToPosition = Matrix.CreateTranslation(new Vector3(centre+Position, 0));
- Matrix centralZoom = zoomMatrix * translateBackToPosition;
- //TransformMatrix = translateBackToPosition * rotationMatrix * positionTranslation * zoomMatrix;
- TransformMatrix = rotationMatrix * zoomMatrix * translateBackToPosition;
- pos = Vector2.Transform(Vector2.Zero, TransformMatrix);
- }
- }
Add Comment
Please, Sign In to add comment