Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

step physics world and build physics world, bad performance or bug?

Discussion in 'Physics for ECS' started by gato0429, Apr 5, 2022.

  1. gato0429

    gato0429

    Joined:
    Mar 5, 2015
    Posts:
    22
    Hello everyone I have a problem with the performance with unity physics Version 0.50.0-preview.24, when I create entities that contain dynamic physics components.
    As you can see in the image, the step physics world and build physics world system shoot up too much in their processing (ms).
    I do not understand why this happens, previously with the version of unity physics Version 0.17.0-preview.42, it did not have this performance problem with the physical ones, supporting 20000 physical entities without problems, for this example I only use 5000 entities .
    Or is it because of something that changed in this new version, that it has another way of instantiating physical entities?

    I leave the codes that I use, the first to create the entity. and the second the system that freezes the z coordinates.

    I hope you can help me,

    upload_2022-4-4_23-12-46.png

    Code (CSharp):
    1. private Entity CreateUnitRecollect(Vector3 posicion)
    2.     {
    3. EntityArchetype archetype = _entityManager.CreateArchetype(typeof(Position2D), typeof(Rotation2D),
    4.             typeof(Scale), typeof(UnidadRecolector), typeof(ComponenteUnidad), typeof(UnidadComponenteMoverA),
    5.             typeof(PathPosicion), typeof(PathfindingParametro), typeof(PathFollow), typeof(PhysicsCollider),
    6.             typeof(PhysicsMass), typeof(PhysicsVelocity), typeof(PhysicsGravityFactor),
    7.             typeof(Translation), typeof(LocalToWorld), typeof(Rotation),
    8.             //required params
    9.             typeof(SpriteIndex), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor),
    10.             typeof(SpriteMatrix), typeof(BufferHook));
    11.  
    12.         BlobAssetReference<Collider> box = Unity.Physics.BoxCollider.Create(new BoxGeometry
    13.         {
    14.             Center = float3.zero,
    15.             BevelRadius = 0f,
    16.             Orientation = quaternion.identity,
    17.             Size = new float3(.45f, .45f, 1.0f)
    18.         });
    19.  
    20.  
    21.         PhysicsCollider physicsCollider = new PhysicsCollider {Value = box};
    22.         var color = Color.white;
    23.         PhysicsMass physicsMass = PhysicsMass.CreateDynamic(physicsCollider.MassProperties, 1f);
    24.  
    25.         PhysicsVelocity physicsVelocity = new PhysicsVelocity {Angular = .05f, Linear = .01f};
    26.         PhysicsGravityFactor physicsGravityFactor = new PhysicsGravityFactor {Value = 0f};
    27.         List<IComponentData> components = new List<IComponentData>
    28.         {
    29.             new Position2D {Value = new float2(posicion.x, posicion.y)},
    30.             new UnidadComponenteMoverA {estaMovimiento = false, posicion = posicion, velocidad = 10f},
    31.             new Scale {Value = .5f},
    32.             new SpriteSheetColor {color = new float4(color.r, color.g, color.b, color.a)},
    33.             new PathfindingParametro {calcular = false},
    34.             new PathFollow() {posicion = -1},
    35.             physicsCollider,
    36.             physicsMass,
    37.             physicsVelocity,
    38.             physicsGravityFactor,          
    39.             new Translation {Value = new float3(posicion.x, posicion.y, 0)},        
    40.             new Rotation {Value = quaternion.identity}
    41.         };
    42.         // 4) Instantiate the entity to Sprite
    43.         Entity entity = SpriteSheetManager.Instantiate(archetype, components, _atlasUnidades.animacionRecolector);
    44.         _entityManager.AddBuffer<PathPosicion>(entity);
    45.         _entityManager.AddSharedComponentData(entity, new PhysicsWorldIndex());      
    46.         return entity;
    47.     }
    //////////////
    Code (CSharp):
    1.  public partial class Fisica2DBloqueoZ : SystemBase
    2.     {
    3.         private EntityQuery m_Query;
    4.         BuildPhysicsWorld m_BuildPhysicsWorldSystem;
    5.         StepPhysicsWorld m_StepPhysicsWorldSystem;
    6.         protected override void OnCreate()
    7.         {
    8.             m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    9.             m_StepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
    10.             m_Query =GetEntityQuery(ComponentType.ReadWrite<PhysicsMass>(),
    11.                 ComponentType.ReadWrite<Translation>(),
    12.                 ComponentType.ReadWrite<Rotation>(),
    13.                 ComponentType.ReadWrite<PhysicsVelocity>(),
    14.                 ComponentType.ReadOnly<ComponenteUnidad>());
    15.         }
    16.         [BurstCompile]
    17.         private struct NiveladorJob :IJobChunk
    18.         {
    19.             public ComponentTypeHandle<PhysicsMass> PhysicsMassType;
    20.             public ComponentTypeHandle<Translation> TranslationType;
    21.             public ComponentTypeHandle<Rotation> RotationType;
    22.             public ComponentTypeHandle<PhysicsVelocity> PhysicsVelocityType;
    23.             public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    24.             {
    25.                 NativeArray<PhysicsMass> physicsMassArray =  chunk.GetNativeArray(PhysicsMassType);
    26.                 NativeArray<Translation> translationsArray =  chunk.GetNativeArray(TranslationType);
    27.                 NativeArray<Rotation> RotationsArray =  chunk.GetNativeArray(RotationType);
    28.                 NativeArray<PhysicsVelocity> physicsVelocitiesArray =  chunk.GetNativeArray(PhysicsVelocityType);
    29.  
    30.                 for (int i = 0; i < chunk.Count; i++)
    31.                 {
    32.                    PhysicsMass masa =  physicsMassArray[i];
    33.                    Translation translation = translationsArray[i];
    34.                    PhysicsVelocity velocity = physicsVelocitiesArray[i];
    35.                    Rotation rotation = RotationsArray[i];
    36.                    rotation.Value = Quaternion.Euler(0,0,0);
    37.                  
    38.                    masa.InverseInertia = new float3(0,0,0);
    39.                    translation.Value.z = 0;
    40.                    masa.Transform.pos = translation.Value;
    41.                    masa.Transform.rot = rotation.Value;
    42.                    velocity.Angular = .00f; // .05
    43.                    velocity.Linear = .00f; //.01
    44.                    physicsMassArray[i] = masa;
    45.                    translationsArray[i] = translation;
    46.                    physicsVelocitiesArray[i] = velocity;
    47.  
    48.                    RotationsArray[i] = rotation;
    49.  
    50.                 }
    51.             }
    52.         }
    53.  
    54.  
    55.         protected override void OnUpdate()
    56.         {
    57.             NiveladorJob jobDetectorCollision = new NiveladorJob
    58.             {
    59.  
    60.                 PhysicsMassType = GetComponentTypeHandle<PhysicsMass>(false),
    61.                 TranslationType = GetComponentTypeHandle<Translation>(false),
    62.                 PhysicsVelocityType = GetComponentTypeHandle<PhysicsVelocity>(false),
    63.                 RotationType = GetComponentTypeHandle<Rotation>(false)
    64.             };
    65.             JobHandle jobHandle = jobDetectorCollision.ScheduleParallel(m_Query, Dependency);
    66.             jobHandle.Complete();          
    67.         }
    68.     }
     
  2. Elapotp

    Elapotp

    Joined:
    May 14, 2014
    Posts:
    98
    Last edited: May 17, 2022
  3. JamieMChr

    JamieMChr

    Unity Technologies

    Joined:
    Feb 22, 2021
    Posts:
    1
    Hey @gato0429 & @Elapotp
    This is definitely something we want to take a look at.
    Could you provide a minimal repro of the issue?

    Thanks!
     
  4. Elapotp

    Elapotp

    Joined:
    May 14, 2014
    Posts:
    98
    Hey, @JamieMChr

    It has been 2 months so, I am not quite sure I have such an issue now, cause a lot has been refactored and optimized since then. If any, I will post here.

    Thank you
     
    JamieMChr likes this.
  5. cdytoby

    cdytoby

    Joined:
    Nov 19, 2014
    Posts:
    181
    Hello, @JamieMChr

    I'm playing around and I encountered same issue here. Test project is here.

    It's using DOTS physics and URP, and it's 2D project, but 3D collider is used.

    Also, I have 0 update, I just spawned everything pre update, once, and let dots physics handle everything on its own side.
     

    Attached Files:

    Elapotp likes this.
  6. Elapotp

    Elapotp

    Joined:
    May 14, 2014
    Posts:
    98
    Small notice from my side. Some of the changes that helped me to fix that, is to disable FixedTimeStep
    Code (CSharp):
    1. _world.GetExistingSystem<FixedStepSimulationSystemGroup>().RateManager = null;
     
  7. cdytoby

    cdytoby

    Joined:
    Nov 19, 2014
    Posts:
    181
    I found out that after build an exe and run it (total runtime), the performance is good, much more fluent than in Editor. But I don't have any profiler data.

    It would be better if we can just disable the "game object conversion" things in Editor to see more real performance. I can't find any way to disable converting things back to gameobjects.