summaryrefslogtreecommitdiff
path: root/Runtime/Dynamics/CharacterJoint.cpp
blob: 38dd0aa24126d48bb43e767609238b917e97a3b4 (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "UnityPrefix.h"
#if ENABLE_PHYSICS
#include "CharacterJoint.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "PhysicsManager.h"
#include "Runtime/Utilities/Utility.h"

#include "External/PhysX/builds/SDKs/Physics/include/NxPhysics.h"

using namespace std;

namespace Unity
{

#define GET_JOINT() static_cast<NxD6Joint*> (m_Joint)

inline void FixupNovodexLimitBug (NxD6JointDesc& desc)
{
	desc.twistMotion = desc.swing2Motion = desc.swing1Motion = NX_D6JOINT_MOTION_LIMITED;
}


/*
- We awake the hingejoint only once. (AwakeFromLoad)
  At this point we setup the axes. They are never changed afterwards
  -> The perfect solution remembers the old position/rotation of the rigid bodies.
      Then when changing axis/anchor is changed it generates axes that are rleative to the old position/rotation state!
*/

CharacterJoint::CharacterJoint (MemLabelId label, ObjectCreationMode mode)
	: Super(label, mode)
{
	m_TargetRotation = Quaternionf::identity();
//	m_TargetAngularVelocity = Vector3f::zero;
	m_UseTargetRotation = false;
	m_SwingAxis = Vector3f::yAxis;
}

CharacterJoint::~CharacterJoint ()
{
}

void CharacterJoint::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);
	Vector3f localNormal = m_SwingAxis;

	OrthoNormalize (&localAxis, &localNormal);

	globalAnchor = transform.TransformPoint (m_Anchor);
//	Vector3f globalRigidbodyPos = transform.GetPosition ();
	globalAxis = transform.TransformDirection (localAxis);
	
	globalNormal = transform.TransformDirection (localNormal);
	
	Matrix3x3f m;
	m.SetOrthoNormalBasisInverse(globalAxis, globalNormal, Cross (globalAxis, globalNormal));
//	m.SetOrthoNormalBasisInverse(Vector3f::xAxis, globalNormal, Cross (globalAxis, globalNormal));
//	m.SetOrthoNormalBasisInverse(globalNormal, Cross (globalAxis, globalNormal), globalAxis);
//	OrthoNormalize(m);
	Quaternionf q;
	MatrixToQuaternion(m, q);
	m_ConfigurationSpace = q * Inverse(transform.GetRotation ());
}

void CharacterJoint::Reset ()
{
	Super::Reset();
	
	m_RotationDrive.maximumForce = 20;
	m_RotationDrive.positionSpring = 50;
	m_RotationDrive.positionDamper = 5;
	
	InitSoftJointLimit (m_LowTwistLimit);
	InitSoftJointLimit (m_HighTwistLimit);
	InitSoftJointLimit (m_Swing1Limit);
	InitSoftJointLimit (m_Swing2Limit);
	
	m_LowTwistLimit.limit = -20;
	m_HighTwistLimit.limit = 70;

	m_Swing1Limit.limit = 40;
	m_Swing2Limit.limit = 0;
}

void CharacterJoint::CheckConsistency ()
{
	Super::CheckConsistency();
	m_LowTwistLimit.limit = clamp<float> (m_LowTwistLimit.limit, -180, 180);
	m_HighTwistLimit.limit = clamp<float> (m_HighTwistLimit.limit, -180, 180);
	m_Swing1Limit.limit = clamp<float> (m_Swing1Limit.limit, 0, 180);
	m_Swing2Limit.limit = clamp<float> (m_Swing2Limit.limit, 0, 180);
}

