Added 3d camera and created 2d camera from it.
This commit is contained in:
parent
e1e2bbb3d7
commit
e0858ad85d
91
RecrownedAthenaeum/Camera/Camera.cs
Normal file
91
RecrownedAthenaeum/Camera/Camera.cs
Normal file
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A generic 3D camera.
|
||||||
|
/// </summary>
|
||||||
|
public class Camera
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Current position in the world.
|
||||||
|
/// </summary>
|
||||||
|
public Vector3 position;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The place the 3D camera is looking at.
|
||||||
|
/// </summary>
|
||||||
|
public Vector3 lookAt;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The direction up is for the 3D camera.
|
||||||
|
/// </summary>
|
||||||
|
public Vector3 upDirection;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The transform matrix representing the world (rotation and translations of the original world).
|
||||||
|
/// </summary>
|
||||||
|
public Matrix worldMatrix;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The view matrix that describes where the camera looks.
|
||||||
|
/// </summary>
|
||||||
|
public Matrix ViewMatrix { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The projection matrix.
|
||||||
|
/// </summary>
|
||||||
|
public Matrix projectionMatrix;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The final transformation matrix.
|
||||||
|
/// </summary>
|
||||||
|
public Matrix TransformationMatrix { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The graphics device used
|
||||||
|
/// </summary>
|
||||||
|
protected GraphicsDevice graphicsDevice;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs 3D camera with an orthographic projection matrix with dimensions of graphics devices viewport. All changes to matrices should have apply called after changes.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="graphicsDevice">The graphics device to use. Will use graphics device from <see cref="Configuration"/>'s graphics device manager if this is null which it is by default.</param>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Applies the changes to the fields and properties of the camera.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void Apply()
|
||||||
|
{
|
||||||
|
ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
|
||||||
|
TransformationMatrix = worldMatrix * ViewMatrix * projectionMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Centers the camera to middle of width and height of game window.
|
||||||
|
/// </summary>
|
||||||
|
public void Center()
|
||||||
|
{
|
||||||
|
position.X = this.graphicsDevice.Viewport.Width * 0.5f;
|
||||||
|
position.Y = this.graphicsDevice.Viewport.Height * 0.5f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,50 +5,22 @@ using System;
|
|||||||
namespace RecrownedAthenaeum.Camera
|
namespace RecrownedAthenaeum.Camera
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A virtual 2D camera.
|
/// A virtual 2D camera that wraps the normal <see cref="Camera"/>. Default projection is orthographic.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Camera2D
|
public class Camera2D : Camera
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Current zoom level.
|
|
||||||
/// </summary>
|
|
||||||
public float Zoom;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Current position in the world.
|
/// The 2D position.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Vector2 Position;
|
public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The matrix representing the zoom and position.
|
/// A 2D camera from the generic <see cref="Camera"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Matrix Matrix { get; private set; }
|
public Camera2D() : base()
|
||||||
|
|
||||||
private GraphicsDevice graphicsDevice;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructs 2D camera.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="graphicsDevice">The graphics device to use. Will use graphics device from <see cref="Configuration"/>'s graphics device manager if this is null which it is by default.</param>
|
|
||||||
public Camera2D(GraphicsDevice graphicsDevice = null)
|
|
||||||
{
|
{
|
||||||
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
|
|
||||||
|
|
||||||
Zoom = 1f;
|
|
||||||
Position.X = this.graphicsDevice.Viewport.Width * 0.5f;
|
|
||||||
Position.Y = this.graphicsDevice.Viewport.Height * 0.5f;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Applies any changes made to the properties of this camera and updates to new dimensions of viewport specified by the graphics device.
|
|
||||||
/// </summary>
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -65,6 +37,13 @@ namespace RecrownedAthenaeum.Camera
|
|||||||
distance *= (float)(1.0f - Math.Pow(1 - alpha, delta / 0.02f));
|
distance *= (float)(1.0f - Math.Pow(1 - alpha, delta / 0.02f));
|
||||||
|
|
||||||
Position += distance;
|
Position += distance;
|
||||||
|
|
||||||
|
Apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Matrix BasicOrthographic()
|
||||||
|
{
|
||||||
|
return Matrix.CreateOrthographic(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user