Camera2D now tested to work. Probably.

This commit is contained in:
Harrison Deng 2019-02-10 23:28:48 -06:00
parent 751bdd72e8
commit 56eb4e9e7f
2 changed files with 35 additions and 12 deletions

View File

@ -15,13 +15,30 @@ namespace RecrownedAthenaeum.Camera
/// </summary> /// </summary>
public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } } public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } }
/// <summary>
/// Places camera in the center given the corner position.
/// </summary>
public Vector2 CenterPosition { set { position.X = value.X + graphicsDevice.Viewport.Width / 2f; position.Y = value.Y + graphicsDevice.Viewport.Height / 2f; } }
/// <summary> /// <summary>
/// A 2D camera from the generic <see cref="Camera3D"/>. /// A 2D camera from the generic <see cref="Camera3D"/>.
/// </summary> /// </summary>
/// <param name="graphicsDevice">The graphics device to use if not using the one in <see cref="Configuration"/>.</param> /// <param name="graphicsDevice">The graphics device to use if not using the one in <see cref="Configuration"/>.</param>
public Camera2D(GraphicsDevice graphicsDevice = null) : base(graphicsDevice) public Camera2D(GraphicsDevice graphicsDevice = null) : base(graphicsDevice)
{ {
projectionMatrix = Matrix.CreateOrthographic(this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, 1);
CenterPosition = new Vector2(0, 0);
Apply();
}
/// <summary>
/// Applies for 2D.
/// Sets where the camera is looking for the view matrix to the position of the camera.
/// </summary>
public override void Apply()
{
lookAt = new Vector3(Position, 1f);
base.Apply();
} }
/// <summary> /// <summary>
@ -38,13 +55,18 @@ 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() /// <summary>
/// Moves the camera.
/// Apply needs to be called.
/// </summary>
/// <param name="move">Magnitude of how much to move per axis.</param>
public void MoveCamera(Vector2 move)
{ {
return Matrix.CreateOrthographic(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 1); Console.WriteLine(move);
Position += move;
Console.WriteLine(Position);
} }
} }
} }

View File

@ -64,8 +64,9 @@ namespace RecrownedAthenaeum.Camera
worldMatrix = Matrix.Identity; worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward; lookAt = Vector3.Forward;
upDirection = Vector3.Up; upDirection = Vector3.Up;
Center();
projectionMatrix = Matrix.CreateOrthographic(this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, 1); GraphicsDevice gDevice = this.graphicsDevice;
projectionMatrix = Matrix.Identity;
Apply(); Apply();
} }
@ -75,17 +76,17 @@ namespace RecrownedAthenaeum.Camera
public virtual void Apply() public virtual void Apply()
{ {
ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection); ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
TransformationMatrix = projectionMatrix * ViewMatrix * worldMatrix; TransformationMatrix = worldMatrix * ViewMatrix * projectionMatrix;
} }
/// <summary> /// <summary>
/// Centers the camera to middle of width and height of game window. /// Moves camera by the given amount.
/// </summary> /// </summary>
public void Center() /// <param name="move">A <see cref="Vector3"/> that contains how much in each direction to move.</param>
public void MoveCamera(Vector3 move)
{ {
position.Z = 0; position += move;
position.X = this.graphicsDevice.Viewport.Width * 0.5f; Apply();
position.Y = this.graphicsDevice.Viewport.Height * 0.5f;
} }
} }
} }