Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Tornado Destruction With Thousands of Rigidbodies - how to optimise?

Discussion in 'Physics' started by SamKennedy, Nov 14, 2022.

  1. SamKennedy

    SamKennedy

    Joined:
    Apr 25, 2021
    Posts:
    12
    I am using some building prefabs alongside a custom tornado collider script that I wrote to simulate tornado destruction in my game.

    The building consists of different segments like walls, floors, roof, windows etc. and each segment has hidden child game objects of the mesh broken up into smaller chunks.

    The way I’m handling the physics is as follows:

    1) When the tornado collider first hits a building (onTriggerEnter), I check to see if it’s within the camera’s viewport, if not I just destroy all the game objects that make the building

    2) If the building is within the view of the camera, I check if the maximum amount of debris is already airborne, if it is I just destroy the current game object

    3) I then use a pre-set probability, either the entire segment will have a rigid body component added to it with a random mass, or the individual chunks that make up the mesh will be activated. When activated I set the transform parent to an empty gameobject that I use as a “debris container”

    4) Each piece of debris is set to be destroyed at a random interval a few seconds into the future

    5) In the onTriggerStay, I apply the necessary physics to the debris to make it swirl around the tornado.

    The problem I’m having is that even with relatively few rigidbodies (500-1000) the game slows to about 17fps when the tornado first hits the buildings.

    What optimisations would you suggest? Would it be better to move the logic from onTriggerEnter to a coroutine that will split the cost of initialising rigidbodies over multiple frames?

    How expensive is it to add rigidbodies on the fly? I also keep checking the transform.childCount - is that an expensive operation also?

    Any advice would be much appreciated :)
     
  2. Niter88

    Niter88

    Joined:
    Jul 24, 2019
    Posts:
    112
    For rigid bodies I would say


    Well, there are other things to optimize on it.
    Like optimizing drawcalls


    You could try to use DOTS and ECS (it's hard and experimental)
     
  3. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    If the destruction is only visual, there is no good to reason fully CPU simulate it. I would rather try achieve the similar effect via a shader + particles.

    You could divide your breakable mesh into chunks with separate UV islands with 3D modeling software, then translate those islands off each other when the thing breaks.
     
    SamKennedy and Niter88 like this.
  4. Niter88

    Niter88

    Joined:
    Jul 24, 2019
    Posts:
    112
    man, kid is struggling with collisions, compute shading and chunks will brain damage him.

    is a pretty strong solution for a big project thou
     
  5. SamKennedy

    SamKennedy

    Joined:
    Apr 25, 2021
    Posts:
    12
    I really like this idea. The models I’m using are already broken up into smaller chunks, turns out the performance issue was that the prefabs I had used had a lot of different materials and meshes, I ended up combining them into a single model then switch the model out on collision. Performance gain was huge.

    I’ve started working on a vertex shader that takes in the position vector of the tornado, the radius and wind speed and will move the positions of the vertices accordingly. I’ll add some randomisation using world space position and also use a nice function to make the piece accelerate up to speed.

    It will be interesting to see how far I can push the scale of the damage compared to simulating physics. I could run the game smoothly with about 3000 batches of meshes being simulated using the physics engine for comparison.
     
  6. SamKennedy

    SamKennedy

    Joined:
    Apr 25, 2021
    Posts:
    12
    It's not complete, but this is really promising:



    I've gone from 1,000 pieces of debris at 17 FPS to 20,000 pieces at 60 - 70 FPS!

    Nope, still here and (mostly) not brain damaged.
     
    Niter88 and AlTheSlacker like this.
  7. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    Well done! I think it already looks pretty good!
     
    Niter88 likes this.
  8. Niter88

    Niter88

    Joined:
    Jul 24, 2019
    Posts:
    112
    I probably got some brain damage from constant thinking and headaches lol

    Your destruction system is looking amazing. Good job