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:
Entity e = m.CreateEntity();
// Position Components
m.AddComponentData(e, new LocalToWorld());
m.AddComponentData(e, new Translation { Value = pos });
m.AddComponentData(e, new Rotation { Value = Quaternion.Euler(new Vector3(0f, 0f, 0f)) });
// Moving Components
m.AddComponentData(e, new PhysicsVelocity());
m.AddComponentData(e, new CompMoves { speed = 1f });
m.AddComponentData(e, new CompControlsMovement());
m.AddComponentData(e, new CompPlayerControlsMovement());
// Physics Components
m.AddComponentData(e, new PhysicsCollider
{
Value = Unity.Physics.BoxCollider.Create(new BoxGeometry
{
Center = new float3(0f, 0.5f, 0f),
Orientation = quaternion.identity,
Size = new float3(1f, 1f, 1f)
})
});
// FIXME
m.AddComponentData(e, PhysicsMass.CreateDynamic(new MassProperties { }, 1f));
// Render Meshs
m.AddSharedComponentData(e, new RenderMesh
{
mesh = Resource.mesh["Cube"],
material = Resource.mat["Warrior"],
subMesh = 0,
castShadows = ShadowCastingMode.On,
receiveShadows = true
});
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: Simulating | Package Manager UI website
entityManager.SetComponentData(entity, new PhysicsCollider { Value = collider });
if (isDynamic)
{
Collider* colliderPtr = (Collider*)collider.GetUnsafePtr();
entityManager.SetComponentData(entity, PhysicsMass.CreateDynamic(colliderPtr->MassProperties, mass));
Any help would be appreciated!