Guest User

Untitled

a guest
Oct 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. public class Camera
  2. {
  3. public const float ZoomThreshold = 0.01f;
  4.  
  5. public const float ZoomSpeed = 0.01f;
  6.  
  7. public const float RotationSpeed = 2f;
  8.  
  9. public const float MovementSpeed = 5f;
  10.  
  11. public float ZoomLevel
  12. {
  13. get
  14. {
  15. return zoom;
  16. }
  17.  
  18. set
  19. {
  20. zoom = Math.Max(value, Camera.ZoomThreshold);
  21. this.RecomputeMatrix();
  22. }
  23. }
  24.  
  25. public float Rotation
  26. {
  27. get
  28. {
  29. return rotation;
  30. }
  31.  
  32. set
  33. {
  34. rotation = value % 360;
  35. this.RecomputeMatrix();
  36. }
  37. }
  38.  
  39. public Vector2 Position
  40. {
  41. get
  42. {
  43. return pos;
  44. }
  45.  
  46. set
  47. {
  48. pos = value;
  49. this.RecomputeMatrix();
  50. }
  51. }
  52.  
  53. public void Zoom(float amount = Camera.ZoomSpeed)
  54. {
  55. this.ZoomLevel = ZoomLevel + amount;
  56. }
  57.  
  58. public void Rotate(float amount = Camera.RotationSpeed)
  59. {
  60. this.Rotation = Rotation + amount;
  61. }
  62.  
  63. public void Move(Vector2? direction = null)
  64. {
  65. if (direction == null)
  66. direction = new Vector2(Camera.MovementSpeed);
  67.  
  68. this.Position = this.Position - direction.Value;
  69. }
  70.  
  71. private Vector2 pos = Vector2.Zero;
  72.  
  73. private float rotation = 0f;
  74.  
  75. private float zoom = 100f;
  76.  
  77. public Matrix TransformMatrix { get; private set; }
  78.  
  79. public Camera(Vector2 position, float rotation = 0f, float zoom = 1f)
  80. {
  81. Position = position;
  82. Rotation = rotation;
  83. ZoomLevel = zoom;
  84. }
  85.  
  86. private void RecomputeMatrix()
  87. {
  88. Vector2 centre = new Vector2(Engine.VriskaEngine.GraphicsDevice.Viewport.Width / 2,
  89. Engine.VriskaEngine.GraphicsDevice.Viewport.Height / 2);
  90.  
  91. Matrix rotationMatrix = Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation));
  92.  
  93. //Translate to the centre
  94. Matrix positionTranslation = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));
  95.  
  96. Matrix negPositionTranslation = Matrix.CreateTranslation(new Vector3(Position, 0));
  97.  
  98. Matrix zoomMatrix = Matrix.CreateScale(new Vector3(zoom, zoom, 1));
  99.  
  100. Matrix translateBackToPosition = Matrix.CreateTranslation(new Vector3(centre+Position, 0));
  101.  
  102. Matrix centralZoom = zoomMatrix * translateBackToPosition;
  103.  
  104. //TransformMatrix = translateBackToPosition * rotationMatrix * positionTranslation * zoomMatrix;
  105. TransformMatrix = rotationMatrix * zoomMatrix * translateBackToPosition;
  106.  
  107. pos = Vector2.Transform(Vector2.Zero, TransformMatrix);
  108. }
  109. }
Add Comment
Please, Sign In to add comment