From f0d3dfccf9d715ff785a8a09c80ec468f6e4d145 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 25 Mar 2022 15:11:38 -0700 Subject: [PATCH] add collision mask system to spatial hash --- src/Collision/SpatialHash2D.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Collision/SpatialHash2D.cs b/src/Collision/SpatialHash2D.cs index 49433877..581751dd 100644 --- a/src/Collision/SpatialHash2D.cs +++ b/src/Collision/SpatialHash2D.cs @@ -12,7 +12,7 @@ namespace MoonWorks.Collision private readonly int cellSize; private readonly Dictionary> hashDictionary = new Dictionary>(); - private readonly Dictionary IDLookup = new Dictionary(); + private readonly Dictionary IDLookup = new Dictionary(); public int MinX { get; private set; } = 0; public int MaxX { get; private set; } = 0; @@ -37,7 +37,8 @@ namespace MoonWorks.Collision /// A unique ID for the shape-transform pair. /// /// - public void Insert(T id, IHasAABB2D shape, Transform2D transform2D) + /// A bitmask value specifying the groups this object belongs to. + public void Insert(T id, IHasAABB2D shape, Transform2D transform2D, uint collisionGroups = uint.MaxValue) { var box = shape.TransformedAABB(transform2D); var minHash = Hash(box.Min); @@ -54,7 +55,7 @@ namespace MoonWorks.Collision } hashDictionary[key].Add(id); - IDLookup[id] = (shape, transform2D); + IDLookup[id] = (shape, transform2D, collisionGroups); } } @@ -67,7 +68,7 @@ namespace MoonWorks.Collision /// /// Retrieves all the potential collisions of a shape-transform pair. Excludes any shape-transforms with the given ID. /// - public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D transform2D) + public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D transform2D, uint collisionMask = uint.MaxValue) { var returned = AcquireHashSet(); @@ -91,8 +92,8 @@ namespace MoonWorks.Collision { if (!returned.Contains(t)) { - var (otherShape, otherTransform) = IDLookup[t]; - if (!id.Equals(t) && AABB2D.TestOverlap(box, otherShape.TransformedAABB(otherTransform))) + var (otherShape, otherTransform, collisionGroups) = IDLookup[t]; + if (!id.Equals(t) && ((collisionGroups & collisionMask) > 0) && AABB2D.TestOverlap(box, otherShape.TransformedAABB(otherTransform))) { returned.Add(t); yield return (t, otherShape, otherTransform); @@ -112,7 +113,7 @@ namespace MoonWorks.Collision /// /// A transformed AABB. /// - public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB2D aabb) + public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB2D aabb, uint collisionMask = uint.MaxValue) { var returned = AcquireHashSet(); @@ -135,8 +136,8 @@ namespace MoonWorks.Collision { if (!returned.Contains(t)) { - var (otherShape, otherTransform) = IDLookup[t]; - if (AABB2D.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform))) + var (otherShape, otherTransform, collisionGroups) = IDLookup[t]; + if (((collisionGroups & collisionMask) > 0) && AABB2D.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform))) { yield return (t, otherShape, otherTransform); }