Moving a large amount of 2D colliders (Profiler Physics2d.FindNewContacts)

Hi everyone!

I am developing a RTS game, where the player can spawn units. Sometimes these units will be overlapping each other (when they are together in a trench/defensive position) - its a 2D game seen from the side that is the reason they are overlapping.

When they attack (exit the defensive position) a big lag spike occurs since so many 2D Box Colliders are overlapping each other - i know this because the biggest peformance costs are the Physics2D.FindNewContacts (60 %) according to the profiler

All the box colliders are Triggers since i am not actually using the Physics system to detect actual collision but using it to see when a Unit triggers with a explosion or with a trench etc.

I have read the following forum post: Profiler Physics2D.FindNewContacts

Thanks for the help!

That’s a contradicting statement. A trigger is part the physics system.

Put a thousand triggers over each other and it has to check for approximately 1000 * 1000 overlaps (so it scales-up fast!). I noticed you didn’t mention a Rigidbody2D so I presume you’re not using one (?) and that these are implicit Static (non-moving) ones you’re moving. The only thing in physics that actually moves is a Rigidbody2D and you always use its API and never, ever the Transform.

Anyway, I’ve made so many posts about this so I’d rather not go over it all again and there’s lots of info in the thread you cited which should aid you in figuring out what you’re doing.

In the end, that call is the physics system being asked to find new contacts (not it being broken or some wrong setting) because something moved and/or was recreated (because it’s Static with no Rigidbody2D) and things are overlapping. Figure out how many things are overlapping and as the thread you cited how many contacts.

Hope that helps.

Hi again Melv. Thanks for the reply. I have tried adding RigidBodies to all of the units and when i do that the FPS goes from 22 (already bad) to 0.5. They are set as Kinematic. I am indeed moving the units using Transform.Translate. Could this cause the problem?

Are you suggesting that i put a Rigidbody2D, set is as kinematic and then move it using the rigibody rathing than having no Rigidbody2D and move through Transform.Translate?

Well I said it here:

I know devs like to poke the Transform but you have to let it go and stop doing it. :slight_smile: The Rigidbody2D is in charge of the Transform, period. But repeating what I said above, you never ever move anything in physics by modifying the Transform; you use the Rigidbody2D API. Without a Rigidbody2D, modifying the Transform position/rotation causes the colliders in question are recreated from scratch so I absolutely don’t understand why it’d be slower but the profiler will show you as there are a lot of details available there.

With a Rigidbody2D you can set it to be Kinematic or Static. If it’s Static, it’s identical to what you have now but it doesn’t require the collider to be recreated. The idea that adding a Rigidbody2D is worse for performance isn’t true at all.

Without a Rigidbody2D the collider is created against a hidden Rigidbody2D anyway that is permanently at the world origin. Like any collider, if you modify its pose relative to the Rigidbody2D its attached to, it’ll need recreatin because colliders and their shapes cannot move in any way; the Rigidbody2D moves.

In short, you must have a Rigidbody2D of one body-type or another if you want the collider to move. Use the Rigidbody2D API to move it and the collider won’t need recreating. Simple stuff, one rule.

Don’t get confused by me telling you this will solve your problem though. What I’m telling you above is how you should always perform movement. You problem can be made worse by not following the above but can also be something else too. I’m not telling you how to fix your issue; recall that I have no idea about your project, what you’re doing and when. :slight_smile:

Thank you so much for you help. It all makes sense now :slight_smile:

1 Like