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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
#include "UnityPrefix.h"
#if ENABLE_PHYSICS
#include "Joint.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Filters/AABBUtility.h"
#include "Runtime/Graphics/Transform.h"
#include "PhysicsManager.h"
#include "RigidBody.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxPhysics.h"
#include "NxWrapperUtility.h"
namespace Unity
{
Joint::Joint (MemLabelId label, ObjectCreationMode mode)
: Super(label, mode)
{
m_DidSetupAxes = false;
m_Joint = NULL;
m_Anchor = Vector3f::zero;
m_AutoConfigureConnectedAnchor = true;
m_ConnectedAnchor = Vector3f::zero;
m_Axis = Vector3f::xAxis;
m_BreakForce = std::numeric_limits<float>::infinity ();
m_BreakTorque = std::numeric_limits<float>::infinity ();
}
Joint::~Joint ()
{
Cleanup ();
}
void Joint::Reset ()
{
// Anchor is at the edge of bounding volume .y !
Super::Reset ();
AABB aabb;
if (GetGameObjectPtr () && CalculateLocalAABB (GetGameObject (), &aabb))
m_Anchor = Vector3f (0, aabb.GetCenter ().y + aabb.GetExtent ().y, 0);
else
m_Anchor = Vector3f::zero;
m_ConnectedAnchor = Vector3f::zero;
m_AutoConfigureConnectedAnchor = true;
m_Axis = Vector3f::xAxis;
}
void Joint::CheckConsistency ()
{
Super::CheckConsistency ();
Rigidbody* body = QueryComponent (Rigidbody);
Rigidbody* otherBody = m_ConnectedBody;
if (otherBody == body)
{
m_ConnectedBody = NULL;
}
}
inline Vector3f GlobalToLocalUnscaledSpace (Transform& transform, Vector3f& p)
{
Vector3f localAnchor = p - transform.GetPosition ();
localAnchor = transform.InverseTransformDirection (localAnchor);
return localAnchor;
}
/// OPTIMIZE ALL THOSE TRANSFORM ACCESSES.
/// Use a matrix instead.
void Joint::SetupAxes (NxJointDesc& desc, int options)
{
Vector3f globalAnchor, globalAxis, globalNormal;
CalculateGlobalHingeSpace (globalAnchor, globalAxis, globalNormal);
Vector3f globalConnectedAnchor = CalculateGlobalConnectedAnchor (m_AutoConfigureConnectedAnchor);
Transform& transform = GetComponent (Transform);
Transform* otherTransform = NULL;
Rigidbody* body;
body = m_ConnectedBody;
if (body)
otherTransform = body->QueryComponent (Transform);
// Setup anchor
if (options & kChangeAnchor)
{
desc.localAnchor[0] = Vec3ToNx(GlobalToLocalUnscaledSpace (transform, globalAnchor));
}
if (options & kChangeAxis)
{
desc.localNormal[0] = Vec3ToNx(transform.InverseTransformDirection (globalNormal));
desc.localAxis[0] = Vec3ToNx(transform.InverseTransformDirection (globalAxis));
}
if (otherTransform)
{
if (options & kChangeAnchor)
{
desc.localAnchor[1] = Vec3ToNx(GlobalToLocalUnscaledSpace (*otherTransform, globalConnectedAnchor));
}
if (options & kChangeAxis)
{
desc.localAxis[1] = Vec3ToNx(otherTransform->InverseTransformDirection (globalAxis));
desc.localNormal[1] = Vec3ToNx(otherTransform->InverseTransformDirection (globalNormal));
}
}
else
{
if (options & kChangeAnchor)
{
desc.localAnchor[1] = Vec3ToNx(globalConnectedAnchor);
}
if (options & kChangeAxis)
{
desc.localAxis[1] = Vec3ToNx(globalAxis);
desc.localNormal[1] = Vec3ToNx(globalNormal);
}
}
}
Vector3f Joint::CalculateGlobalConnectedAnchor (bool autoConfigureConnectedFrame)
{
Vector3f globalConnectedAnchor;
Transform* otherTransform = NULL;
Rigidbody* body = m_ConnectedBody;
if (body)
otherTransform = body->QueryComponent (Transform);
if (autoConfigureConnectedFrame)
{
Vector3f globalAnchor = GetComponent (Transform).TransformPoint (m_Anchor);
if (otherTransform)
m_ConnectedAnchor = otherTransform->InverseTransformPoint (globalAnchor);
else
m_ConnectedAnchor = globalAnchor;
}
if (otherTransform)
globalConnectedAnchor = otherTransform->TransformPoint (m_ConnectedAnchor);
else
globalConnectedAnchor = m_ConnectedAnchor;
return globalConnectedAnchor;
}
void Joint::CalculateGlobalHingeSpace (Vector3f& globalAnchor, Vector3f& globalAxis, Vector3f& globalNormal) const
{
const Transform& transform = GetComponent (Transform);
Vector3f localAxis = m_Axis;
if (SqrMagnitude (localAxis) < Vector3f::epsilon)
localAxis = Vector3f (1.0F, 0.0F, 0.0F);
globalAnchor = transform.TransformPoint (m_Anchor);
Vector3f localDirection = - m_Anchor;
Vector3f localNormal = Cross (localDirection, localAxis);
OrthoNormalize (&localAxis, &localNormal);
globalAxis = transform.TransformDirection (localAxis);
globalNormal = transform.TransformDirection (localNormal);
}
float ToNovodexInfinity (float f)
{
if (f == std::numeric_limits<float>::infinity ())
return NX_MAX_REAL;
else
return f;
}
void Joint::SetBreakForce (float force)
{
SetDirty ();
m_BreakForce = force;
if (m_Joint)
m_Joint->setBreakable (ToNovodexInfinity (m_BreakForce), GetBreakTorque ());
}
void Joint::SetBreakTorque (float torque)
{
SetDirty ();
m_BreakTorque = torque;
if (m_Joint)
m_Joint->setBreakable (GetBreakForce (), ToNovodexInfinity (m_BreakTorque));
}
float Joint::GetBreakForce () const
{
return m_BreakForce;
}
float Joint::GetBreakTorque () const
{
return m_BreakTorque;
}
void Joint::FinalizeCreateImpl (NxJointDesc& desc, bool swapActors)
{
desc.maxForce = ToNovodexInfinity (m_BreakForce);
desc.maxTorque = ToNovodexInfinity (m_BreakTorque);
desc.userData = this;
Rigidbody& body = GetComponent (Rigidbody);
body.Create (true);
body.FetchPoseFromTransform ();
AssertIf (!body.m_ActiveScene);
bool didActorsChange = false;
int actor0 = swapActors ? 1 : 0;
int actor1 = swapActors ? 0 : 1;
if (desc.actor[actor0] != body.m_Actor)
{
desc.actor[actor0] = body.m_Actor;
didActorsChange = true;
}
Rigidbody* otherBody = m_ConnectedBody;
if (otherBody != NULL && otherBody->IsActive ())
{
otherBody->Create (true);
otherBody->FetchPoseFromTransform ();
AssertIf (!otherBody->m_ActiveScene);
if (desc.actor[actor1] != otherBody->m_Actor)
{
desc.actor[actor1] = otherBody->m_Actor;
didActorsChange = true;
}
}
else
{
if (desc.actor[actor1] != NULL)
{
desc.actor[actor1] = NULL;
didActorsChange = true;
}
}
// If we don't recreate the joint novodex will mark the joint broken and then we get an assert.
// because we can't save to the desc anymore!
if (didActorsChange)
Cleanup ();
if (!m_DidSetupAxes || m_Joint == NULL)
{
SetupAxes (desc);
m_DidSetupAxes = true;
}
}
void Joint::Cleanup ()
{
if (m_Joint)
{
GetDynamicsScene ().releaseJoint (*m_Joint);
m_Joint = NULL;
}
}
void Joint::AwakeFromLoad (AwakeFromLoadMode awakeMode)
{
Super::AwakeFromLoad (awakeMode);
if (IsActive ())
Create ();
else
Cleanup ();
}
void Joint::Deactivate (DeactivateOperation operation)
{
Cleanup ();
Super::Deactivate (operation);
}
void Joint::SetConnectedBody (PPtr<Rigidbody> body)
{
if (m_ConnectedBody != body)
{
SetDirty ();
m_ConnectedBody = body;
}
if (IsActive ())
Create ();
}
void Joint::SetAxis (const Vector3f& axis)
{
SetDirty ();
m_Axis = axis;
ApplySetupAxesToDesc(kChangeAxis);
}
void Joint::SetAnchor (const Vector3f& anchor)
{
SetDirty ();
m_Anchor = anchor;
ApplySetupAxesToDesc(kChangeAnchor);
}
void Joint::SetConnectedAnchor (const Vector3f& anchor)
{
SetDirty ();
m_ConnectedAnchor = anchor;
ApplySetupAxesToDesc(kChangeAnchor);
}
void Joint::SetAutoConfigureConnectedAnchor (bool anchor)
{
SetDirty ();
m_AutoConfigureConnectedAnchor = anchor;
ApplySetupAxesToDesc(kChangeAxis|kChangeAnchor);
}
}
IMPLEMENT_CLASS (Joint)
#endif //ENABLE_PHYSICS
|