From 27e0404ec0e9232874e173f2dda3f9405e3b6112 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 21 Apr 2022 16:04:40 -0700 Subject: [PATCH] expose Line properties + no-ID retrieve --- src/Collision/Shapes/Line.cs | 4 ++-- src/Collision/SpatialHash2D.cs | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Collision/Shapes/Line.cs b/src/Collision/Shapes/Line.cs index 5948704..261e27c 100644 --- a/src/Collision/Shapes/Line.cs +++ b/src/Collision/Shapes/Line.cs @@ -8,8 +8,8 @@ namespace MoonWorks.Collision /// public struct Line : IShape2D, System.IEquatable { - private Vector2 Start { get; } - private Vector2 End { get; } + public Vector2 Start { get; } + public Vector2 End { get; } public AABB2D AABB { get; } diff --git a/src/Collision/SpatialHash2D.cs b/src/Collision/SpatialHash2D.cs index 1ee82fc..beec12d 100644 --- a/src/Collision/SpatialHash2D.cs +++ b/src/Collision/SpatialHash2D.cs @@ -99,6 +99,43 @@ namespace MoonWorks.Collision FreeHashSet(returned); } + /// + /// Retrieves all the potential collisions of a shape-transform pair. + /// + public IEnumerable<(T, ICollidable, Transform2D, uint)> Retrieve(ICollidable shape, Transform2D transform2D, uint collisionMask = uint.MaxValue) + { + var returned = AcquireHashSet(); + + var box = shape.TransformedAABB(transform2D); + var (minX, minY) = Hash(box.Min); + var (maxX, maxY) = Hash(box.Max); + + if (minX < MinX) { minX = MinX; } + if (maxX > MaxX) { maxX = MaxX; } + if (minY < MinY) { minY = MinY; } + if (maxY > MaxY) { maxY = MaxY; } + + foreach (var key in Keys(minX, minY, maxX, maxY)) + { + if (hashDictionary.ContainsKey(key)) + { + foreach (var t in hashDictionary[key]) + { + if (!returned.Contains(t)) + { + var (otherShape, otherTransform, collisionGroups) = IDLookup[t]; + if (((collisionGroups & collisionMask) > 0) && AABB2D.TestOverlap(box, otherShape.TransformedAABB(otherTransform))) + { + returned.Add(t); + yield return (t, otherShape, otherTransform, collisionGroups); + } + } + } + } + } + + FreeHashSet(returned); + } /// /// Retrieves objects based on a pre-transformed AABB.