Search Unity

How to use RigidBodyIndex in a job?

Discussion in 'Data Oriented Technology Stack' started by Vicotin, Sep 11, 2019 at 12:43 PM.

  1. Vicotin

    Vicotin

    Joined:
    Mar 18, 2014
    Posts:
    6
    When I use the PhysicsWorld.GetRigidBodyIndex in job.There are a
    ArgumentException: Slice may not be used on a restricted range array Parameter name: array
    Unity.Collections.NativeSlice`1[T]..ctor (Unity.Collections.NativeArray`1[T] array, System.Int32 start, System.Int32 length) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeSlice.cs:105)
    Unity.Physics.CollisionWorld.get_Bodies () (at Library/PackageCache/com.unity.physics@0.2.2-preview/Unity.Physics/Collision/World/CollisionWorld.cs:19)
    Unity.Physics.PhysicsWorld.get_Bodies () (at Library/PackageCache/com.unity.physics@0.2.2-preview/Unity.Physics/Dynamics/World/PhysicsWorld.cs:18)
    Unity.Physics.Extensions.PhysicsWorldExtensions.GetRigidBodyIndex (Unity.Physics.PhysicsWorld& world, Unity.Entities.Entity entity) (at Library/PackageCache/com.unity.physics@0.2.2-preview/Unity.Physics/Extensions/World/PhysicsWorldExtensions.cs:13)
    EngineSystem+EngineJob.Execute (EngineData& engineData) (at Assets/_Scripts/EngineSystem.cs:37)


    the EngineSystem.cs:37 is"int rigidBodyIndex = physicsWorld.GetRigidBodyIndex(engineData.Entity);"

    Any and all help will be appreciated.

    Code (CSharp):
    1. public class EngineSystem : JobComponentSystem
    2. {
    3.     IGetInput input;
    4.  
    5.     BuildPhysicsWorld m_BuildPhysicsWorldSystem;
    6.  
    7.     protected override void OnStartRunning()
    8.     {
    9.         input = Manager_Input.instance.input;
    10.     }
    11.  
    12.     protected override void OnCreate()
    13.     {
    14.         m_BuildPhysicsWorldSystem = World.Active.GetOrCreateSystem<BuildPhysicsWorld>();
    15.     }
    16.  
    17.     [BurstCompile]
    18.     struct EngineJob : IJobForEach<EngineData>
    19.     {
    20.         public PhysicsWorld physicsWorld;
    21.  
    22.         public void Execute([ReadOnly]ref EngineData engineData)
    23.         {
    24.             int rigidBodyIndex = physicsWorld.GetRigidBodyIndex(engineData.Entity);
    25.             physicsWorld.ApplyImpulse(rigidBodyIndex, engineData.Impule, engineData.Point);
    26.         }
    27.     }
    28.  
    29.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    30.     {
    31.         if (!input.DoingTest) return inputDeps;
    32.         inputDeps = JobHandle.CombineDependencies(inputDeps, m_BuildPhysicsWorldSystem.FinalJobHandle);
    33.         var physicsWorld = m_BuildPhysicsWorldSystem.PhysicsWorld;
    34.         var handle = new EngineJob
    35.         {
    36.             physicsWorld = physicsWorld
    37.         }.Schedule(this, inputDeps);
    38.         handle.Complete();
    39.         return handle;
    40.     }
    41. }
    42.  
    43. public struct EngineData : IComponentData
    44. {
    45.     public Entity Entity;
    46.     public float3 Impule;
    47.     public float3 Point;
    48. }