Search Unity

Anyone else have PhysicsVelocity.Linear Randomly Decreasing and then Returning to Normal?

Discussion in 'Physics for ECS' started by adammpolak, Jan 9, 2021.

  1. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    Using DOTS Physics v0.5

    I have a InputResponse system that takes WASD and updates the PhysicsVelocity of my player entity.

    Code (CSharp):
    1. Entities
    2.         .WithReadOnly(inputFromEntity)
    3.         .WithReadOnly(childFromEntity)
    4.         .WithReadOnly(localToWorldFromEntity)
    5.         .WithAll<PlayerTag, PlayerCommand, Child>()
    6.         .ForEach((Entity entity, int nativeThreadIndex, ref Translation position, ref Rotation rotation,
    7.                 ref PhysicsVelocity physicsVelocity, ref PlayerStateComponent state,
    8.                 in GhostOwnerComponent ghostOwner, in PredictedGhostComponent prediction) =>
    9.         {
    10.  
    11.             if (!GhostPredictionSystemGroup.ShouldPredict(currentTick, prediction))
    12.                 return;
    13.             var input = inputFromEntity[entity];
    14.  
    15.             PlayerCommand inputData;
    16.             if (!input.GetDataAtTick(currentTick, out inputData))
    17.                 inputData.shoot = 0;
    18.  
    19.             state.State = inputData.thrust;
    20.  
    21.             var linearVelocity = physicsVelocity.Linear.xyz;
    22.  
    23.             var amount = 50;
    24.  
    25.             if (inputData.right == 1)
    26.             {   //thrust to the right of where the player is facing
    27.                 linearVelocity -= math.mul(rotation.Value, new float3(-1,0,0)).xyz * deltaTime * amount;
    28.              
    29.             }
    30.             if (inputData.left == 1)
    31.             {   //thrust to the left of where the player is facing
    32.                 linearVelocity -= math.mul(rotation.Value, new float3(1,0,0)).xyz * deltaTime * amount;
    33.             }
    34.             if (inputData.thrust == 1)
    35.             {   //thrust forward of where the player is facing
    36.                 linearVelocity += math.mul(rotation.Value, new float3(0,0,1)).xyz * deltaTime * amount;
    37.             }
    38.             if (inputData.reverseThrust == 1)
    39.             {   //thrust backwards of where the player is facing
    40.                 linearVelocity += math.mul(rotation.Value, new float3(0,0,-1)).xyz * deltaTime * amount;
    41.             }
    42.  
    43.             linearVelocity.x = ((int)(linearVelocity.x * 100.0f))/100.0f;
    44.             linearVelocity.y = ((int)(linearVelocity.y * 100.0f))/100.0f;
    45.             linearVelocity.z = ((int)(linearVelocity.z * 100.0f))/100.0f;
    46.  
    47.             physicsVelocity.Linear.xyz = linearVelocity;
    This results in this odd jittery effect:


    I had initially thought this had something to networking but finally pinpointed the issue. The PhysicsVelocity.Linear randomly drops from its value down, and then returns to its normal value.

    I printed the PhysicsVelocity.Linear for both Server/Client (to make sure the issue wasn't a discrepancy)

    [ upload_2021-1-9_11-20-47.png

    There was no change in thrust in between those logs.

    Is this a known DOTS Physics issue because we are using preview packages or is it something else?

    I also updated to use Linear Impulses and still got the same jittery effect and change in PhysicsVelocity.Linear

    Code (CSharp):
    1.             if (inputData.right == 1)
    2.             {   //thrust to the right of where the player is facing
    3.                 // linearVelocity += math.mul(rotation.Value, new float3(1,0,0)).xyz * deltaTime * amount;
    4.                 PhysicsComponentExtensions.ApplyLinearImpulse(ref physicsVelocity, physicsMass, (math.mul(rotation.Value, new float3(1,0,0)).xyz));
    5.                
    6.             }
    7.             if (inputData.left == 1)
    8.             {   //thrust to the left of where the player is facing
    9.                 // linearVelocity += math.mul(rotation.Value, new float3(-1,0,0)).xyz * deltaTime * amount;
    10.                 PhysicsComponentExtensions.ApplyLinearImpulse(ref physicsVelocity, physicsMass, (math.mul(rotation.Value, new float3(-1,0,0)).xyz));
    11.             }
    12.             if (inputData.thrust == 1)
    13.             {   //thrust forward of where the player is facing
    14.                 // linearVelocity += math.mul(rotation.Value, new float3(0,0,1)).xyz * deltaTime * amount;
    15.                 PhysicsComponentExtensions.ApplyLinearImpulse(ref physicsVelocity, physicsMass, (math.mul(rotation.Value, new float3(0,0,1)).xyz));
    16.             }
    17.             if (inputData.reverseThrust == 1)
    18.             {   //thrust backwards of where the player is facing
    19.                 // linearVelocity += math.mul(rotation.Value, new float3(0,0,-1)).xyz * deltaTime * amount;
    20.                 PhysicsComponentExtensions.ApplyLinearImpulse(ref physicsVelocity, physicsMass, (math.mul(rotation.Value, new float3(0,0,-1)).xyz));
    21.             }
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    I have to say this looks more like an "identity" problem - your bodies keep appearing and disappearing, which makes me think something gets reshuffled around the memory somewhere and your index used to access the data is no longer valid. And then it becomes valid again. Something along those lines at least... 4.75 and 0.22 seem like completely unrelated values, so I would guess they are coming from different bodies.