Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to use RigidBodyIndex in a job?

Discussion in 'Data Oriented Technology Stack' started by Vicotin, Sep 11, 2019.

  1. Vicotin

    Vicotin

    Joined:
    Mar 18, 2014
    Posts:
    7
    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. }
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,968
    Did you ever find out the syntax to use RigidBodyIndex properly? I am getting a slightly different error but believe it is from not passing in an array of hits using the RigidBodyIndex. This syntax is tricky and the docs are remit in giving even the basics of what something does. The CollisonEvent Execute works differently. No other refs or inoputs allowed except Execute(CollisionEvent collisionEvent) {//code}
     
unityunity