Search Unity

Best way to join hundreds of 2d rigidbodies for destructible object

Discussion in 'Physics' started by Serinx, Jul 27, 2020.

  1. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    I'm trying to think of the best way to design a giant 2d destructible object made of squares. Each square having its own collider so you can destroy pieces of it and disconnected "chunks" will fall off.

    My first thought is to have a fixed joint for each of the squares neighbors, this way as the squares are destroyed the chunks should fall off automatically.
    I'm just a bit worried that having massive amounts of joints will have performance implications.

    Another way could be to have all the squares as children of a parent object, and every time one of the squares is destroyed, I could check to see if any squares are "disconnected" and parent them to another object - but that seems pretty heavy for runtime code.

    Can anyone think of some better ways of doing this, or have your own experiences of what works and what doesn't?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    It really depends on what you've not said yet which is how these "squares" need to react to the physcis environment.

    Lots of joints get expensive quickly so depending on the device(s) this is going to run on I would stay away from that.

    Your idea of parenting seems best but to clarify that; they can be attached to a parent with their own collider then when they are hit you can add their own Rigidbody2D (dynamic?) so that it falls and interacts with the environment if that's what you want. If it's not interacting with physics when it falls then maybe there's no need for a Collider2D or even a Rigidbody2D as you could just move it yourself by just animating it or some destroy-move script.

    If the set-up is that these are just child colliders on a parent Rigidbody2D then moving the parent body costs very little so that's the most efficient set-up. Detecting the collision is obviously easy the it's just about what you want to happen at that point when it's destroyed.
     
  3. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    @MelvMay Thanks for the reply! I would want each square to be destructible even after it has broken away from the original structure. Imagine a castle made of blocks and you cut one of the towers off by destroying the blocks at the base of the tower, that tower piece should fall to the ground and you should be able to break it down even more.

    I'll give the re-parenting approach a go and let you know how it works out.