void CharacterJoint::UpdateTargetRotation ()
{
	NxD6Joint* joint = GET_JOINT ();
	if (joint)
	{
		
		Quaternionf temp = m_ConfigurationSpace * Inverse(m_TargetRotation) * Inverse(m_ConfigurationSpace);
		NxQuat targetRotation = (const NxQuat&)temp;
//		NxActor *a0, *a1;
//		joint->getActors(&a0, &a1);
//		if (a1)
//			targetRotation = a1->getGlobalOrientationQuat () * targetRotation;
//NxQuat id;
//id.id();
		joint->setDriveOrientation(targetRotation);
	}
}

Quaternionf CharacterJoint::GetTargetRotation ()
{
	return m_TargetRotation;
}

void CharacterJoint::SetTargetRotation (const Quaternionf& rot)
{
	SetDirty ();
	if (m_Joint)
	{
		NxActor* a0, *a1;
		m_Joint->getActors(&a0, &a1);
/*		if (a1)
		{
			Quaternionf q = (const Quaternionf&)a1->getGlobalOrientationQuat();
			m_TargetRotation = NormalizeSafe(Inverse(q) * rot);
		}
		else*/
			m_TargetRotation = NormalizeSafe (rot);	
	}

	UpdateTargetRotation();
}

Vector3f CharacterJoint::GetTargetAngularVelocity ()
{
	return m_TargetAngularVelocity;
}

void CharacterJoint::SetTargetAngularVelocity (const Vector3f& rot)
{
	SetDirty ();
	m_TargetAngularVelocity = rot;
	if (m_Joint)
		GET_JOINT ()->setDriveAngularVelocity ((const NxVec3&)m_TargetAngularVelocity);
}

JointDrive CharacterJoint::GetRotationDrive ()
{
	return m_RotationDrive;
}

void CharacterJoint::SetRotationDrive (const JointDrive& drive)
{
	SetDirty ();
	m_RotationDrive = drive;
	if (GET_JOINT())
	{
		NxD6JointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		FixupNovodexLimitBug(desc);
	
		int flag = 0;
		if (m_UseTargetRotation)
			flag |= NX_D6JOINT_DRIVE_POSITION;

		ConvertDrive (m_RotationDrive, desc.slerpDrive, flag);
		ConvertDrive (m_RotationDrive, desc.twistDrive, flag);
		ConvertDrive (m_RotationDrive, desc.swingDrive, flag);
		GET_JOINT()->loadFromDesc (desc);
	}
}

void CharacterJoint::SetLowTwistLimit (const SoftJointLimit& limit)
{
	SetDirty ();
	m_LowTwistLimit = limit;
	if (GET_JOINT())
	{
		NxD6JointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		FixupNovodexLimitBug(desc);

		ConvertSoftLimit (m_LowTwistLimit, desc.twistLimit.low);
		ConvertSoftLimit (m_HighTwistLimit, desc.twistLimit.high);
		if (desc.twistLimit.low.value > desc.twistLimit.high.value)
			swap (desc.twistLimit.low, desc.twistLimit.high);
		GET_JOINT()->loadFromDesc (desc);
	}
}

void CharacterJoint::SetHighTwistLimit (const SoftJointLimit& limit)
{
	SetDirty ();
	m_HighTwistLimit = limit;
	if (GET_JOINT())
	{
		NxD6JointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		FixupNovodexLimitBug(desc);


		ConvertSoftLimit (m_LowTwistLimit, desc.twistLimit.low);
		ConvertSoftLimit (m_HighTwistLimit, desc.twistLimit.high);
		if (desc.twistLimit.low.value > desc.twistLimit.high.value)
			swap (desc.twistLimit.low, desc.twistLimit.high);
		GET_JOINT()->loadFromDesc (desc);
	}
}

void CharacterJoint::SetSwing1Limit (const SoftJointLimit& limit)
{
	SetDirty ();
	m_Swing1Limit = limit;
	if (GET_JOINT())
	{
		NxD6JointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		FixupNovodexLimitBug(desc);

		ConvertSoftLimit (m_Swing1Limit, desc.swing1Limit);
		GET_JOINT()->loadFromDesc (desc);
	}
}

