Skip to content

Improved collision filter test#6431

Open
eoineoineoin wants to merge 1 commit intospace-wizards:masterfrom
eoineoineoin:eoin/2026.02.28.collisionmasks
Open

Improved collision filter test#6431
eoineoineoin wants to merge 1 commit intospace-wizards:masterfrom
eoineoineoin:eoin/2026.02.28.collisionmasks

Conversation

@eoineoineoin
Copy link
Member

Minor improvements to collision filter test:

SharedPhysicsSystem was doing an O(n*m) test for every possible fixture pair when testing two FixturesComponents for collision. But because it's not doing any narrowphase collisions, the same result can be calculated in O(n+m) steps, just by summing up the collision layers/masks.

Added an interface to check IsHardCollidable() using just the fixtures components. I wanted this when checking collision with an EntityPrototype, so didn't have an Entity yet.

Copy link
Contributor

@moonheart08 moonheart08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tentative approval, I'm iffy on the idea of encouraging people to do physics checks without a real entity, but the logic looks fine. Please double-check your logic in-game if this code path doesn't have any tests.

@eoineoineoin
Copy link
Member Author

Tentative approval, I'm iffy on the idea of encouraging people to do physics checks without a real entity, but the logic looks fine. Please double-check your logic in-game if this code path doesn't have any tests.

Pretty confident that it's equivalent - have written a few physics engines in my time :). Robust.Shared.Tests/Physics is pretty light on tests, but happy to write some if you like?

The code before was effectively doing:

for every fixtureA in A
     for every fixtureB in B
           if fixtureA.Mask & fixtureB.Layer != 0 || fixtureA.Layer & fixtureB.Mask != 0
                        return true
return false

So I'm just transforming this to get rid of the n^2 loop, by combining all the layers/masks and testing them once:

allLayersA = A[0].Layer | A[1].Layer | A[2].Layer | ....
allMasksA = A[0].Mask | A[1].Mask | A[2].Mask | ....
allLayersB = B[0].Layer | B[1].Layer | B[2].Layer | ....
allMasksB = B[0].Mask | B[1].Mask | B[2].Mask | ....

if allLayersA & allMasksB != 0 || allMasksA & allLayersB != 0
      return true;
return false;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants