Adding PhysicsVelocity components at runtime produces NaNs?

I’m running into an issue where entities do not render if I create an entity and add the PhysicsVelocity component programmatically.

I am currently re-using an existing collider & other physics components from an entity converted from a prefab. The physics for the prefab does work.

Prefab:

Generation code

    void Start()
    {
        sharedTemplate = ConvertGameObjectHierarchy(
            root: sharedColliderPrefab,
            settings: World.Active
        );

        cachedCollider = entityManager
            .GetComponentData<PhysicsCollider>(sharedTemplate)
            .Value;

        cachedPhysicsMass = entityManager.GetComponentData<PhysicsMass>(sharedTemplate);
        cachedPhysicsDamping = entityManager.GetComponentData<PhysicsDamping>(sharedTemplate);

      ....
    }
var entity = entityManager.CreateEntity();
        entityManager.AddComponent(entity, typeof(LocalToWorld));
        entityManager.AddComponent(entity, typeof(Rotation));
        entityManager.AddComponentData(entity, new Scale { Value = 0.9f });
        entityManager.AddComponentData(entity, new Translation
        {
            Value = new float3
            {
                x = x - voxelHalfDepth,
                y = y - voxelHalfDepth
            }
        });
        entityManager.AddComponentData(entity, cachedPhysicsMass);
        entityManager.AddComponentData(entity, cachedPhysicsDamping);
        entityManager.AddComponentData(entity, new PhysicsVelocity
        {
            Angular = float3.zero,
            Linear = float3.zero
        });
        entityManager.AddComponentData(entity, new PhysicsCollider
        {
            Value = cachedCollider
        });
        entityManager.AddSharedComponentData(entity, new RenderMesh
        {
            mesh = mesh,
            material = material
        });

What’s interesting is that if I use either the prefabs PhysicsVelocity or a new PhysicsVelocity, nothing shows up and these are the component values.

4820678--462197--upload_2019-8-4_13-13-49.png

But if I remove the PhysicsVelocity, then the values are okay and the boxes do show up. (But they don’t fall)

4820678--462200--upload_2019-8-4_13-15-43.png

4820678--462203--upload_2019-8-4_13-16-27.png

I had a similar issue. The root cause was I had a rotation component (required for physics) but it had no value. This somehow worked before I added the PhysicsVelocity component because it was being treated as a static object. Once I added a PhysicsVelocity component it tried to calculate angular velocity and barfed because it was undefined. Based on your code sample, I think you might be having the same issue since you aren’t actually setting Rotation data.

I would hope in the future there is more intuitive error handling around this rather than just NaN positions.

Did you ever find a solution to this? I have the same issue, With and without rotation set as Astropuffin suggested.

For you and anyone else trying to find a solution (You probably solved it, but others might not, and it kept me occupied for a bit…Ran across that post during that)

Make sure your PhysicsMass is correct, too. PhysicsVelocity uses PhysicsMass to update rotation and transform, and if it uses a bad value(especially the rotation value, it has to be quaternion.identity or another value, not (0,0,0,0) which it’ll get if you just add PhysicsMass without setting it), it’ll NAN position and rotation.

1 Like