diff --git a/RecrownedAthenaeum/Camera/Camera.cs b/RecrownedAthenaeum/Camera/Camera.cs new file mode 100644 index 0000000..ee52a9a --- /dev/null +++ b/RecrownedAthenaeum/Camera/Camera.cs @@ -0,0 +1,91 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RecrownedAthenaeum.Camera +{ + /// + /// A generic 3D camera. + /// + public class Camera + { + /// + /// Current position in the world. + /// + public Vector3 position; + + /// + /// The place the 3D camera is looking at. + /// + public Vector3 lookAt; + + /// + /// The direction up is for the 3D camera. + /// + public Vector3 upDirection; + + /// + /// The transform matrix representing the world (rotation and translations of the original world). + /// + public Matrix worldMatrix; + + /// + /// The view matrix that describes where the camera looks. + /// + public Matrix ViewMatrix { get; private set; } + + /// + /// The projection matrix. + /// + public Matrix projectionMatrix; + + /// + /// The final transformation matrix. + /// + public Matrix TransformationMatrix { get; private set; } + + /// + /// The graphics device used + /// + protected GraphicsDevice graphicsDevice; + + /// + /// Constructs 3D camera with an orthographic projection matrix with dimensions of graphics devices viewport. All changes to matrices should have apply called after changes. + /// + /// The graphics device to use. Will use graphics device from 's graphics device manager if this is null which it is by default. + public Camera(GraphicsDevice graphicsDevice = null) + { + this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice); + + position.Z = 0; + worldMatrix = Matrix.Identity; + lookAt = Vector3.Forward; + upDirection = Vector3.Up; + Center(); + projectionMatrix = Matrix.CreateOrthographic(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 1); + Apply(); + } + + /// + /// Applies the changes to the fields and properties of the camera. + /// + public virtual void Apply() + { + ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection); + TransformationMatrix = worldMatrix * ViewMatrix * projectionMatrix; + } + + /// + /// Centers the camera to middle of width and height of game window. + /// + public void Center() + { + position.X = this.graphicsDevice.Viewport.Width * 0.5f; + position.Y = this.graphicsDevice.Viewport.Height * 0.5f; + } + } +} diff --git a/RecrownedAthenaeum/Camera/Camera2D.cs b/RecrownedAthenaeum/Camera/Camera2D.cs index 98a980e..b9f05cb 100644 --- a/RecrownedAthenaeum/Camera/Camera2D.cs +++ b/RecrownedAthenaeum/Camera/Camera2D.cs @@ -5,50 +5,22 @@ using System; namespace RecrownedAthenaeum.Camera { /// - /// A virtual 2D camera. + /// A virtual 2D camera that wraps the normal . Default projection is orthographic. /// - public class Camera2D + public class Camera2D : Camera { - /// - /// Current zoom level. - /// - public float Zoom; /// - /// Current position in the world. + /// The 2D position. /// - public Vector2 Position; + public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } } /// - /// The matrix representing the zoom and position. + /// A 2D camera from the generic . /// - public Matrix Matrix { get; private set; } - - private GraphicsDevice graphicsDevice; - - /// - /// Constructs 2D camera. - /// - /// The graphics device to use. Will use graphics device from 's graphics device manager if this is null which it is by default. - public Camera2D(GraphicsDevice graphicsDevice = null) + public Camera2D() : base() { - this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice); - Zoom = 1f; - Position.X = this.graphicsDevice.Viewport.Width * 0.5f; - Position.Y = this.graphicsDevice.Viewport.Height * 0.5f; - } - - /// - /// Applies any changes made to the properties of this camera and updates to new dimensions of viewport specified by the graphics device. - /// - public void Update() - { - Rectangle bounds = graphicsDevice.Viewport.Bounds; - Matrix = - Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) * - Matrix.CreateScale(Zoom) * - Matrix.CreateTranslation(new Vector3(bounds.Width * 0.5f, bounds.Height * 0.5f, 0f)); } /// @@ -65,6 +37,13 @@ namespace RecrownedAthenaeum.Camera distance *= (float)(1.0f - Math.Pow(1 - alpha, delta / 0.02f)); Position += distance; + + Apply(); + } + + private Matrix BasicOrthographic() + { + return Matrix.CreateOrthographic(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 1); } } }