void CharacterJoint::SetSwing2Limit (const SoftJointLimit& limit)
{
	SetDirty ();
	m_Swing2Limit = limit;
	if (GET_JOINT())
	{
		NxD6JointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		FixupNovodexLimitBug(desc);
		
		ConvertSoftLimit (m_Swing2Limit, desc.swing2Limit);
		GET_JOINT()->loadFromDesc (desc);
	}
}

/*
- When the rigid body which is attached with the hinge joint is activated after the joint is loaded
  It will not be connected with the joint!
*/

template<class TransferFunction>
void CharacterJoint::Transfer (TransferFunction& transfer)
{
	JointTransferPre (transfer);
	TRANSFER (m_SwingAxis);
//	TRANSFER (m_UseTargetRotation);
//	TRANSFER (m_RotationDrive);
	TRANSFER (m_LowTwistLimit);
	TRANSFER (m_HighTwistLimit);
	TRANSFER (m_Swing1Limit);
	TRANSFER (m_Swing2Limit);
	
	JointTransferPost (transfer);
}

void CharacterJoint::Create ()
{
	AssertIf (!IsActive ());

	NxD6JointDesc desc;
	
	if (m_Joint && m_Joint->getState () == NX_JS_SIMULATING)
		GET_JOINT()->saveToDesc (desc);

	desc.xMotion = NX_D6JOINT_MOTION_LOCKED;
	desc.yMotion = NX_D6JOINT_MOTION_LOCKED;
	desc.zMotion = NX_D6JOINT_MOTION_LOCKED;
	
	ConvertSoftLimit (m_LowTwistLimit, desc.twistLimit.low);
	ConvertSoftLimit (m_HighTwistLimit, desc.twistLimit.high);
	
	if (desc.twistLimit.low.value > desc.twistLimit.high.value)
		swap (desc.twistLimit.low, desc.twistLimit.high);
	
	ConvertSoftLimit (m_Swing1Limit, desc.swing1Limit);
	ConvertSoftLimit (m_Swing2Limit, desc.swing2Limit);
	
	desc.swing1Motion = NX_D6JOINT_MOTION_LIMITED;
	desc.swing2Motion = NX_D6JOINT_MOTION_LIMITED;
	desc.twistMotion = NX_D6JOINT_MOTION_LIMITED;
	
//	desc.driveOrientation = (const NxQuat&)m_TargetRotation;
	desc.driveAngularVelocity = (const NxVec3&)m_TargetAngularVelocity;
	
	desc.flags = NX_D6JOINT_SLERP_DRIVE;
	
	int flag = 0;
	if (m_UseTargetRotation)
		flag |= NX_D6JOINT_DRIVE_POSITION;
	
	ConvertDrive (m_RotationDrive, desc.slerpDrive, flag);
	ConvertDrive (m_RotationDrive, desc.twistDrive, flag);
	ConvertDrive (m_RotationDrive, desc.swingDrive, flag);

	FINALIZE_CREATE (desc, NxD6Joint);
	
	///////// DO WE WANT THIS?????????
	SetTargetRotation (GetComponent(Transform).GetRotation());
}

void CharacterJoint::SetSwingAxis (const Vector3f& axis)\
{
	SetDirty ();
	m_SwingAxis = axis;
	ApplySetupAxesToDesc (kChangeAxis);
}

void CharacterJoint::ApplySetupAxesToDesc (int option)
{
	if (IsActive () && m_Joint)
	{
		NxD6JointDesc desc;
		AssertIf (m_Joint->getState () == NX_JS_BROKEN);
		GET_JOINT()->saveToDesc (desc);
		FixupNovodexLimitBug(desc);
		
		SetupAxes (desc, option);
		GET_JOINT()->loadFromDesc (desc);
		AssertIf (m_Joint->getState () == NX_JS_BROKEN);
	}
}

}


IMPLEMENT_CLASS (CharacterJoint)
IMPLEMENT_OBJECT_SERIALIZE (CharacterJoint)
#endif //ENABLE_PHYSICS