Is it possible to disable collision detection fot physics2D?

I’m possibly asking the wrong question. I am using Phsyics2D for manual interaction resolution. All colliders are Triggers, all RB’s are kinematic, and I simply perform overlap tests. I’ve just been profiling and found large spikes around physics, caused by:

physics2d.simulate
physics2d.findnewcontactstask
physics2d.solvediscreteislandstask

As I’m not using collision responses, no callbacks, no physical placement, is there a way for me to disable the 2D engine’s attempts to process physics interactions? Or is that not even what’s happening here?!

Edit: Bit more clarity with these profiler screenshots. The first one shows ordinary behaviour with physics taking up next to zero time even with plenty going on. The second shows nothing except lots of bullets at a very fast fire-rate resulting in a whole load of extra physics work and massive processing spike. In one frame we have multiple Physics2D.Simulates happening. This is all unwanted work.


Well yes, don’t run the simulation by setting the simulation mode to “Script” i.e. only update it when you ask for it. There’s three modes available, per fixed-update (FixedUpdate), per frame (Update) and manual via script (Script).

Alternately you can turn off all checkboxes in the Layer Collision Matrix too if you don’t want contacts between anything although these won’t even be used if the simulation doesn’t run.

I’m more curious on what you’re doing to cause so much time. Maybe you’re directly modifying the Transform or are using full Rigidbody2D for bullets when they can just be queries.

EDIT: The profiler there is saying it’s running the physics step FIVE times per-frame so that’s why it’s so high i.e. FixedUpdate is having to run so many times to keep up with game time. Maybe you’ve set the fixed-update to run at some crazy high frequency, that’ll do it. Note that rendering is taking a lot of time so that also might be why Unity is running FixedUpdate multiple times per-frame to catch up to game time.

Doesn’t look like physics, looks like FixedUpdate running lots of times because of other reasons.

Cheers. Fixed timestep is 0.02 seconds. I’ve fixed it by disabling “Auto Simulation” in the Phsyics2D properties. Now there’s zero physics2D overhead and my overlap tests are working great. Looks like Auto Simulation was activating lots of times? I can’t find any other changes I’m applying to physics in code.

1 Like

No. As I said above, it’s FixedUpdate being called multiple times. FixedUpdate is not driven by physics, it’s the other way around as it’s not just for physics; physics is called by it as part of the player-loop but so are other systems such as Animation, Scripts etc.

As I said, FixedUpdate will be called multiple times because it cannot keep with up so that can only be cause other stuff is taking longer than 0.02 so scripts/rendering etc. The profiler will show you this including how many times for things like Script FixedUpdate being called multiple times back-to-back etc.

The problem is that by Unity calling FixedUpdate multiple times, that itself takes time so can make the problem worse if whatever is running during FixedUpdate takes a lot of time, especially if running it 5 times takes a lot so you can easily end up in a sprial of death, performance-wise.

https://docs.unity3d.com/Manual/TimeFrameManagement.html

To note, it’s not called “Auto Simulation” anymore unless you’re using an older version of Unity.

2019 LTS. There was an issue with 2020 taking forever to load on some devices, notably my LG G3 phone of the time. About 30 seconds to load an near empty scene IIRC.

FixedUpdate is only be being called on two scripts. I have one game controller loop that uses FixedUpdate to process all my game objects. There are no FixedUpdate calls on any other objects. I have a LateUpdate call on every visible graphic to update its drawing.

Here’s the profiler showing a blank collision matrix with Auto Simulation on and then off and a massive spike I’m able to generate.

My FixedUpdate calls are <10 ms, well within the 20 ms time interval.

Forgive me but I’m not sure why you’re telling me the above TBH. I was only telling you that FixedUpdate is a TimeManager thing, not a Physics thing. Unity decides when to call FixedUpdate multiple times and it’s because it’s falling behind and anything can cause that including Physics itself. I was only informing you WHY FixedUpdate gets called multiple times. :slight_smile:

Assuming physics doesn’t need 10ms normally, that increased time there is likely because it’s just been asked to do a whole lot of work. If a lot is FineNewContacts then that’ll be becauses there’s suddenly a lot to calculate, maybe a load of new bullets and it must be a lot. If you don’t have a lot of contacts (again, the profiler shows this) then it could well be because your bullets are not set to ignore each other in the Layer Collision Matrix if they were (say) triggers.

