Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Adding physics body to a parented object in a system

Discussion in 'Physics for ECS' started by Avol, Dec 26, 2022.

  1. Avol


    May 27, 2016
    I'm trying to add a PhysicsBody to a tree. Each part of the tree trunk is parented under each other. The idea is to be able to chop individual parts and they would fall. Doing this with mono behaviour is as simple as adding a rigidbody. In ECS theres many other elements.


    This is as far as I managed to get:

    Code (CSharp):
    2.                 _ECB.AddComponent(treeParts[3].Entity, collider);
    3.                 _ECB.AddSharedComponent(treeParts[3].Entity, new PhysicsWorldIndex());
    5.                 // collider key pair.
    6.                 _ECB.AddBuffer<PhysicsColliderKeyEntityPair>(treeParts[3].Entity);
    7.                 PhysicsColliderKeyEntityPair pair = new PhysicsColliderKeyEntityPair();
    8.                 pair.Entity = treeParts[4].Entity;
    9.                 _ECB.AppendToBuffer(treeParts[3].Entity, pair);
    12.                 // rigidbody
    13.                 _ECB.AddComponent(treeParts[3].Entity, new PhysicsVelocity());
    14.                 _ECB.AddComponent(treeParts[3].Entity, new PhysicsDamping { Angular = 0.05f, Linear = 0.01f });
    16.                 MassProperties massProperties = new MassProperties();
    17.                 massProperties.MassDistribution.Transform.pos = new float3(0, -0.22f, 1.8f);
    18.                 massProperties.MassDistribution.Transform.rot = new quaternion(0.1f, 0, 0, 1);
    19.                 massProperties.AngularExpansionFactor = 3.29f;
    20.                 massProperties.Volume = 1;
    21.                 massProperties.MassDistribution.InertiaTensor = 1.0f / new float3(0.63f, 0.63f, 23.27f);
    22.                 _ECB.AddComponent(treeParts[3].Entity, PhysicsMass.CreateDynamic(massProperties, 1.0f));
    the mass properties is under question here, as I'm unable to figure out how to apply correct properties. and the part just flies away in one direction. Also somehow I need to create a collider from physicsshape authoring component, because nothing gets attached to the entity when adding the component.
    has anybody tried doing something similar? how would I do this?
    Last edited: Dec 26, 2022