Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pitch support to Camera & OrthographicCamera #907

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions source/MonoGame.Extended/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public abstract class Camera<T> where T : struct
public abstract float Zoom { get; set; }
public abstract float MinimumZoom { get; set; }
public abstract float MaximumZoom { get; set; }
public abstract float Pitch { get; set; }
public abstract float MinimumPitch { get; set; }
public abstract float MaximumPitch { get; set; }
public abstract RectangleF BoundingRectangle { get; }
public abstract T Origin { get; set; }
public abstract T Center { get; }
Expand All @@ -17,6 +20,8 @@ public abstract class Camera<T> where T : struct
public abstract void Rotate(float deltaRadians);
public abstract void ZoomIn(float deltaZoom);
public abstract void ZoomOut(float deltaZoom);
public abstract void PitchUp(float deltaZoom);
public abstract void PitchDown(float deltaZoom);
public abstract void LookAt(T position);

public abstract T WorldToScreen(T worldPosition);
Expand Down
66 changes: 65 additions & 1 deletion source/MonoGame.Extended/OrthographicCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public sealed class OrthographicCamera : Camera<Vector2>, IMovable, IRotatable
private float _maximumZoom = float.MaxValue;
private float _minimumZoom;
private float _zoom;
private float _pitch;
private float _maximumPitch = float.MaxValue;
private float _minimumPitch;

public OrthographicCamera(GraphicsDevice graphicsDevice)
: this(new DefaultViewportAdapter(graphicsDevice))
Expand All @@ -23,6 +26,7 @@ public OrthographicCamera(ViewportAdapter viewportAdapter)

Rotation = 0;
Zoom = 1;
Pitch = 1;
Origin = new Vector2(viewportAdapter.VirtualWidth/2f, viewportAdapter.VirtualHeight/2f);
Position = Vector2.Zero;
}
Expand Down Expand Up @@ -74,6 +78,48 @@ public override float MaximumZoom
}
}

public override float Pitch
{
get => _pitch;
set
{
if ((value < MinimumPitch) || (value > MaximumPitch))
throw new ArgumentException("Pitch must be between MinimumPitch and MaximumPitch");

_pitch = value;
}
}

public override float MinimumPitch
{
get => _minimumPitch;
set
{
if (value < 0)
throw new ArgumentException("MinimumPitch must be greater than zero");

if (Pitch < value)
Pitch = MinimumPitch;

_minimumPitch = value;
}
}

public override float MaximumPitch
{
get => _maximumPitch;
set
{
if (value < 0)
throw new ArgumentException("MaximumPitch must be greater than zero");

if (Pitch > value)
Pitch = value;

_maximumPitch = value;
}
}

public override RectangleF BoundingRectangle
{
get
Expand Down Expand Up @@ -116,6 +162,24 @@ private void ClampZoom(float value)
Zoom = value > MaximumZoom ? MaximumZoom : value;
}

public override void PitchUp(float deltaPitch)
{
ClampPitch(Pitch + deltaPitch);
}

public override void PitchDown(float deltaPitch)
{
ClampPitch(Pitch - deltaPitch);
}

private void ClampPitch(float value)
{
if (value < MinimumPitch)
Pitch = MinimumPitch;
else
Pitch = value > MaximumPitch ? MaximumPitch : value;
}

public override void LookAt(Vector2 position)
{
Position = position - new Vector2(_viewportAdapter.VirtualWidth/2f, _viewportAdapter.VirtualHeight/2f);
Expand Down Expand Up @@ -155,7 +219,7 @@ private Matrix GetVirtualViewMatrix(Vector2 parallaxFactor)
Matrix.CreateTranslation(new Vector3(-Position*parallaxFactor, 0.0f))*
Matrix.CreateTranslation(new Vector3(-Origin, 0.0f))*
Matrix.CreateRotationZ(Rotation)*
Matrix.CreateScale(Zoom, Zoom, 1)*
Matrix.CreateScale(Zoom, Zoom * Pitch, 1)*
Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
}

Expand Down
Loading