Looks like physics is taking around 10ms, the real problem is that FixedUpdate is called 5 times at least which I’ve said. I don’t see anything going on that would require you turning off the simulation though but when anything scales, you must ensure it’s doing the least work so turn off all the Layer Collision Matrix because you’ve already stated you don’t need any of that. Would need to see more information from the profiler physics step hierarchy above above, the physics 2D area stats, the LCM etc.

One thing I didn’t mention is that the physics engine (Box2D) isn’t designed to be not called yet still have to deal with updates of Colliders (Physics shapes). The reason for this is that it marks each one when it changes in a queue and then processes it next time the simulation runs. I will state this is a minor thing, it’s an int in a queue but over many, many hours (possibly a day or so) it’d add up. You could clear the queue by periodically running the simulation step once, maybe a menu or scene load etc. It’s likely not going to be a problem but though it worth mentioning. We do have a simulation “flush” call internally but we don’t expose that publically because it’s a pretty specialised call.

Okay. You mentioned “FixedUpdate will be called multiple times because it cannot keep with up so that can only be cause other stuff is taking longer than 0.02 so scripts/rendering etc. The profiler will show you this including how many times for things like Script FixedUpdate being called multiple times back-to-back etc.”

So I went looking and couldn’t see anything responsible.

But as I said, it’s likely also Physics itself because you’re asking it to do too much. My point was only that anything can cause FixedUpdate to run multiple times, including physics itself.

Running five times is a lot and I’d be more interested in seeing why that is but it sounds like it’s likely a whole lot of bullets being dumped into the scene and potentially them doing contact work they should not. Total guess but I don’t have that information at hand to help you.

If you had a simpler reproduction project then I’d be happy to look at it for you to advise you further. If not, maybe post the extra profiler information, LCM etc as I mentioned above.

I have a copy of the profile data (1GB). Would that be useful? AFAICS I’m only spawning 5 shots per frame. There’s less than 200 objects present.

I can create a two “object” and bring a powerful PC to its knees. Numbers like that are kind of meaningless TBH. The number of contacts and the number of physics shapes the colliders have can dominate it most of the time.

The problem though is throughout all of this I’ve yet to see a profiler view of the physics update. You showed Physics2DFixedUpate but never expanded it for the image. The rest of the image isn’t useful info.

The other problem is that above I’ve said a bunch of things which you’ve not replied to so it might be worth re-reading. Stuff such as ensuring you’ve not got things like large triggers overlapping the play area that is set to contact everything and that you’ve reduce what can contact what by using the Layer Collision Matrix.

Sure but it might be worth addressing the above first. If you DM with your email I can set-up a workspace to host it unless you already have a way to do that. I can take a look if you give me the Unity version. it’s from.

Is this what you mean by Physics update view?

At this point I’d say only continue if it’s of value to you. You’re a busy man with higher priorities and I have my system running at speed now thanks to your insights by disabling the collision matrices and automatic simulation. However, if you are as curious as I am about the unobvious processing demands, this is profiler data from 2019.4.36f1

I didn’t say Physics update view, I said Physics2DFixedUpdate. :slight_smile:

I said,

In other words, this image you attached but didn’t expand the Physics2DFixedUpdate which is what we’re mainly discussing here:
7941406--1015855--upload_2022-3-4_15-43-9.png
Knowing it’s 58.66ms is fine but what is it comprised of?

The image you attached in your previous post, you did something similar. I cannot see the 2D physics detail pane below, just some pretty lines in the graph. Remove the Network Messages view and show me the details view by selecting 2D physics.

So you fixed it? By stopping the physics spending time calculating collisions you don’t need via the Layer Collision Matrix.

Sorry, misunderstood:

Yes. However, I don’t understand why the spike happened. Every 1/50th of a second I spawned 5 bullets consisting of a small circle collider and a Kinematic rigid body. These overlap each other and the firing ship. The firing ship has a simple polygon collider, 1 path, 16 points, and kinematic RB. All colliders are set as triggers. I have no calls to Physics scripts (OnTrigger2D etc). The bullets travel away quickly so don’t overlap anything by the next frame. There are about 70 physics objects not overlapping or interacting. This strikes me as a very simple set-up and not something to tax an AMD Ryzen 9 3900X 12-Core Processor at 3.79 GHz!

