Search Unity

[SOLVED]Sample scene 2b1 not working

Discussion in 'Physics for ECS' started by Sylmerria, May 11, 2020.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hello,

    I played with physics this weekend with no success.
    So I try sample scene 2b1 which is close to what I want earlier but this sample is broken.
    Is it known ? Do I have to report a bug ?
     

    Attached Files:

  2. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    Hi! Can you be more specific about what is broken in the sample? In this particular case, the simulation will not stabilize, so it is expected that things do not stay in balance, and instead will ultimately fall.
     
  3. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hi :)

    I misunderstood the purpose of this sample then, I assumed simulation will stabilized. ok Thanks

    I tried this scene because during my tests something goes wrong, I tried to stack cubes and they always slide away whatever settings I tweak .
    With Physix engine, in GO world, stack blocks works directly :(
     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Unity Physics, being stateless does not cache friction impulses from frame to frame. This leads to an accumulation of errors and the unstable stacking drift you are seeing. You can 2 options.
    1. Download the Havok Physics for Unity package from Package Manager and switch the "Simulation Type" of the "Physics Step" component on the "Physics Scene Basic Elements > Physics Settings" node. upload_2020-5-12_13-52-9.png
      Havok Physics, replaces the simulation backed to a cached version where stacking of the boxes will work as in the GO world. However, the multi-sphere balance is inherently unstable without a high Damping/Drag factor.
    2. If sticking with Unity Physics you can clip out small velocities using the following system. I recommend adding the ClipMotionAuthoring component to the same "Physics Settings" node. This approach does have issues. The thresholds are independent of mass so if set for light bodies then stacked heavy bodies will still drift. If set for heavier bodies, lighter bodies will clip to unrealistic orientations. We are working on a better version of this system that work within the simulation, but this might get you want you need now.
      Code (CSharp):
      1. using Unity.Burst;
      2. using Unity.Collections;
      3. using Unity.Entities;
      4. using Unity.Jobs;
      5. using Unity.Mathematics;
      6. using Unity.Physics;
      7. using Unity.Physics.Systems;
      8. using UnityEngine;
      9.  
      10. public class ClipMotionAuthoring : MonoBehaviour
      11. {
      12.     //public bool EnableSystem = false;
      13.     public float LinearThreshold = 1E-4f;
      14.     public float AngularThreshold = 1E-6f;
      15.  
      16.     ClipMotionAuthoring() { }
      17.  
      18.     private void OnEnable() { OnValidate(); }
      19.  
      20.     void OnValidate()
      21.     {
      22.         var world = BasePhysicsDemo.DefaultWorld;
      23.         if (world != null)
      24.         {
      25.             var system = BasePhysicsDemo.DefaultWorld.GetOrCreateSystem<ClipMotionSystem>();
      26.             if (system != null)
      27.             {
      28.                 system.Enabled = this.enabled;
      29.                 system.LinearThreshold = LinearThreshold;
      30.                 system.AngularThreshold = AngularThreshold;
      31.             }
      32.         }
      33.     }
      34. }
      35.  
      36.  
      37. [AlwaysUpdateSystem]
      38. [UpdateBefore(typeof(StepPhysicsWorld))]
      39. public class ClipMotionSystem : JobComponentSystem
      40. {
      41.     public float LinearThreshold = 1E-4f;
      42.     public float AngularThreshold = 1E-6f;
      43.  
      44.     StepPhysicsWorld m_stepPhysicsWorld;
      45.  
      46.     protected override void OnCreate()
      47.     {
      48.         Enabled = false;
      49.         m_stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
      50.     }
      51.  
      52.     [BurstCompile]
      53.     struct ClipMotionDataJob : IJobParallelFor
      54.     {
      55.         public float linearThreshold;
      56.         public float angularThreshold;
      57.         public float3 gravityDir;
      58.  
      59.         public NativeSlice<MotionVelocity> MotionVelocities;
      60.  
      61.         public void Execute(int index)
      62.         {
      63.             var pm = MotionVelocities[index];
      64.  
      65.             float3 linearEnergy = 0.5f * math.rcp(pm.InverseMass) * (pm.LinearVelocity * pm.LinearVelocity);
      66.             float3 angularEnergy = 0.5f * math.rcp(pm.InverseInertia) * (pm.AngularVelocity* pm.AngularVelocity);
      67.  
      68.             if (math.lengthsq(linearEnergy) < linearThreshold)
      69.             {
      70.                 var gravityComponent = math.dot(pm.LinearVelocity, gravityDir) * gravityDir;
      71.                 pm.LinearVelocity = gravityComponent;
      72.                 if (math.lengthsq(angularEnergy) < angularThreshold)
      73.                 {
      74.                     pm.AngularVelocity = float3.zero;
      75.                 }
      76.             }
      77.  
      78.             MotionVelocities[index] = pm;
      79.         }
      80.     }
      81.  
      82.     protected override JobHandle OnUpdate(JobHandle inputDeps)
      83.     {
      84.         if (Enabled)
      85.         {
      86.             SimulationCallbacks.Callback clipMotionsCallback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
      87.             {
      88.                 return new ClipMotionDataJob
      89.                 {
      90.                     gravityDir = math.normalizesafe(GetSingleton<PhysicsStep>().Gravity),
      91.                     linearThreshold = LinearThreshold,
      92.                     angularThreshold = AngularThreshold,
      93.                     MotionVelocities = world.MotionVelocities,
      94.                 }.Schedule(world.NumDynamicBodies, 64, inDeps);
      95.             };
      96.             m_stepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostSolveJacobians, clipMotionsCallback, inputDeps);
      97.         }
      98.  
      99.         return inputDeps;
      100.     }
      101. }
      102.  
     
    Adam-Mechtley and florianhanke like this.
  5. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Oh I will try that, thanks for hints !