forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifyContactJacobiansBehaviour.cs
More file actions
230 lines (197 loc) · 9.46 KB
/
Copy pathModifyContactJacobiansBehaviour.cs
File metadata and controls
230 lines (197 loc) · 9.46 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Math = Unity.Physics.Math;
using UnityEngine;
using Unity.Burst;
public struct ModifyContactJacobians : IComponentData
{
public enum ModificationType
{
None,
SoftContact,
SurfaceVelocity,
InfiniteInertia,
NoAngularEffects,
DisabledContact,
DisabledAngularFriction,
}
public ModificationType type;
}
[Serializable]
public class ModifyContactJacobiansBehaviour : MonoBehaviour, IConvertGameObjectToEntity
{
void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
dstManager.AddComponentData(entity, new ModifyContactJacobians { type = ModificationType } );
}
public ModifyContactJacobians.ModificationType ModificationType;
}
// A system which configures the simulation step to modify contact jacobains in various ways
[UpdateBefore(typeof(StepPhysicsWorld))]
public class ModifyContactJacobiansSystem : JobComponentSystem
{
EntityQuery m_ContactModifierGroup;
StepPhysicsWorld m_StepPhysicsWorld;
protected override void OnCreate()
{
m_StepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
m_ContactModifierGroup = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(ModifyContactJacobians) }
});
}
// This job reads the modify component and sets some data on the contact, to get propogated to the jacobian
// for processing in our jacobian modifier job. This is necessary because some flags require extra data to
// be allocated along with the jacobian (e.g., SurfaceVelocity data typically does not exist). We also set
// user data bits in the jacobianFlags to save us from looking up the ComponentDataFromEntity later.
[BurstCompile]
struct SetContactFlagsJob : IContactsJob
{
[ReadOnly]
public ComponentDataFromEntity<ModifyContactJacobians> modificationData;
public void Execute(ref ModifiableContactHeader manifold, ref ModifiableContactPoint contact)
{
Entity entityA = manifold.Entities.EntityA;
Entity entityB = manifold.Entities.EntityB;
ModifyContactJacobians.ModificationType typeA = ModifyContactJacobians.ModificationType.None;
if(modificationData.Exists(entityA))
{
typeA = modificationData[entityA].type;
}
ModifyContactJacobians.ModificationType typeB = ModifyContactJacobians.ModificationType.None;
if(modificationData.Exists(entityB))
{
typeB = modificationData[entityB].type;
}
if (typeA == ModifyContactJacobians.ModificationType.SurfaceVelocity || typeB == ModifyContactJacobians.ModificationType.SurfaceVelocity)
{
manifold.JacobianFlags |= JacobianFlags.EnableSurfaceVelocity;
}
if (typeA == ModifyContactJacobians.ModificationType.InfiniteInertia || typeB == ModifyContactJacobians.ModificationType.InfiniteInertia)
{
manifold.JacobianFlags |= JacobianFlags.EnableMassFactors;
}
}
}
[BurstCompile]
struct ModifyJacobiansJob : IJacobiansJob
{
[ReadOnly]
public ComponentDataFromEntity<ModifyContactJacobians> modificationData;
// Don't do anything for triggers
public void Execute(ref ModifiableJacobianHeader h, ref ModifiableTriggerJacobian j){ }
public void Execute(ref ModifiableJacobianHeader jacHeader, ref ModifiableContactJacobian contactJacobian)
{
Entity entityA = jacHeader.Entities.EntityA;
Entity entityB = jacHeader.Entities.EntityB;
ModifyContactJacobians.ModificationType typeA = ModifyContactJacobians.ModificationType.None;
if (modificationData.Exists(entityA))
{
typeA = modificationData[entityA].type;
}
ModifyContactJacobians.ModificationType typeB = ModifyContactJacobians.ModificationType.None;
if (modificationData.Exists(entityB))
{
typeB = modificationData[entityB].type;
}
{
// Check for jacobians we want to ignore:
if (typeA == ModifyContactJacobians.ModificationType.DisabledContact || typeB == ModifyContactJacobians.ModificationType.DisabledContact)
{
jacHeader.Flags = jacHeader.Flags | JacobianFlags.Disabled;
}
// Check if NoTorque modifier, or friction should be disabled through jacobian
if (typeA == ModifyContactJacobians.ModificationType.NoAngularEffects || typeB == ModifyContactJacobians.ModificationType.NoAngularEffects ||
typeA == ModifyContactJacobians.ModificationType.DisabledAngularFriction || typeB == ModifyContactJacobians.ModificationType.DisabledAngularFriction)
{
// Disable all friction angular effects
var friction0 = contactJacobian.Friction0;
friction0.AngularA = 0.0f;
friction0.AngularB = 0.0f;
contactJacobian.Friction0 = friction0;
var friction1 = contactJacobian.Friction1;
friction1.AngularA = 0.0f;
friction1.AngularB = 0.0f;
contactJacobian.Friction1 = friction1;
var angularFriction = contactJacobian.AngularFriction;
angularFriction.AngularA = 0.0f;
angularFriction.AngularB = 0.0f;
contactJacobian.AngularFriction = angularFriction;
}
// Check if SurfaceVelocity present
if (jacHeader.HasSurfaceVelocity &&
(typeA == ModifyContactJacobians.ModificationType.SurfaceVelocity || typeB == ModifyContactJacobians.ModificationType.SurfaceVelocity))
{
// Since surface normal can change, make sure angular velocity is always relative to it, not independent
jacHeader.SurfaceVelocity = new SurfaceVelocity
{
LinearVelocity = float3.zero,
AngularVelocity = contactJacobian.Normal * (new float3(0.0f, 1.0f, 0.0f))
};
}
// Check if MassFactors present
if (jacHeader.HasMassFactors &&
(typeA == ModifyContactJacobians.ModificationType.InfiniteInertia || typeB == ModifyContactJacobians.ModificationType.InfiniteInertia))
{
// Give both bodies infinite inertia
jacHeader.MassFactors = new MassFactors
{
InvInertiaAndMassFactorA = new float4(0.0f, 0.0f, 0.0f, 1.0f),
InvInertiaAndMassFactorB = new float4(0.0f, 0.0f, 0.0f, 1.0f)
};
}
}
// Angular jacobian modifications
for (int i = 0; i < contactJacobian.NumContacts; i++)
{
ContactJacAngAndVelToReachCp jacobianAngular = jacHeader.GetAngularJacobian(i);
// Check if NoTorque modifier
if (typeA == ModifyContactJacobians.ModificationType.NoAngularEffects || typeB == ModifyContactJacobians.ModificationType.NoAngularEffects)
{
// Disable all angular effects
jacobianAngular.Jac.AngularA = 0.0f;
jacobianAngular.Jac.AngularB = 0.0f;
}
// Check if SoftContact modifier
if (typeA == ModifyContactJacobians.ModificationType.SoftContact || typeB == ModifyContactJacobians.ModificationType.SoftContact)
{
jacobianAngular.Jac.EffectiveMass *= 0.1f;
if (jacobianAngular.VelToReachCp > 0.0f)
{
jacobianAngular.VelToReachCp *= 0.5f;
}
}
jacHeader.SetAngularJacobian(i, jacobianAngular);
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
if (m_StepPhysicsWorld.Simulation.Type == SimulationType.NoPhysics)
{
return inputDeps;
}
SimulationCallbacks.Callback preparationCallback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
{
return new SetContactFlagsJob
{
modificationData = GetComponentDataFromEntity<ModifyContactJacobians>(true)
}.Schedule(simulation, ref world, inDeps);
};
SimulationCallbacks.Callback jacobianModificationCallback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
{
return new ModifyJacobiansJob
{
modificationData = GetComponentDataFromEntity<ModifyContactJacobians>(true)
}.Schedule(simulation, ref world, inDeps);
};
m_StepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostCreateContacts, preparationCallback, inputDeps);
m_StepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostCreateContactJacobians, jacobianModificationCallback, inputDeps);
return inputDeps;
}
}