Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Adding PhysicsVelocity components at runtime produces NaNs?

Discussion in 'Physics Previews' started by HeyZoos, Aug 4, 2019.

  1. HeyZoos

    HeyZoos

    Joined:
    Jul 31, 2016
    Posts:
    50
    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:

    Screenshot from 2019-08-04 13-09-35.png

    Generation code

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         sharedTemplate = ConvertGameObjectHierarchy(
    4.             root: sharedColliderPrefab,
    5.             settings: World.Active
    6.         );
    7.  
    8.         cachedCollider = entityManager
    9.             .GetComponentData<PhysicsCollider>(sharedTemplate)
    10.             .Value;
    11.  
    12.         cachedPhysicsMass = entityManager.GetComponentData<PhysicsMass>(sharedTemplate);
    13.         cachedPhysicsDamping = entityManager.GetComponentData<PhysicsDamping>(sharedTemplate);
    14.  
    15.       ....
    16.     }
    Code (CSharp):
    1. var entity = entityManager.CreateEntity();
    2.         entityManager.AddComponent(entity, typeof(LocalToWorld));
    3.         entityManager.AddComponent(entity, typeof(Rotation));
    4.         entityManager.AddComponentData(entity, new Scale { Value = 0.9f });
    5.         entityManager.AddComponentData(entity, new Translation
    6.         {
    7.             Value = new float3
    8.             {
    9.                 x = x - voxelHalfDepth,
    10.                 y = y - voxelHalfDepth
    11.             }
    12.         });
    13.         entityManager.AddComponentData(entity, cachedPhysicsMass);
    14.         entityManager.AddComponentData(entity, cachedPhysicsDamping);
    15.         entityManager.AddComponentData(entity, new PhysicsVelocity
    16.         {
    17.             Angular = float3.zero,
    18.             Linear = float3.zero
    19.         });
    20.         entityManager.AddComponentData(entity, new PhysicsCollider
    21.         {
    22.             Value = cachedCollider
    23.         });
    24.         entityManager.AddSharedComponentData(entity, new RenderMesh
    25.         {
    26.             mesh = mesh,
    27.             material = material
    28.         });

    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.

    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)

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

    upload_2019-8-4_13-16-27.png
     
  2. astropuffin

    astropuffin

    Joined:
    May 16, 2015
    Posts:
    7
    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.
     
  3. Codecomp

    Codecomp

    Joined:
    Apr 17, 2019
    Posts:
    9
    Did you ever find a solution to this? I have the same issue, With and without rotation set as Astropuffin suggested.
     
  4. Zeffi

    Zeffi

    Joined:
    Nov 18, 2017
    Posts:
    24
    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.
     
    mntuli likes this.