Unity won’t spend 34ms because of “5 bullets”. With all due respect, this is a story you’re telling yourself because you don’t know what is going on! :slight_smile: The number 5 though isn’t what’s relevant. I could add 5 GameObject but it might contain a million colliders. I’m not saying it does but numbers like this are irrelevant. The important part is knowing what those 5 are doing.

For example, here’s a real case reported as a bug but it happens all the time: I created a single trigger collider and my FPS drops to 20 fps. It turns out that this trigger covered a whole tilemap. It might have to figure out from the 5000 shapes, which if any it must trigger on. I could shout, “I’m only adding a single trigger!” There are many variations of this and the more common one are a bunch of overlapping objects for a few physics frames, often they have complex colliders so many, many shapes. You end up with the number of objects squared pairs of contacts to calculate.

There is a new 2D physics profiler coming that will help solve this as it provides much more detail for the broadphase here looking at broadphase updates/pairs. The manual will go into hot spots to look for and common causes:

7954183--1018774--PhysicsProfiler2D.png

For you, you’re saying you only create “5 bullets”. But there you mean a body and a polygoncollider2d. So how many shapes does this polygoncollider2D produce? Colliders don’t come into contacts in the physics engine, it knows nothing about Unity or colliders. Let’s say each created 100 shapes because it’s less than optimal. I might’ve forgot to stop those bullets from contacting each other using the LCM so the “5 bullets” equates to 5*100=500 * 500 potential contacts or 250,000 potential pairs because these 500 shapes are overlapping each other. 500 potential contacts, maybe 500 real contacts.

Hopefully you get my point in that to even stop these, the Layer Collision Matrix needs to be set correctly to stop these potentials before they even begin. Keep your shapes down to a minimum if you’re going to allow them to overlap and not use the LCM to stop them from interacting. In my example I’d ensure that my bullet colliders were not overly complex which is why they’re often simpler single physics shape colliders such as a circle, box or capsule. A polygon collider can create 1 to n shapes.

But they overlap when you create them and sounds like this is where you’re saying, “why the spike?” And you’re also saying they’re each a polygon collider with an outline of 16 points. These 16 points are an outline, not a physics physics. It gets broken down into multiple convex polygons. You can look in the inspector to see how many shapes they produce too. ShapeCount is shown in the Collider2D Info rollout.

Note that you didn’t mention you’re using the multi-threaded solver until now. A key piece of information. So not everything can go wide. It makes no difference if you have 12-Cores if the bottleneck isn’t something that’s going wide. Box2D is not multi-threaded, we had to add that into key places that we could. Some areas just cannot because it would take a rewrite but I don’t think that’s the issue here.

Triggers can be the silent enemy. They don’t produce a collision response so you don’t visually see them interacting. If you don’t add in a trigger callback or you don’t monitor that you’re getting bullet/bullet triggers then again, you’ll not know. You know when you suddenly add complex triggers overlapping complex triggers because you’ll see “FindNewContacts” and it working furiously. A simply check in the LCM stops it.

1 Like

I agree with that. It’s not a story so much as a working theory as it’s the only thing I can see related, but it’s clear I don’t know what is going on.

No, there’s one ship with 16 points, and 5 bullets which are just circle colliders which overlap for one frame.

There are no other triggers. There’s no large overlapping trigger or tilemap or anything else. It’s a very simple game, a Galaga type vertical shooter! Consider a blank scene. Add one polygon collider with 16 points, 6 shapes as a trigger. Every fixed update at 50 fps spawn five GO’s (pooled objects) consisting of a particle system and circle-collider trigger at the ship every frame. That shouldn’t be taxing any processor, multithreaded or not.

And curiously, I can have the scene with dozens of alien ships all with polygon colliders running far faster than no aliens and just circle-collider bullets.

I can well believe there’s something a bit screwy like, for example, if I was updating physics every Update, which, at 1000fps would be demanding, or if I was actually spawning shots at 1000 fps instead of 50 fps resulting in 100 overlapping bullets per frame, but I can’t see that I’m doing anything like that and I can’t see anything in the profiler pointing to that.

Well, I just went to investigate if I could find any more info, and I’m unable to repeat the issue. Enabling all interactions in the matrix and autosimulate and spawning even more shots than before, it’s performing as you’d expect. At this point the performance is just gone so definitely one to put to bed. At least I have learnt notable performance optimisations though for future reference. :smile:

Well if that’s all there was, something is very, very broken! :slight_smile:

Good luck with your project! :slight_smile: