Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question BuildPhysicsWorld call frequency without structural changes

Discussion in 'Physics for ECS' started by heu3becteh, Nov 8, 2022.

  1. heu3becteh


    Aug 6, 2020
    It seems that even without structural changes some "light version" of Physics.Systems.BuildPhysicsWorld is being called from time to time, especially when some ray is being cast.

    On the Profiler graph there is a time when no ray is cast, but there is a peak indicating Default World Unity.Physics.Systems.PhysicsInitializeGroupInternal activity anyway.
    After that there is a period when raycast is done in MonoBehaviour Update(), indicated by a "saw" of activity.

    I understand the peaks of activity when some structural change is happening, but what else is triggering Default World Unity.Physics.Systems.PhysicsSystemGroup - Default World Unity.Physics.Systems.PhysicsInitializeGroup - Default World Unity.Physics.Systems.PhysicsInitializeGroupInternal - Default World Unity.Physics.Systems.BuildPhysicsWorld - Jobs:CreateRigidBodies?

    The profiler screenshot is from sort of stress test, when there are tens of thousands Box PhysicsCollider Entities.

    The code of RaycastHit call looks like that:
    Code (CSharp):
    1. public static Unity.Physics.RaycastHit RaycastHit(float3 fromPosition, float3 toPosition, ref bool doesHit, uint _collidesWith = ~0u, uint _belongsTo = ~0u)
    2. {
    3.     PhysicsWorld physicsWorld = new EntityQueryBuilder(Allocator.Temp).WithAll<PhysicsWorldSingleton>().Build(World.DefaultGameObjectInjectionWorld.EntityManager).GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
    4.     CollisionWorld collisionWorld = physicsWorld.CollisionWorld;
    5.     RaycastInput raycastInput = new RaycastInput
    6.     {
    7.         Start = fromPosition,
    8.         End = toPosition,
    9.         Filter = new CollisionFilter
    10.         {
    11.             CollidesWith = _collidesWith,
    12.             BelongsTo = _belongsTo
    13.         }
    14.     };
    15.     Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
    16.     doesHit = collisionWorld.CastRay(raycastInput, out raycastHit);
    17.     return raycastHit;
    18. }
    So I guess getting the PhysicsWorld is triggering the update, but it happens occasionally even without the ray being cast.
    Is there any sense in trying to store the CollisionWorld in attempt to reduce BuildPhysicsWorld calls?

    Best regards.