Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Poor physics simulation performance: where have I gone wrong?

Discussion in 'Physics for ECS' started by Ninjars, Nov 9, 2019.

  1. Ninjars

    Ninjars

    Joined:
    Feb 13, 2013
    Posts:
    7
    Github link: https://github.com/Ninjars/Cubetrastrophe/tree/stress-test
    Scene:
     Assets/Battleground/Gun Stress Test


    I've been playing around with ECS and the new physics for a couple of months, pulling what I could from various sources to make a little gun turret simulation. The idea is to end up with many instances of turrets and targets for the simple spectacle of scale. I'm wanting to be able to have physically simulated projectiles, so I've been working on trying to support collision events, initially to spawn particle effects at the point of impact, and later to power game logic.

    Having finally gotten my rotation maths working I attempted to scale the number of turrets from the 4 I was previously testing with to 100, but immediately saw skyrocketing frame times. This applies even if I remove any targets from the scene, so it's just a bunch of turrets sitting there, waiting for something to happen.

    Looking at the Profiler when running the scene containing 100 turrets, 1500ms is being taken up with
    NarrowPhase: ProcessBodyPairsJob
    , which is basically the entire frame time. I've been unable to find out what that really means through docs or google, I'm assuming (given its nesting inside
    SimulationSystemGroup/World CollisionSystem
    ) that it's a job that picks up collisions between physics colliders, though why it's taking so long I do not know.

    Looking at the Entity Debugger, it appears that
    CollisionSystem 
    I have written is taking up the majority of that time. However, disabling that script, either toggling it off in the debugger or by removing it from the project doesn't improve frame times; instead
    RenderMeshSystemV2 
    takes on exactly the same frame time cost. I'm not sure if this is an Entity Debugger bug or what, but it's not been very helpful for me to diagnose the issue!

    So, in summary, even without anything going on the physics simulation is taking seeming exponentially longer to process with each turret I add to the scene. I may have overlooks something with how I'm creating my entities, but even then the performance impact seems unreasonably high.

    Any pointers in general or in specific for investigating this and improving my lil project are most welcome!
     
  2. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    Took a quick look. You can diagnose some things by adding a "Physics Debug Display Authoring" + "Convert To Entity" compenent anywhere in the scene - this gives you debug draw options for colliders, contacts, etc. (Note, the collider display has some depth buffer issues..)

    One problem is that all the turrets are spawned in the same place overlapping each other, that will generate a ton of contacts that you don't want:

    upload_2019-11-9_21-16-34.png

    There may be more problems, I haven't had a deeper look. Main thing to look out for is the number of contacts being generated (you can enable drawing of those) - that number tends to map to the time spent in ProcessBodyPairsJob.
     
    NotaNaN and florianhanke like this.
  3. Ninjars

    Ninjars

    Joined:
    Feb 13, 2013
    Posts:
    7
    Thanks @Rory_Havok, that's a great tip.

    It looks like the colliders for the gun part of the turret aren't being re-positioned to be relative to the parent entity in the same way that the mesh component is. Certainly something I can look into now, many thanks :D
     
  4. Ninjars

    Ninjars

    Joined:
    Feb 13, 2013
    Posts:
    7
    @Rory_Havok I can confirm that removing the PhysicsCollision component from the gun fixes the frame rate issue, however I'm finding precious little info about how to reposition the collisions mesh that's being calculated during the gameobject -> entity conversion. I kinda hoped that the PhysicsCollision component would have some positional info in it, but the only thing I can see is the entity's Translation and the LocalToParent. It looks like the collider data isn't correctly looking up the position accounting for LocalToParent - though again there could be more to the story.

    There's a comment on https://forum.unity.com/threads/unity-physics-discussion.646486/page-9 by @LlamaFarmer42 that also mentioning issues with child entity colliders not being correctly positioned at the transform position and instead appearing to be positioned in world space, but I can't see any more discussion. Do you have any further hints, Rory?