Search Unity

Any ideas how to implement breakable trees?

Discussion in 'Editor & General Support' started by alexzzzz, Nov 13, 2012.

  1. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I'm trying to find a way to make breakable trees: a player chops a tree, it falls, when it hits the ground some branches break off, maybe the trunk breaks apart if it hits a big rock or a wall.

    I have a script that creates trees:


    It's a procedural tree. I can easily split it in arbitrary place into severals pieces, it's not a problem. The problem is how to calculate where and when to split?

    I thought about using a set of primitive colliders connected with fixed joints:
    $Captura-de-pantalla-(11).gif

    but how could I link these multiple colliders with a single tree object (before it gets broken)?
     
    Last edited: Nov 13, 2012
  2. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Well, you already got the hardest part which is the arbitrary splitting. Simple answer, add kinematic rigid bodies to your trees parts and link them using joints. Then specify the force at which the joints will break. This is easy but will look incorrect in the case of smaller objects (as bullets or small rocks) hitting the three. The hardest one is procedurally adding rigid bodies to the parts where impacts occurs. This is surely more complex that it sounds but of course give more accurate results.
     
  3. Westerbly

    Westerbly

    Joined:
    Aug 15, 2012
    Posts:
    60
    @tatoforever What about performance?
     
  4. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    The best bet will be having it procedurally (the second option) as it doesn't involve into any pre-setup and the game do not spawn with gazillions rigid bodies.
    [EDIT] I mean, workflow wise and performance wise. :rolleyes:
     
    Last edited: Nov 13, 2012
  5. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Could the main trunk and each branch be a separate object or mesh, but linked as one tree?
    If so, have a collider for each and when the tree falls slice the colliding mesh at the point of contact, outward from the collision point along the normal through the trunk or branch maybe. Seems like there are assets that do this. You'd have to just know the diameter of the mesh I guess. Would that work?
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I can't use kinematic bodies, I want a tree be able to fall and then break apart because of the collision with the ground or obstacles.

    Before I had started to mess with physics, each tree had one single mesh and a mesh collider. Today I tried to split a tree into a set of independent objects. The branches are linked to the trunk with fixed joints. All the objects have a mesh collider and a rigidbody, the trunk's rigidbody is kinematic.



    Initially the branch and trunk colliders penetrate into each other a little and when I hit Play the tree starts to dance. Looks weird. In the middle of the video I switch the trunk's IsKinematic off.

    Since you've mentioned a point of contact, are you suggesting to use OnCollisionEnter? (I was thinking about OnJointBreak.)

    Ok, let's see

    1. Using multiple colliders, instead a single convex mesh collider, helps to avoid this situation:
    $1.png

    2. If the point of contact is not at the end of a branch/trunk, then break the branch/trunk at this point.
    $2.png

    3. If the point of contact is at the end, then ... just detach the whole branch from its parent.
    $3.png

    So, I could implement it this way:
    1. Leave a tree with one single mesh.
    2. Create a bunch of child game objects with mesh colliders for trunk and all the branches.
    3. Attach a rigidbody component to the main tree object.
    4. When one of the colliders hit something, take the collision point, convert it to the coordinate system of the tree and split the tree at that point. Or if the point is close to a branch's top, cut off the branch at its base.

    I hope I haven't missed something important.

    PS
    1. I can't put a rigidbody component on the main tree object, because a rigidbody requires a collider. I have to put rigidbodies on the branches.
    2. I can't do this, because the branches will immediately fly away from each other, because they intersect a little. So, I have to make the rigibodies kinematic.
    3. I can't do this, because kinematic rigidbodies are not affected by gravity, someone needs to move them.

    Hm...
     
    Last edited: Nov 14, 2012
  7. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    I hadn't even thought about when the tree lands on the ground. This is tricky...you don't want a "glass" tree that just shatters right, some branches need to be able to bend.
    Hm... is right!

    Ok, so this isn't an area I have much experience with, maybe I can just toss out some ideas. How about:
    You have a simple skeleton of the tree...simple line segments with data that holds the physics information for the trunk and branches. Each line segment has rigidity, weight, that sort of thing. Depending on the "resolution" you want, the trunk and branches can be made up of segments of varying length.

    You create the visible mesh based on the segments, and that is what triggers collisions.
    Each collision point finds the appropriate segment, calculates the force, direction, whatever to that segment. Depending on the segment's rigidity, weight, etc...it can bend, or it might just break.

    Easy as pie, eh? :)
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    You know, when you have a clear and reliable algorithm, the coding becomes the easiest part. You just tell the things what to do and they do as you've told. It's fun.

    And the God said, "Let there be light", and there was light. :)

    I was wrong. Rigidbody doesn't require a collider.
     
  9. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    @alexzzzz,
    You should probably take a closer look at Crysis threes behaviors more closely (why not try to debug three behaviors on CryEngine Sandbox, maybe you can find something useful). I'm pretty sure they compute all physical parts (when forces and impacts occurs on the three) procedurally. Now, that's the big question, to find the most efficient/realistic way of creating such behaviors. Time for an other Crysis round up? Maybe, why not! :rolleyes:
     
    Last edited: Nov 16, 2012
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I did as I've described:
    Every branch has a concave mesh collider, the ground has a box collider.



    But then I realized that in the project, I want to use these falling trees in, the ground has a concave mesh collider, and the trees will not interact with such a ground. I have to use multiple primitive colliders on my trees:

    $tree.png
     
  11. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  12. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Unfortunately, fixed joints behave like springs.

    I tried sphere colliders (on the picture above) and then capsule colliders, doesn't work good either.

     
  13. wwg

    wwg

    Joined:
    Apr 2, 2014
    Posts:
    130
    Any progress on this idea? I'd love to have my trees break when something falls on them from above, or a vehicle crashes into them.
     
    Jaqal likes this.
  14. Jaqal

    Jaqal

    Joined:
    Jul 3, 2014
    Posts:
    288
    Anyone ever make any progress here? I've had no such luck myself.
     
  15. Will-D-Ramsey

    Will-D-Ramsey

    Joined:
    Dec 9, 2012
    Posts:
    221
    Stumbled onto this thread looking for proper angular drag and drag parameters for the falling tree. Im just about finished with a chopable tree prefab but its not procedural but it looks pretty nice just gotta add audio and particles and ill be ready to move on.
     
  16. sourbacon

    sourbacon

    Joined:
    Nov 15, 2016
    Posts:
    1
    I know this is a very old post, but did you ever get this working? It seems like you got pretty close by the end, and it would be cool to see the finished outcome!
     
  17. JotaRata

    JotaRata

    Joined:
    Dec 8, 2014
    Posts:
    61
    What about creating a TON of trees like this, what would happen to the perfomance?