How does layer based collision affect performance

How does layer based collision affect performance

In Unity3D, and others, there are Layer Based Collision System which makes collisions only happen between specified Layers.
It defaults to all layers colliding with all others.
Does making use of these setting decrease performance, or increase performance?
I could imagine it decreases it if every potential collision had to be checked for it's layer.
Or are those collisions completely bypassed?

Solutions/Answers:

Answer 1:

Does making use of this setting decrease performance, or increase
performance?

It (generally) increases performance. If you only have a handful of colliders in a scene, this could result in a slight performance hit, but it wouldn’t be noticeable. Best practice is if you don’t ever intend two layers to interact with each other, go ahead and disable it in the Matrix.

I could imagine it decreases it if every potential collision had to be
checked for it’s layer. Or are those collisions completely bypassed?

Your assumptions are correct.

The Matrix is a filter on objects: If the object currently being checked belongs to this group, then check for collisions against object belonging to checked groups.

This means, for layers where you’ve disabled some collisions in the matrix, you are only checking for collisions between subsets of the colliders in the scene. Since there’s less checks to perform, you gain some performance.

This is really only necessary as a performance optimization when you have a large number of objects in a scene. Most of the time this is used to support game functionality. For instance if you want bullets to only impact with players and walls, but not balloons that get pushed around the room by players as they move.

Answer 2:

I could imagine it decreases it if every potential collision had to be checked for its layer. Or are those collisions completely bypassed?

I don’t have the first-hand knowledge about how Unity handles its internal connections with PhysX, but I imagine those layers would be bypassed. If anything, I imagine disabling collisions between layers should increase the performance since the physics engine would have fewer calculations to perform.


On the other hand, I imagine this question is a typical scenario of premature optimization, which I usually interpret as trying to optimize your code without actually even having a working code.

Instead of trying to predict the performance effects, I would suggest building a test project and actually measuring it.

Even better, forget about optimization and build your game. After you have a game that works but runs at 2 FPS, then you can begin optimization phase with a clearer state of mind.

References