Need help optimizing colliders for 25k entities on scene

Ok, so I’ve been pulling out my hair on this for a while. So basically I have around 25 000 entities on the scene and they are moving in random direction (i am using the unity job system to manipulate their transform). Now I need check if they are colliding. If I add standard unity 2D colliders onto them the framerate drops from around 70fps to about 0.3. I am now wondering if there is another way of checking for intersections that doesn’t create such a huge performance hit. Thank you in advance.

NOTE: I was thinking of making my own implementation of 2D Circle Colliders that the job system takes care of but they also halt the performance.

Anything goes…

Do you need collisions between those entities too? In that case you might be out of luck… Only chance I’d see is switching to the ECS framework (DOTS) and even there 25k are a huge lot.
If the entities may phase through each other but should interact with walls etc. try the Collision module of particle systems. Using the particle system for this is generally a very good idea because 25k game objects alone already have a major impact. There is a good API for using jobs and burst with particle systems too.

If you dare to, you could do it with compute shaders. This would be fast enough, especially if you do some binning / SDF. But coding this is somewhat a nightmare, if you don’t have much experience in GPU programming.

Compute shaders are a good keyword - you don’t happen to do fluid simulations, do you?
In that case I’d recommend looking at FluXY in the asset store. An amazing asset for that kind of simulations.

You can throw numbers around but that’s what the profiler is for. FPS is not a unit of measure that tells you anything. For starters, if you’re modifying Transforms and you have just a 2D Collider on it then each time that Transform changes, that Static 2D Collider would need to be completely recreated. The Profiler would show you where the time is being spent and that happening.

You would calculate what you needed then update the Rigidbody2D pose itself, not the Transform. The Rigidbody2D would update the Transform. Then there’s there contacts all these would created which depends on how many are overlapping. That’s potentially a lot of contacts.

In the end, I can write many words but only you know what you’ve done there. Box2D isn’t designed to operate at such scales for sure though and there’s many reasons why. It’s made worse by doing the stuff above on top of it but it’s not something you’ll ever get that scale out of.

It’s far easier to have a simple BVH and each of your poses has a point+radius. You can then do a quick query if you need to know what’s around it etc.

Although it was put on hold until DOTS matured, the 2D DOTS physics system produced astounding performance even on a moderate CPU. This was all about a decent BVH and efficient queries but also DOTS has a decent BOIDS simulation that uses a simpler spatial database.

https://www.youtube.com/watch?v=LBkUh7p2EjU

1 Like