Search Unity

physics performance issue -> compound-colliders hierarchy changes

Discussion in 'Scripting' started by ADNCG, Sep 25, 2021.

  1. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I am suffering massive performance hits from moving several colliders to new rigidbodies, aka modifying compound colliders (as in .SetParent and not physically moving).

    I have a world made of voxels. It's physics based.

    Structures are made of several blocks.
    There is one RB / structure.
    One collider / block.
    One or more "root" block.

    When structures are damaged, they
    • iterate over their content and look for disconnected blocks. (Disconnected blocks by definition are blocks that are no longer connected to any root blocks)
    • They then make clusters with those disconnected blocks in order to create new structures.
    • They create a new structure for each cluster, and move the relevant blocks to it. (CULPRIT)

    The problem is that the physics engine seems to do something under the hood every time a compound collider is modified, instead of once per step. This is the main drop in performance by far for me.

    Transform.SetParent() -> Physics.HandleColliderHierarchyChange.

    Auto-sync transforms is disabled.

    Can't think of a way around this. I want the blocks to move together.

    I can't turn large rectangles of blocks into single blocks since I need to be able to do individual targeting/destruction. I probably could derive what block is being damaged from a big collider but there are many ways of damaging blocks and I really would prefer not to go there unless I absolutely have to.

    Thoughts?
     
    Last edited: Sep 25, 2021
  2. NocturnalWisp

    NocturnalWisp

    Joined:
    Oct 2, 2019
    Posts:
    62
    Hello there!

    I don't have a full solution to your problem, but a suggestion that may help with the performance hit is to space out the algorithm to happen over a couple frames rather than all at once. The visual effect wouldn't be super noticeable and you wouldn't have that lag all on one frame and you can divide the issue over time clobbering lag spikes.

    My other suggestion would be to use coroutines scheduled for a couple frames later to do the same as above instead of overhandling the timing of each cluster algorithm.

    Hope this suggestion helps!
     
    ADNCG likes this.
  3. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    The thought of manually moving blocks for a few steps scares me. I feel like I'll get weird sim issues since the structure will collide with blocks that should be part of its compound, losing momentum, or if done in WaitForFixedUpdate, then minor depenetration, or lack of, issues.

    That being said, I'll try it. It definitely sounds better to me than writing a ton of custom collisions code. Thanks for replying!
     
  4. NocturnalWisp

    NocturnalWisp

    Joined:
    Oct 2, 2019
    Posts:
    62
    Of course! If that method doesn't work, (for the reasons you've given) You could always try and apply the object pool pattern and have predefined clusters in one form or another (could be procedurally altered based on whatever values) that could be spawned when a cluster breaks off, as apposed to working with a bunch of collections.

    Also could cause potential issues with physics, but it's a thought!

    Good luck!
     
    ADNCG likes this.