From 7bfbbb2c4292d1b586c8ab52ba46385ec5102d8f Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Sun, 19 Jul 2015 20:00:32 -0400 Subject: [PATCH 1/6] Adding bounding frustum, projection matrix, inverse view matrix --- .../MonoGame.Extended/Camera2D.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs index 5f007c30..b96affbf 100644 --- a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs +++ b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs @@ -61,6 +61,31 @@ namespace MonoGame.Extended public Matrix GetViewMatrix() { return GetViewMatrix(Vector2.One); + } + + public Matrix GetInverseViewMatrix() + { + return Matrix.Invert(GetViewMatrix()); + } + + private Matrix GetProjectionMatrix(Matrix viewMatrix) + { + // Note: This projection matrix is the same one used inside of the MonoGame SpriteBatch by default. + Matrix projection = Matrix.CreateOrthographicOffCenter(0, _viewportAdapter.VirtualWidth, _viewportAdapter.VirtualHeight, 0, -1, 0); + + // Half pixel offset. + projection.M41 += -0.5f * projection.M11; + projection.M42 += -0.5f * projection.M22; + + Matrix.Multiply(ref viewMatrix, ref projection, out projection); + + return projection; + } + + public BoundingFrustum GetBoundingFrustum() + { + Matrix viewMatrix = GetViewMatrix(); + return new BoundingFrustum(viewMatrix * GetProjectionMatrix(viewMatrix)); } } } From 391f6f8b0f70a555084a327a046a2a2d36fded9f Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Sun, 19 Jul 2015 20:10:17 -0400 Subject: [PATCH 2/6] Adding Contains overloads --- .../MonoGame.Extended/Camera2D.cs | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs index b96affbf..90763199 100644 --- a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs +++ b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs @@ -85,7 +85,26 @@ namespace MonoGame.Extended public BoundingFrustum GetBoundingFrustum() { Matrix viewMatrix = GetViewMatrix(); - return new BoundingFrustum(viewMatrix * GetProjectionMatrix(viewMatrix)); - } + return new BoundingFrustum(viewMatrix * GetProjectionMatrix(viewMatrix)); + } + + #region Contains overloads + + public bool Contains(Point point) + { + return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; + } + + public bool Contains(Vector2 point) + { + return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; + } + + public bool Contains(Rectangle rectangle) + { + throw new System.NotImplementedException(); + } + + #endregion } } From 7d042216241c8e5c960fb45b6ced52c8f8b000af Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Sun, 19 Jul 2015 20:10:42 -0400 Subject: [PATCH 3/6] Removing region directives --- MonoGame.Extended/MonoGame.Extended/Camera2D.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs index 90763199..7c52c7f0 100644 --- a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs +++ b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs @@ -88,8 +88,6 @@ namespace MonoGame.Extended return new BoundingFrustum(viewMatrix * GetProjectionMatrix(viewMatrix)); } - #region Contains overloads - public bool Contains(Point point) { return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; @@ -104,7 +102,5 @@ namespace MonoGame.Extended { throw new System.NotImplementedException(); } - - #endregion } } From b47c49ab0ebd75f887cfce44e0aa3995fcbf5592 Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Sun, 19 Jul 2015 20:16:04 -0400 Subject: [PATCH 4/6] Adding Contains overload for rectangles --- MonoGame.Extended/MonoGame.Extended/Camera2D.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs index 7c52c7f0..f4a7ea2f 100644 --- a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs +++ b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs @@ -100,7 +100,7 @@ namespace MonoGame.Extended public bool Contains(Rectangle rectangle) { - throw new System.NotImplementedException(); + return GetBoundingFrustum().Contains(new BoundingBox(new Vector3(0, 0, 0), new Vector3(15, 15, 0))) != ContainmentType.Disjoint; } } } From a9744c1ca2c6cb9e3ec954f1a9390851e957f827 Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Sun, 19 Jul 2015 20:18:31 -0400 Subject: [PATCH 5/6] Adding ContainmentType overloads for Contains methods --- MonoGame.Extended/MonoGame.Extended/Camera2D.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs index f4a7ea2f..289c8d66 100644 --- a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs +++ b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs @@ -93,14 +93,29 @@ namespace MonoGame.Extended return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; } + public ContainmentType Contains(Point point) + { + return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)); + } + public bool Contains(Vector2 point) { return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; } + public ContainmentType Contains(Vector2 point) + { + return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)); + } + public bool Contains(Rectangle rectangle) { return GetBoundingFrustum().Contains(new BoundingBox(new Vector3(0, 0, 0), new Vector3(15, 15, 0))) != ContainmentType.Disjoint; } + + public ContainmentType Contains(Rectangle rectangle) + { + return GetBoundingFrustum().Contains(new BoundingBox(new Vector3(0, 0, 0), new Vector3(15, 15, 0))); + } } } From 5c6736e8b510d96195d997b68a76d2d2e964ffed Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Sun, 19 Jul 2015 20:19:05 -0400 Subject: [PATCH 6/6] Code organization --- MonoGame.Extended/MonoGame.Extended/Camera2D.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs index 289c8d66..70a47b3e 100644 --- a/MonoGame.Extended/MonoGame.Extended/Camera2D.cs +++ b/MonoGame.Extended/MonoGame.Extended/Camera2D.cs @@ -93,24 +93,24 @@ namespace MonoGame.Extended return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; } - public ContainmentType Contains(Point point) - { - return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)); - } - public bool Contains(Vector2 point) { return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)) != ContainmentType.Disjoint; } - public ContainmentType Contains(Vector2 point) + public bool Contains(Rectangle rectangle) + { + return GetBoundingFrustum().Contains(new BoundingBox(new Vector3(0, 0, 0), new Vector3(15, 15, 0))) != ContainmentType.Disjoint; + } + + public ContainmentType Contains(Point point) { return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)); } - public bool Contains(Rectangle rectangle) + public ContainmentType Contains(Vector2 point) { - return GetBoundingFrustum().Contains(new BoundingBox(new Vector3(0, 0, 0), new Vector3(15, 15, 0))) != ContainmentType.Disjoint; + return GetBoundingFrustum().Contains(new Vector3(point.X, point.Y, 0)); } public ContainmentType Contains(Rectangle rectangle)