Search Unity

Seemingly excessive physics calculations for the simple freefall

Discussion in 'Physics for ECS' started by WildMaN, Oct 26, 2020.

  1. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    Hey,

    I noticed considerable performance drop on mobiles for rather simple setup - 100k cubes in freefall. Just PhysicsBody, no colliders, nothing fancy. Profiler shows that a lot is going on - CreateRigidBodies, CreateMotions, Broadphase, BoundingVolumeHierarchy, RecordDynamicBodyIntegrity etc.

    Is it working as intended? With no colliders involved what's going on with those bounding boxes calculations? I'd expect a much much simpler and faster routine for a mere gravity movement.

    On a side note, while exploring the issue I've also reproduced a completely opposite bug when simulation just aborts when entities are stripped of colliders (Case 1287541).
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Adding a physics body is enough to get a full simulation. Not adding a collider just means you'll get a "default" one - unit sphere. In your case, if you don't want physics at a particular point, I'd say move the bodies on your own (in parallel, in a job) and add the PhysicsExclude component to all of them. When you want to have them collide, just remove this component.

    Also, the integrity checks can be disabled in an actual game, they are there to help you catch stuff you shouldn't be doing.
     
  3. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    Whoa! Ok, thanks for the clarification!
     
    petarmHavok likes this.
  4. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Looks like I spoke too soon. Not adding the collider is actually fine, it will result in null collider and empty AABB. Effectively, you'll get movement, but no collisions. So that's expected I assume. Other than the integrity jobs, the rest is fine, we still need create jobs to create runtime data for physics. Also, broadphase doesn't know there's no colliders in the scene, it needs to build the tree and while iterating it realizes that there are no colliders.
     
  5. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    Makes sense, though I'd expect it to be blazing fast - building up a tree of just one node )

    So I ran a custom freefall system test and surprisingly there's no performance gain at all versus default physics with no colliders.

    Code (CSharp):
    1. [UpdateInGroup(typeof(FixedStepSimulationSystemGroup)), UpdateAfter(typeof(ExportPhysicsWorld))]
    2. public class sy_CustomFreefall : SystemBase
    3. {
    4.     float timestep;
    5.  
    6.     protected override void OnCreate()
    7.     {
    8.         base.OnCreate();
    9.  
    10.         var fsss = World.GetOrCreateSystem<FixedStepSimulationSystemGroup>();
    11.         timestep = fsss.Timestep;
    12.     }
    13.  
    14.     protected override void OnUpdate()
    15.     {
    16.         var _timestep = timestep;
    17.  
    18.         Entities
    19.         .WithName("CustomFreefall")
    20.         .WithAll<sc_CustomFreefall>()
    21.         .WithBurst()
    22.         .ForEach(
    23.             (
    24.                 ref Translation trs,
    25.                 ref Rotation rt,
    26.                 ref PhysicsVelocity pv,
    27.                 in PhysicsGravityFactor pgf
    28.             ) =>
    29.             {
    30.                 pv.Linear += new float3(0, -9.81f, 0) * _timestep * pgf.Value;
    31.                 trs.Value += pv.Linear * _timestep;
    32.                 rt.Value = math.mul(rt.Value, quaternion.Euler(pv.Angular * _timestep));
    33.             }
    34.         )
    35.         .ScheduleParallel();
    36.     }
    37. }

    That's kinda strange...
     
  6. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    What physics does is - creates the world, quickly builds empty broadphase, no contacts, integrates. I would expect your system to be faster, since it only integrates. If you have a profile capture we can discuss.