Skip to content

Commit 5518977

Browse files
authored
Merge pull request #75 from DaveGreen-Games/collision-system-collider-enabled-check
Collision System Collider enabled checks added.
2 parents f93db9a + 05fba28 commit 5518977

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

ShapeEngine/Geometry/CollisionSystem/CollisionHandler.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ private void ProcessCollisions(float dt)
393393
{
394394
foreach (var candidate in bucket)
395395
{
396+
396397
if (candidate == collider) continue;
397398
if (candidate.Parent == null) continue;
398399
if (candidate.Parent == collider.Parent) continue;
@@ -471,13 +472,15 @@ private void ProcessCollisions(float dt)
471472
{
472473
foreach (var candidate in bucket)
473474
{
475+
//Only enabled colliders are added to the spatial hash
476+
//Therefore only enabled colliders are in each bucket!
474477
if (candidate == collider) continue;
475478
if (candidate.Parent == null) continue;
476479
if (candidate.Parent == collider.Parent) continue;
477480
if (!mask.Has(candidate.CollisionLayer)) continue;
478481
if (!collisionCandidateCheckRegister.Add(candidate)) continue;
479482

480-
bool overlap = collider.Overlap(candidate); // ShapeGeometry.Overlap(collider, candidate);
483+
bool overlap = collider.Overlap(candidate);
481484
if (overlap)
482485
{
483486
//multiple colliders can be involved with the same pair of collision objects, therefore we also have to check if the collision object pair was already added to the temp register.
@@ -575,14 +578,11 @@ private void Resolve()
575578

576579
#endregion
577580

578-
//TODO: Collider of the query object (collision object, list of colldiers, collider) are never checked if they are enabled at the beginning
579-
// Intersect Space and Cast Space region have this problem
580-
// Check query collider(s) right away before doing anything else!
581581
#region Intersect Space
582582
/// <summary>
583583
/// Performs intersection queries for all colliders of a <see cref="CollisionObject"/> against the collision system.
584584
/// </summary>
585-
/// <param name="colObject">The collision object whose colliders are used for intersection queries.</param>
585+
/// <param name="colObject">The collision object whose colliders are used for intersection queries</param>
586586
/// <param name="origin">The origin point for the intersection result.</param>
587587
/// <returns>An <see cref="IntersectSpaceResult"/> containing intersection data, or null if no intersections are found.</returns>
588588
/// <remarks>
@@ -646,14 +646,15 @@ private void Resolve()
646646
/// <summary>
647647
/// Performs an intersection query for a single <see cref="Collider"/> against the collision system.
648648
/// </summary>
649-
/// <param name="collider">The collider to test for intersections.</param>
649+
/// <param name="collider">The collider to test for intersections. The collider needs to be enabled!</param>
650650
/// <param name="origin">The origin point for the intersection result.</param>
651651
/// <returns>An <see cref="IntersectSpaceResult"/> containing intersection data, or null if no intersections are found.</returns>
652652
/// <remarks>
653653
/// Only enabled colliders are considered for intersection checks in this query.
654654
/// </remarks>
655655
public IntersectSpaceResult? IntersectSpace(Collider collider, Vector2 origin)
656656
{
657+
if(!collider.Enabled) return null;
657658
collisionCandidateBuckets.Clear();
658659
collisionCandidateCheckRegister.Clear();
659660

@@ -1199,7 +1200,7 @@ private void Resolve()
11991200
/// <summary>
12001201
/// Performs an overlap query for all colliders of a <see cref="CollisionObject"/> against the collision system and stores the results in the provided <see cref="CastSpaceResult"/>.
12011202
/// </summary>
1202-
/// <param name="collisionBody">The collision object whose colliders are used for the cast query.</param>
1203+
/// <param name="collisionBody">The collision object whose colliders are used for the cast query. Only enabled colliders from the collisionBody are used!</param>
12031204
/// <param name="result">A reference to the result object that will be populated with colliders that overlap.</param>
12041205
/// <remarks>
12051206
/// <list type="bullet">
@@ -1216,7 +1217,7 @@ public void CastSpace(CollisionObject collisionBody, ref CastSpaceResult result)
12161217
/// <summary>
12171218
/// Performs an overlap query for a list of <see cref="Collider"/> instances against the collision system and stores the results in the provided <see cref="CastSpaceResult"/>.
12181219
/// </summary>
1219-
/// <param name="colliders">The list of colliders to use for the cast query.</param>
1220+
/// <param name="colliders">The list of colliders to use for the cast query. Only enabled colliders are used!</param>
12201221
/// <param name="result">A reference to the result object that will be populated with colliders that overlap.</param>
12211222
/// <remarks>
12221223
/// <list type="bullet">
@@ -1232,6 +1233,8 @@ public void CastSpace(List<Collider> colliders, ref CastSpaceResult result)
12321233

12331234
foreach (var collider in colliders)
12341235
{
1236+
if(!collider.Enabled) continue;
1237+
12351238
collisionCandidateBuckets.Clear();
12361239
collisionCandidateCheckRegister.Clear();
12371240
spatialHash.GetCandidateBuckets(collider, ref collisionCandidateBuckets);
@@ -1258,7 +1261,7 @@ public void CastSpace(List<Collider> colliders, ref CastSpaceResult result)
12581261
/// <summary>
12591262
/// Performs an overlap query for a set of <see cref="Collider"/> instances against the collision system and stores the results in the provided <see cref="CastSpaceResult"/>.
12601263
/// </summary>
1261-
/// <param name="colliders">The set of colliders to use for the cast query.</param>
1264+
/// <param name="colliders">The set of colliders to use for the cast query. Only enabled colliders are used!</param>
12621265
/// <param name="result">A reference to the result object that will be populated with colliders that overlap.</param>
12631266
/// <remarks>
12641267
/// <list type="bullet">
@@ -1274,6 +1277,7 @@ public void CastSpace(HashSet<Collider> colliders, ref CastSpaceResult result)
12741277

12751278
foreach (var collider in colliders)
12761279
{
1280+
if(!collider.Enabled) continue;
12771281
collisionCandidateBuckets.Clear();
12781282
collisionCandidateCheckRegister.Clear();
12791283
spatialHash.GetCandidateBuckets(collider, ref collisionCandidateBuckets);
@@ -1300,16 +1304,17 @@ public void CastSpace(HashSet<Collider> colliders, ref CastSpaceResult result)
13001304
/// <summary>
13011305
/// Performs an overlap query for a single <see cref="Collider"/> against the collision system and stores the results in the provided <see cref="CastSpaceResult"/>.
13021306
/// </summary>
1303-
/// <param name="collider">The collider to use for the cast query.</param>
1307+
/// <param name="collider">The collider to use for the cast query. The collider needs to be enabled!</param>
13041308
/// <param name="result">A reference to the result object that will be populated with colliders that overlap.</param>
13051309
/// <remarks>
13061310
/// <list type="bullet">
13071311
/// <item>Only enabled colliders in the collision system are checked.</item>
13081312
/// <item>The result is cleared before being populated.</item>
13091313
/// </list>
13101314
/// </remarks>
1311-
public void CastSpace(Collider collider, ref CastSpaceResult result)
1315+
public void CastSpace(Collider collider, ref CastSpaceResult result)
13121316
{
1317+
if (!collider.Enabled) return;
13131318
if(result.Count > 0) result.Clear();
13141319
collisionCandidateBuckets.Clear();
13151320
collisionCandidateCheckRegister.Clear();
@@ -1618,7 +1623,7 @@ public void CastSpace(Polyline shape, BitFlag collisionMask, ref CastSpaceResult
16181623
/// <summary>
16191624
/// Performs an overlap query for all colliders of a <see cref="CollisionObject"/> against the collision system.
16201625
/// </summary>
1621-
/// <param name="collisionBody">The collision object whose colliders are used for the cast query.</param>
1626+
/// <param name="collisionBody">The collision object whose colliders are used for the cast query. Only enabled colliders from the collisionBody are used!</param>
16221627
/// <returns>The number of colliders in the system that overlap with any collider of the given <paramref name="collisionBody"/>.</returns>
16231628
/// <remarks>
16241629
/// Only enabled colliders of the <paramref name="collisionBody"/> are considered.
@@ -1631,6 +1636,8 @@ public int CastSpace(CollisionObject collisionBody)
16311636
int count = 0;
16321637
foreach (var collider in collisionBody.Colliders)
16331638
{
1639+
if(!collider.Enabled) continue;
1640+
16341641
collisionCandidateBuckets.Clear();
16351642
collisionCandidateCheckRegister.Clear();
16361643
spatialHash.GetCandidateBuckets(collider, ref collisionCandidateBuckets);
@@ -1658,13 +1665,14 @@ public int CastSpace(CollisionObject collisionBody)
16581665
/// <summary>
16591666
/// Performs an overlap query for a single <see cref="Collider"/> against the collision system.
16601667
/// </summary>
1661-
/// <param name="collider">The collider to use for the cast query.</param>
1668+
/// <param name="collider">The collider to use for the cast query. The collider needs to be enabled!</param>
16621669
/// <returns>The number of colliders in the system that overlap with the given <paramref name="collider"/>.</returns>
16631670
/// <remarks>
16641671
/// Only enabled colliders in the collision system are checked.
16651672
/// </remarks>
16661673
public int CastSpace(Collider collider)
16671674
{
1675+
if (!collider.Enabled) return 0;
16681676
collisionCandidateBuckets.Clear();
16691677
collisionCandidateCheckRegister.Clear();
16701678

ShapeEngine/Geometry/CollisionSystem/SpatialHash.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public void ChangeGrid(int rows, int cols)
204204
/// <param name="candidateBuckets">A list to populate with candidate buckets.</param>
205205
public void GetRegisteredCollisionCandidateBuckets(Collider collider, ref List<Bucket> candidateBuckets)
206206
{
207+
if (!collider.Enabled) return;
207208
if (!register.TryGetValue(collider, out var bucketIds)) return;
208209
if (bucketIds.Count <= 0) return;
209210
foreach (var id in bucketIds)
@@ -231,7 +232,7 @@ public void GetCandidateBuckets(CollisionObject collidable, ref List<Bucket> can
231232
/// <param name="candidateBuckets">A list to populate with candidate buckets.</param>
232233
public void GetCandidateBuckets(Collider collider, ref List<Bucket> candidateBuckets)
233234
{
234-
//TODO: Does not check if collider is enabled!
235+
if (!collider.Enabled) return;
235236
if (register.TryGetValue(collider, out var bucketIds))
236237
{
237238
if (bucketIds.Count <= 0) return;
@@ -244,7 +245,7 @@ public void GetCandidateBuckets(Collider collider, ref List<Bucket> candidateBuc
244245
return;
245246
}
246247
List<int> ids = new();
247-
GetCellIDs(collider, ref ids); //TODO: Does check if collider is enabled
248+
GetCellIDs(collider, ref ids);
248249
FillCandidateBuckets(ids, ref candidateBuckets);
249250
}
250251

@@ -376,6 +377,7 @@ public void GetUniqueCandidates(CollisionObject collisionBody, ref HashSet<Colli
376377
/// <param name="candidates">A set to populate with unique colliders.</param>
377378
public void GetUniqueCandidates(Collider collider, ref HashSet<Collider> candidates)
378379
{
380+
if (!collider.Enabled) return;
379381
if (register.TryGetValue(collider, out var bucketIds))
380382
{
381383
if (bucketIds.Count <= 0) return;
@@ -634,6 +636,7 @@ private void Add(CollisionObject collisionBody)
634636
/// <param name="collider">The collider to add.</param>
635637
private void Add(Collider collider)
636638
{
639+
// The SpatialHash is cleared and filled every frame, so skipping disabled colliders here is safe.
637640
if (!collider.Enabled) return;
638641

639642
List<int> ids;

0 commit comments

Comments
 (0)