Search Unity

Bug Problem with unity.physics IndexOutOfRangeException: Index 1 is out of restricted IJobParallelFor

Discussion in 'Physics for ECS' started by qubka, Dec 31, 2020.

  1. qubka

    qubka

    Joined:
    Dec 10, 2020
    Posts:
    4
    Hello guys, I just install entity physics today and I am kind of a new person in the unity dots community. I just have a small question about the error which I got when I install physics from the package manager. I am spawning entity with BoxCollider. I don't have any jobs running but It throw me exactly 3 errors at time, no matter how many objects I will spawn, however when I attach physics body and shape, it stop throw errors. I need physics to do simple Ray (SampleHeight to the ground) and something like Physics.OverlapSphere to get colliders inside Parallel Job, however, I don't want to use rigid body physics on the entities, only colliders. What can cause that issue? maybe better to get entities in radius with ForEach then using unity physics just for simple collision detection?

    upload_2020-12-31_2-50-55.png
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @qubka , not 100% sure what the problem is (would need to see the code), but here are some suggestions to start with:

    1. BoxCollider is a component used in GameObject physics (the "old" one), not DOTS/"entity physics" (UnityPhysics and HavokPhysics). For DOTS physics you need PhysicsShape and PhysicsBody.

    2. You need to have ConvertToEntity component on your game object, in order to convert it into an entity.

    3. If you're adding entities from code, you should make sure to do it before BuildPhysicsWorld or after ExportPhysicsWorld systems. To achieve that, you'll need system ordering attributes and if you're spawning jobs on worker threads you'll need to hook them with BuildPhysicsWorld's and ExportPhysicsWorld's jobs via AddInputDependency and GetOutputDependency. Scheduling is often discussed on this forum, so it shouldn't be too hard to find some examples (here's a recent thread on the topic, for example).


    UnityPhysicsSamples project has a very broad set of examples (Assets/Demos/1. Hello World/1c. Conversion/1c3. DOTS GravityWell.unity, for instance), so it's quite useful.
     
    qubka likes this.
  3. qubka

    qubka

    Joined:
    Dec 10, 2020
    Posts:
    4
    Thanks for your help. I definitely will try to install it again. The last my question is about PhysicsShape and PhysicsBody. Is I am able to spawn object without PhysicsBody only with PhysicsShape ? I need to move object but I don't need physics emulation on it.
     
  4. qubka

    qubka

    Joined:
    Dec 10, 2020
    Posts:
    4
    I tried different update after /before but still get the same errors for entities which I spawn with code bellow. However it work fine when I set the Dynamic/Kinetic physics. I only get errors when I spawn prefabs with static physics.

    Code (CSharp):
    1. [UpdateInGroup(typeof(SimulationSystemGroup))]
    2. //[UpdateAfter(typeof(ExportPhysicsWorld))]
    3. //[UpdateBefore(typeof(EndFramePhysicsSystem))]
    4. [UpdateBefore(typeof(BuildPhysicsWorld))]
    5. public class UnitSpawnerSystem : SystemBase
    6. {
    7.     private BeginInitializationEntityCommandBufferSystem m_BeginInitializationEcbSystem;
    8.  
    9.     protected override void OnCreate()
    10.     {
    11.         m_BeginInitializationEcbSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    12.     }
    13.  
    14.     protected override void OnUpdate()
    15.     {
    16.         var ecb = m_BeginInitializationEcbSystem.CreateCommandBuffer().AsParallelWriter();
    17.         Entities
    18.             .WithBurst(synchronousCompilation:true)
    19.             .WithAll<UnitSpawner>()
    20.             .ForEach((Entity entity, int entityInQueryIndex, ref Agent agent, in UnitSpawner spawner, in Translation trans, in Rotation rotation) => {
    21.                 //setup buffers
    22.                 var units = ecb.AddBuffer<UnitBuffer>(entityInQueryIndex, entity);
    23.  
    24.                                 //create x units of type, spaced out in a nice pattern
    25.                                 for (var i = 0; i < spawner.squadSize; i++) {
    26.                                         var unitEntity = ecb.Instantiate(entityInQueryIndex, spawner.unitPrefab);
    27.                                         var pos = trans.Value + new float3((i % 3f) * 2f, 5f, (i / 3f) * 2f);
    28.                                         //pos.y = bpw.SampleHeight(pos, ground);
    29.                                         ecb.SetComponent(entityInQueryIndex, unitEntity, new Translation { Value = pos });
    30.                                         ecb.SetComponent(entityInQueryIndex, unitEntity, new Rotation { Value = rotation.Value });
    31.                                         ecb.SetComponent(entityInQueryIndex, unitEntity, new Unit { id = i, parent = entity });
    32.                                         units.Add(new UnitBuffer { Value = unitEntity });
    33.                                 }
    34.  
    35.                 //set default state
    36.                 ecb.AddComponent<IdleBehavior>(entityInQueryIndex, entity);
    37.  
    38.                 //rotate agent correctly
    39.                 agent.orientation = rot.ToEulerAngles().y;
    40.                
    41.                 //remove spawner component
    42.                 ecb.RemoveComponent<UnitSpawner>(entityInQueryIndex, entity);
    43.                
    44.             }).ScheduleParallel();
    45.        
    46.         m_BeginInitializationEcbSystem.AddJobHandleForProducer(Dependency);
    47.         CompleteDependency();
    48.     }
    49. }
    Thats all my components on the entity which I spawning to the world with command buffer
    upload_2021-1-1_23-6-7.png
     
  5. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @qubka , sorry for late response. Here are some suggestions:
    - Move to fixed step group: [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    - Have a private field referencing BuildPhysicsWorld, like you do for BeginInitializationEntityCommandBufferSystem. Then, at the end of OnUpdate, do m_BuildPhysicsWorld.AddInputDependencyToComplete(Dependency)

    Please let me know how it goes.