// Schedule() implementation for ICollisionEventsJob when Havok Physics is available public static unsafe JobHandle Schedule <T>(this T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps) where T : struct, ICollisionEventsJobBase { switch (simulation.Type) { case SimulationType.UnityPhysics: // Call the scheduling method for Unity.Physics return(ICollisionEventJobExtensions.ScheduleUnityPhysicsCollisionEventsJob(jobData, simulation, ref world, inputDeps)); case SimulationType.HavokPhysics: { var data = new CollisionEventJobData <T> { UserJobData = jobData, EventReader = ((Havok.Physics.HavokSimulation)simulation).CollisionEvents }; // Ensure the input dependencies include the end-of-simulation job, so events will have been generated inputDeps = JobHandle.CombineDependencies(inputDeps, simulation.FinalSimulationJobHandle); var parameters = new JobsUtility.JobScheduleParameters( UnsafeUtility.AddressOf(ref data), CollisionEventJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Batched); return(JobsUtility.Schedule(ref parameters)); } default: return(inputDeps); } }
public unsafe static void Execute(ref CollisionEventJobData <T> jobData, IntPtr additionalData, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex) { foreach (ref LowLevel.CollisionEvent eventData in jobData.EventReader) { int numContactPoints = eventData.NumNarrowPhaseContactPoints; var contactPoints = new NativeArray <ContactPoint>(numContactPoints, Allocator.Temp); for (int i = 0; i < numContactPoints; i++) { contactPoints[i] = eventData.AccessContactPoint(i); } int bodyAIndex = eventData.BodyIndices.BodyAIndex; int bodyBIndex = eventData.BodyIndices.BodyBIndex; jobData.UserJobData.Execute(new CollisionEvent { EventData = eventData, Entities = new EntityPair { EntityA = jobData.Bodies[bodyAIndex].Entity, EntityB = jobData.Bodies[bodyBIndex].Entity }, TimeStep = jobData.TimeStep, InputVelocityA = bodyAIndex < jobData.InputVelocities.Length ? jobData.InputVelocities[bodyAIndex] : Velocity.Zero, InputVelocityB = bodyBIndex < jobData.InputVelocities.Length ? jobData.InputVelocities[bodyBIndex] : Velocity.Zero, NarrowPhaseContactPoints = contactPoints }); } }
public unsafe static void Execute(ref CollisionEventJobData <T> jobData, IntPtr additionalData, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex) { foreach (CollisionEvent collisionEvent in jobData.EventReader) { jobData.UserJobData.Execute(collisionEvent); } }
// Schedules a collision events job only for UnityPhysics simulation internal static unsafe JobHandle ScheduleUnityPhysicsCollisionEventsJob <T>(T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps) where T : struct, ICollisionEventsJobBase { SafetyChecks.CheckAreEqualAndThrow(SimulationType.UnityPhysics, simulation.Type); var data = new CollisionEventJobData <T> { UserJobData = jobData, EventReader = ((Simulation)simulation).CollisionEvents }; // Ensure the input dependencies include the end-of-simulation job, so events will have been generated inputDeps = JobHandle.CombineDependencies(inputDeps, simulation.FinalSimulationJobHandle); var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data), CollisionEventJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Batched); return(JobsUtility.Schedule(ref parameters)); }
// Schedules a collision events job only for UnityPhysics simulation internal static unsafe JobHandle ScheduleUnityPhysicsCollisionEventsJob <T>(T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps) where T : struct, ICollisionEventsJobBase { if (simulation.Type != SimulationType.UnityPhysics) { throw new ArgumentException($"Simulation type {simulation.Type} is not supported! Should be called only for SimulationType.UnityPhysics."); } var data = new CollisionEventJobData <T> { UserJobData = jobData, EventReader = ((Simulation)simulation).CollisionEvents }; // Ensure the input dependencies include the end-of-simulation job, so events will have been generated inputDeps = JobHandle.CombineDependencies(inputDeps, simulation.FinalSimulationJobHandle); var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data), CollisionEventJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Batched); return(JobsUtility.Schedule(ref parameters)); }
internal static unsafe JobHandle ScheduleImpl <T>(this T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps) where T : struct, ICollisionEventsJob { if (simulation.Type == SimulationType.UnityPhysics) { var data = new CollisionEventJobData <T> { UserJobData = jobData, EventReader = ((Simulation)simulation).CollisionEvents, Bodies = world.Bodies, TimeStep = ((Simulation)simulation).m_Context.TimeStep, InputVelocities = ((Simulation)simulation).m_Context.InputVelocities }; // Ensure the input dependencies include the end-of-simulation job, so events will have been generated inputDeps = JobHandle.CombineDependencies(inputDeps, simulation.FinalSimulationJobHandle); var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data), CollisionEventJobProcess <T> .Initialize(), inputDeps, ScheduleMode.Batched); return(JobsUtility.Schedule(ref parameters)); } return(inputDeps); }