Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Jobified collider physics queries in Unity.Physics

Discussion in 'Physics Previews' started by Marius_L, Apr 28, 2019.

  1. Marius_L

    Marius_L

    Joined:
    Jan 16, 2016
    Posts:
    7
    I am trying to sphere cast through my scene. I'm getting error:
    InvalidOperationException: SetStateJob.Data.castInput.Collider uses unsafe Pointers which is not allowed. Unsafe Pointers can lead to crashes and no safety against race conditions can be provided.


    Job struct:
    Code (CSharp):
    1. [BurstCompile]
    2.     unsafe struct SetStateJob : IJobForEach<Translation, PhysicsVelocity, ControllerState>
    3.     {
    4.         public CollisionWorld world;
    5.         public ColliderCastInput castInput;
    6.         public unsafe void Execute([ReadOnly] ref Translation c0, [ReadOnly] ref PhysicsVelocity c1, ref ControllerState c2)
    7.         {
    8.             ColliderCastHit hit = new ColliderCastHit();
    9.             ColliderCastInput jobInput = new ColliderCastInput()
    10.             {
    11.                 Collider = castInput.Collider,
    12.                 Direction = castInput.Direction,
    13.                 Orientation = castInput.Orientation,
    14.                 Position = c0.Value
    15.             };
    16.  
    17.             c2 = new ControllerState()
    18.             {
    19.                 Grounded = world.CastCollider(jobInput, out hit),
    20.                 CollisionNormal = hit.SurfaceNormal,
    21.                 Fraction = hit.Fraction,
    22.             };
    23.         }
    24.     }
    Schedule function:
    Code (CSharp):
    1. unsafe JobHandle ScheduleSphereCastJob(JobHandle inputDeps)
    2.     {
    3.         Unity.Physics.Collider* collider = (Unity.Physics.Collider*)Unity.Physics.SphereCollider.Create(float3.zero, 0.5f, sphereFilter).GetUnsafePtr();
    4.         JobHandle stateJob = new SetStateJob()
    5.         {
    6.             world = World.Active.GetExistingSystem<Unity.Physics.Systems.BuildPhysicsWorld>().PhysicsWorld.CollisionWorld,
    7.             castInput = new ColliderCastInput() { Collider = collider, },
    8.         }.Schedule(this, inputDeps);
    9.  
    10.         return stateJob;
    11.     }
     
  2. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    I put used an unsafe block instead of declaring the whole job unsafe and that seemed to make it happy.
    Code (CSharp):
    1. unsafe
    2. {
    3.  
    4. }
     
  3. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    Hi, give this a try:

    Code (CSharp):
    1.  
    2. using Unity.Collections.LowLevel.Unsafe;
    3.  
    4. [BurstCompile]
    5. unsafe struct SetStateJob : IJobForEach<Translation, PhysicsVelocity, ControllerState>
    6. {
    7.     public CollisionWorld world;
    8.     [NativeDisableUnsafePtrRestriction] public Collider* Collider;
    9.     public quaternion Orientation;
    10.     public float3 Direction;
    11.  
    12.     public unsafe void Execute([ReadOnly] ref Translation c0, [ReadOnly] ref PhysicsVelocity c1, ref ControllerState c2)
    13.     {
    14.         ColliderCastHit hit = new ColliderCastHit();
    15.         ColliderCastInput jobInput = new ColliderCastInput()
    16.         {
    17.             Collider = Collider,
    18.             Direction = Direction,
    19.             Orientation = Orientation,
    20.             Position = c0.Value
    21.         };
    22.  
    23.         c2 = new ControllerState()
    24.         {
    25.             Grounded = world.CastCollider(jobInput, out hit),
    26.             CollisionNormal = hit.SurfaceNormal,
    27.             Fraction = hit.Fraction,
    28.         };
    29.     }
    30. }
    31.