expose Line properties + no-ID retrieve

pull/19/head
cosmonaut 2022-04-21 16:04:40 -07:00
parent be4b5cf2c7
commit 27e0404ec0
2 changed files with 39 additions and 2 deletions

View File

@ -8,8 +8,8 @@ namespace MoonWorks.Collision
/// </summary>
public struct Line : IShape2D, System.IEquatable<Line>
{
private Vector2 Start { get; }
private Vector2 End { get; }
public Vector2 Start { get; }
public Vector2 End { get; }
public AABB2D AABB { get; }

View File

@ -99,6 +99,43 @@ namespace MoonWorks.Collision
FreeHashSet(returned);
}
/// <summary>
/// Retrieves all the potential collisions of a shape-transform pair.
/// </summary>
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);
}
/// <summary>
/// Retrieves objects based on a pre-transformed AABB.