Skip to content
Closed
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
63 changes: 54 additions & 9 deletions Source/MonoGame.Extended/Camera2D.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.ViewportAdapters;
Expand All @@ -23,6 +23,7 @@ public Camera2D(ViewportAdapter viewportAdapter)

Rotation = 0;
Zoom = 1;
zPosition = 1;
Origin = new Vector2(viewportAdapter.VirtualWidth/2f, viewportAdapter.VirtualHeight/2f);
Position = Vector2.Zero;
}
Expand Down Expand Up @@ -70,7 +71,7 @@ public float MaximumZoom
_maximumZoom = value;
}
}

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

public void DollyIn(float deltaDolly)
{
zPosition -= deltaDolly;
}

public void DollyOut(float deltaDolly)
{
zPosition += deltaDolly;
}

/// <summary>
/// Outputs a Z dolly value.
/// Let's say you want a layer with a Z Position of 2 to be drawn at a scale of 0.5.
/// You'd call this function with 0.5 as the zoom parameter and 2 as the zOffset parameter:
///
/// GetZPositionFromZoom(0.5, 2);
///
/// The result would be 4. In other words, you'd have to set the camera's zPosition to 4.
/// <seealso cref="GetZoomFromZPosition(float, float)"/>
/// </summary>
public float GetZPositionFromZoom(float zoom, float zOffset)
{
return (1 / (zoom)) + zOffset;
}

/// <summary>
/// Outputs a zoom value.
/// This is the sister function to GetZPositionFromZoom.
/// Finds a layer's zoom value relative to an other layer.
/// <seealso cref="GetZPositionFromZoom(float, float)"/>
/// </summary>
public float GetZoomFromZPosition(float zPosition, float zOffset)
{
//Might be a good idea to return 0 in case of a division by 0.
return (1 / (zPosition - zOffset));
}
public void LookAt(Vector2 position)
{
Position = position - new Vector2(_viewportAdapter.VirtualWidth/2f, _viewportAdapter.VirtualHeight/2f);
Expand Down Expand Up @@ -144,29 +181,37 @@ public Vector2 ScreenToWorld(Vector2 screenPosition)
Matrix.Invert(GetViewMatrix()));
}

public Matrix GetViewMatrix(Vector2 parallaxFactor)
public bool IsVisible(float zOffset)
{
float zoom = GetZoomFromZPosition(zPosition, zOffset);
//10 Mean we can only get within 0.1 Z distance from a layer before we are considered behind it.
return zoom > 0 && zoom < 10;
}

public Matrix GetViewMatrix(float zOffset)
{
return GetVirtualViewMatrix(parallaxFactor)*_viewportAdapter.GetScaleMatrix();
return GetVirtualViewMatrix(zOffset) * _viewportAdapter.GetScaleMatrix();
}

private Matrix GetVirtualViewMatrix(Vector2 parallaxFactor)
private Matrix GetVirtualViewMatrix(float zOffset)
{
return
Matrix.CreateTranslation(new Vector3(-Position*parallaxFactor, 0.0f))*
Matrix.CreateTranslation(new Vector3(-Position, 0.0f))*
Matrix.CreateTranslation(new Vector3(-Origin, 0.0f))*
Matrix.CreateRotationZ(Rotation)*
Matrix.CreateScale(Zoom, Zoom, 1)*
Matrix.CreateScale(GetZoomFromZPosition(zPosition, zOffset), GetZoomFromZPosition(zPosition, zOffset), 1)*
Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
}

private Matrix GetVirtualViewMatrix()
{
return GetVirtualViewMatrix(Vector2.One);
return GetVirtualViewMatrix(0);
}

public Matrix GetViewMatrix()
{
return GetViewMatrix(Vector2.One);
return GetViewMatrix(0);
}

public Matrix GetInverseViewMatrix()
Expand Down Expand Up @@ -207,4 +252,4 @@ public ContainmentType Contains(Rectangle rectangle)
return GetBoundingFrustum().Contains(boundingBox);
}
}
}
}