forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifyBroadphasePairsBehaviour.cs
More file actions
78 lines (66 loc) · 2.52 KB
/
Copy pathModifyBroadphasePairsBehaviour.cs
File metadata and controls
78 lines (66 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using UnityEngine;
using Unity.Burst;
//<todo.eoin.usermod Rename to ModifyOverlappingBodyPairsComponentData?
public struct ModifyBroadphasePairs : IComponentData {}
public class ModifyBroadphasePairsBehaviour : MonoBehaviour
{
}
class ModifyBroadphasePairsBehaviourBaker : Baker<ModifyBroadphasePairsBehaviour>
{
public override void Bake(ModifyBroadphasePairsBehaviour authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new ModifyBroadphasePairs());
}
}
// A system which configures the simulation step to disable certain broad phase pairs
[UpdateInGroup(typeof(PhysicsSimulationGroup))]
[UpdateAfter(typeof(PhysicsCreateBodyPairsGroup))]
[UpdateBefore(typeof(PhysicsCreateContactsGroup))]
[RequireMatchingQueriesForUpdate]
public partial struct ModifyBroadphasePairsSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate(state.GetEntityQuery(ComponentType.ReadOnly<ModifyBroadphasePairs>()));
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var simulation = SystemAPI.GetSingleton<SimulationSingleton>();
if (simulation.Type == SimulationType.NoPhysics)
{
return;
}
var physicsWorld = SystemAPI.GetSingletonRW<PhysicsWorldSingleton>().ValueRW.PhysicsWorld;
var disablePairsJob = new DisablePairsJob
{
Bodies = physicsWorld.Bodies,
Motions = physicsWorld.MotionVelocities
};
state.Dependency = disablePairsJob.Schedule(SystemAPI.GetSingleton<SimulationSingleton>(), ref physicsWorld, state.Dependency);
}
[BurstCompile]
struct DisablePairsJob : IBodyPairsJob
{
[ReadOnly] public NativeArray<RigidBody> Bodies;
[ReadOnly] public NativeArray<MotionVelocity> Motions;
public unsafe void Execute(ref ModifiableBodyPair pair)
{
// Disable the pair if a box collides with a static object
int indexA = pair.BodyIndexA;
int indexB = pair.BodyIndexB;
if ((Bodies[indexA].Collider != null && Bodies[indexA].Collider.Value.Type == ColliderType.Box && indexB >= Motions.Length)
|| (Bodies[indexB].Collider != null && Bodies[indexB].Collider.Value.Type == ColliderType.Box && indexA >= Motions.Length))
{
pair.Disable();
}
}
}
}