Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to create a PhysicsMass component entirely in code

Discussion in 'Entity Component System' started by axschacher, Apr 7, 2020.

  1. axschacher

    axschacher

    Joined:
    Mar 14, 2018
    Posts:
    21
    I am attempting to create physics bodies in pure ECS without relying on the use of prefabs and ConvertToEntity. I am having issues figuring out how to create a PhysicsMass component completely from code without using the Unity docs method of using an unsafe pointer to a collider.

    Here is my current code:

    Code (CSharp):
    1. Entity e = m.CreateEntity();
    2.  
    3.         // Position Components
    4.         m.AddComponentData(e, new LocalToWorld());
    5.         m.AddComponentData(e, new Translation { Value = pos });
    6.         m.AddComponentData(e, new Rotation { Value = Quaternion.Euler(new Vector3(0f, 0f, 0f)) });
    7.        
    8.         // Moving Components
    9.         m.AddComponentData(e, new PhysicsVelocity());
    10.         m.AddComponentData(e, new CompMoves { speed = 1f });
    11.         m.AddComponentData(e, new CompControlsMovement());
    12.         m.AddComponentData(e, new CompPlayerControlsMovement());
    13.  
    14.  
    15.         // Physics Components
    16.         m.AddComponentData(e, new PhysicsCollider
    17.         {
    18.             Value = Unity.Physics.BoxCollider.Create(new BoxGeometry
    19.             {
    20.                 Center = new float3(0f, 0.5f, 0f),
    21.                 Orientation = quaternion.identity,
    22.                 Size = new float3(1f, 1f, 1f)
    23.             })
    24.         });
    25.         // FIXME
    26.         m.AddComponentData(e, PhysicsMass.CreateDynamic(new MassProperties { }, 1f));
    27.  
    28.         // Render Meshs
    29.         m.AddSharedComponentData(e, new RenderMesh
    30.         {
    31.             mesh = Resource.mesh["Cube"],
    32.             material = Resource.mat["Warrior"],
    33.             subMesh = 0,
    34.             castShadows = ShadowCastingMode.On,
    35.             receiveShadows = true
    36.         });
    37.  
    38.         return e;
    Everything works when the PhysicsMass component is commented out, however the added PhysicsMass component causes issues where the entities LocalToWorld, Rotation, and Translation values are set to NaN in the entity debugger, and the entity is nowhere to be found visually of course. I assume this is because I am not creating the component correctly. I can't find any resources on how to create PhysicsMass purely from scratch without this method in the Unity Manual where it uses an unsafe pointer: https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/getting_started.html

    Code (CSharp):
    1.  entityManager.SetComponentData(entity, new PhysicsCollider { Value = collider });
    2.     if (isDynamic)
    3.     {
    4.         Collider* colliderPtr = (Collider*)collider.GetUnsafePtr();
    5.         entityManager.SetComponentData(entity, PhysicsMass.CreateDynamic(colliderPtr->MassProperties, mass));
    Any help would be appreciated!
     
  2. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    I think you can just use the "PhysicsCollider.MassProperties" property?
     
  3. axschacher

    axschacher

    Joined:
    Mar 14, 2018
    Posts:
    21
    Thank you, that worked!

    For anyone else who might find this thread looking for answers, here's the code that seems to work:


    Code (CSharp):
    1. PhysicsCollider collider = new PhysicsCollider
    2.         {
    3.             Value = BoxCollider.Create(new BoxGeometry
    4.             {
    5.                 Center = new float3(0f, 0.5f, 0f),
    6.                 Orientation = quaternion.identity,
    7.                 Size = new float3(1f, 1f, 1f)
    8.             })
    9.         };
    10.         m.AddComponentData(e, collider);
    11.         m.AddComponentData(e, PhysicsMass.CreateDynamic(collider.MassProperties, 1f));