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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
#include "UnityPrefix.h"
#if ENABLE_PHYSICS
#include "WheelCollider.h"
#include "Runtime/Graphics/Transform.h"
#include "RigidBody.h"
#include "PhysicsManager.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Filters/AABBUtility.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxPhysics.h"
#include "Runtime/BaseClasses/IsPlaying.h"
#define GET_SHAPE() static_cast<NxWheelShape*>(m_Shape)
const float kMinSize = 0.00001F;
const float kMinMass = 0.00001F;
// we can't just reinterpret_cast one to another because NxTireFunctionDesc
// has a vtable (for apparently no good reason)
static inline void FrictionCurveToNovodexTireFunc( const WheelFrictionCurve& src, NxTireFunctionDesc& dst )
{
dst.extremumSlip = src.extremumSlip;
dst.extremumValue = src.extremumValue;
dst.asymptoteSlip = src.asymptoteSlip;
dst.asymptoteValue = src.asymptoteValue;
dst.stiffnessFactor = src.stiffnessFactor;
}
static inline void NovodexTireFuncToFrictionCurve( const NxTireFunctionDesc& src, WheelFrictionCurve& dst )
{
dst.extremumSlip = src.extremumSlip;
dst.extremumValue = src.extremumValue;
dst.asymptoteSlip = src.asymptoteSlip;
dst.asymptoteValue = src.asymptoteValue;
dst.stiffnessFactor = src.stiffnessFactor;
}
WheelCollider::WheelCollider(MemLabelId label, ObjectCreationMode mode)
: Super(label, mode)
{
// set default parameters for tire curves
NxTireFunctionDesc func;
NovodexTireFuncToFrictionCurve( func, m_SidewaysFriction );
NovodexTireFuncToFrictionCurve( func, m_ForwardFriction );
}
WheelCollider::~WheelCollider ()
{
}
void WheelCollider::AwakeFromLoad(AwakeFromLoadMode awakeMode)
{
if (m_Shape)
{
// Apply changed values
SetCenter (m_Center);
SetRadius (m_Radius);
SetSuspensionDistance (m_SuspensionDistance);
SetSuspensionSpring (m_SuspensionSpring);
SetForwardFriction(m_ForwardFriction);
SetSidewaysFriction(m_SidewaysFriction);
SetMass(m_Mass);
}
Super::AwakeFromLoad (awakeMode);
}
void WheelCollider::SmartReset()
{
Super::SmartReset();
AABB aabb;
if (GetGameObjectPtr () && CalculateLocalAABB (GetGameObject (), &aabb))
{
SetCenter( aabb.GetCenter() );
SetRadius( aabb.GetExtent ().y );
SetSuspensionDistance( aabb.GetExtent ().y * 0.1f );
}
else
{
SetCenter( Vector3f::zero );
SetRadius( 1.0F );
SetSuspensionDistance( 0.1f );
}
// setup default friction curves
WheelFrictionCurve curve;
curve.extremumSlip = 1.0f;
curve.extremumValue = 20000.0f;
curve.asymptoteSlip = 2.0f;
curve.asymptoteValue = 10000.0f;
curve.stiffnessFactor = 1.0f;
SetForwardFriction( curve );
SetSidewaysFriction( curve );
}
void WheelCollider::Reset()
{
Super::Reset();
m_Center = Vector3f::zero;
m_Radius = 1.0F;
m_SuspensionDistance = 0.1F;
m_SuspensionSpring.spring = 1.0f;
m_SuspensionSpring.damper = 0.0f;
m_SuspensionSpring.targetPosition = 0.0f;
m_Mass = 1.0F;
// setup default friction curves
WheelFrictionCurve curve;
curve.extremumSlip = 1.0f;
curve.extremumValue = 20000.0f;
curve.asymptoteSlip = 2.0f;
curve.asymptoteValue = 10000.0f;
curve.stiffnessFactor = 1.0f;
m_ForwardFriction = curve;
m_SidewaysFriction = curve;
}
Vector3f WheelCollider::GetGlobalCenter() const
{
return GetComponent(Transform).TransformPoint( m_Center );
}
float WheelCollider::GetGlobalRadius() const
{
Vector3f scale = GetComponent(Transform).GetWorldScaleLossy();
return std::max( Abs(m_Radius*scale.y), kMinSize );
}
float WheelCollider::GetGlobalSuspensionDistance() const
{
Vector3f scale = GetComponent(Transform).GetWorldScaleLossy();
return std::max( Abs(m_SuspensionDistance*scale.y), kMinSize );
}
void WheelCollider::Create(const Rigidbody* ignoreAttachRigidbody)
{
if( m_Shape )
Cleanup();
NxWheelShapeDesc shapeDesc;
shapeDesc.radius = GetGlobalRadius();
shapeDesc.suspensionTravel = GetGlobalSuspensionDistance();
shapeDesc.suspension = (NxSpringDesc&)m_SuspensionSpring;
shapeDesc.inverseWheelMass = 1.0F / std::max( m_Mass, kMinMass );
shapeDesc.wheelFlags = NX_WF_INPUT_LNG_SLIPVELOCITY | NX_WF_INPUT_LAT_SLIPVELOCITY;
shapeDesc.materialIndex = 2; // use default wheel material setup in CreateScene
FrictionCurveToNovodexTireFunc( m_ForwardFriction, shapeDesc.longitudalTireForceFunction );
FrictionCurveToNovodexTireFunc( m_SidewaysFriction, shapeDesc.lateralTireForceFunction );
m_IsTrigger = false; // TBD: make something nicer!
Rigidbody* body = FindNewAttachedRigidbody (ignoreAttachRigidbody);
// WheelCollider requires a Rigidbody, or PhysX will give an error.
if (body != NULL)
FinalizeCreate( shapeDesc, false, ignoreAttachRigidbody );
// Don't show error in Editor, since during setup people may create WheelColliders on empty GameObjects,
// and later move to a Rigidbody. We don't want to spam the console with errors in that case.
else
#if UNITY_EDITOR
if (IsWorldPlaying())
#endif
ErrorStringObject("WheelCollider requires an attached Rigidbody to function.", this);
}
void WheelCollider::Cleanup()
{
Super::Cleanup();
}
void WheelCollider::SetCenter( const Vector3f& center )
{
if (m_Center != center)
{
m_Center = center;
SetDirty();
}
if( GET_SHAPE () )
{
TransformChanged( Transform::kRotationChanged | Transform::kPositionChanged );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
RefreshPhysicsInEditMode();
}
}
void WheelCollider::SetRadius( float f )
{
if (m_Radius != f)
{
SetDirty();
m_Radius = f;
}
PROFILE_MODIFY_STATIC_COLLIDER
if( GET_SHAPE () )
{
GET_SHAPE()->setRadius( GetGlobalRadius() );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
RefreshPhysicsInEditMode();
}
}
void WheelCollider::SetSuspensionDistance( float f )
{
if (m_SuspensionDistance != f)
{
SetDirty();
m_SuspensionDistance = f;
}
if( GET_SHAPE () )
{
GET_SHAPE()->setSuspensionTravel( GetGlobalSuspensionDistance() );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
void WheelCollider::SetSuspensionSpring( const JointSpring& spring )
{
if (m_SuspensionSpring != spring)
{
SetDirty();
m_SuspensionSpring = spring;
}
if( GET_SHAPE () )
{
GET_SHAPE()->setSuspension( (NxSpringDesc&)spring );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
void WheelCollider::SetSidewaysFriction( const WheelFrictionCurve& curve )
{
if (m_SidewaysFriction != curve)
{
SetDirty();
m_SidewaysFriction = curve;
}
if( GET_SHAPE () )
{
NxTireFunctionDesc func;
FrictionCurveToNovodexTireFunc( curve, func );
GET_SHAPE()->setLateralTireForceFunction( func );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
void WheelCollider::SetForwardFriction( const WheelFrictionCurve& curve )
{
if (m_ForwardFriction != curve)
{
SetDirty();
m_ForwardFriction = curve;
}
if( GET_SHAPE () )
{
NxTireFunctionDesc func;
FrictionCurveToNovodexTireFunc( curve, func );
GET_SHAPE()->setLongitudalTireForceFunction( func );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
void WheelCollider::SetMass( float f )
{
if (m_Mass != f)
{
SetDirty();
m_Mass = std::max( f, kMinMass );
}
if( GET_SHAPE() )
{
GET_SHAPE()->setInverseWheelMass( 1.0F / m_Mass );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
void WheelCollider::SetMotorTorque( float f )
{
if( m_Shape )
{
GET_SHAPE()->setMotorTorque( f );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
float WheelCollider::GetMotorTorque() const
{
if( m_Shape )
return GET_SHAPE()->getMotorTorque();
else
return 0.0F;
}
void WheelCollider::SetBrakeTorque( float f )
{
if( m_Shape )
{
GET_SHAPE()->setBrakeTorque( f );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
float WheelCollider::GetBrakeTorque() const
{
if( m_Shape )
return GET_SHAPE()->getBrakeTorque();
else
return 0.0F;
}
void WheelCollider::SetSteerAngle( float f )
{
if( m_Shape )
{
GET_SHAPE()->setSteerAngle( Deg2Rad(f) );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
}
}
float WheelCollider::GetSteerAngle() const
{
if( m_Shape )
return Rad2Deg( GET_SHAPE()->getSteerAngle() );
else
return 0.0F;
}
bool WheelCollider::IsGrounded() const
{
if( m_Shape )
{
NxWheelContactData contactData;
NxShape* otherShape = GET_SHAPE()->getContact( contactData );
return ( otherShape != NULL );
}
else
{
return false;
}
}
bool WheelCollider::GetGroundHit( WheelHit& hit ) const
{
if( m_Shape )
{
NxWheelContactData contactData;
NxShape* otherShape = GET_SHAPE()->getContact( contactData );
if( otherShape != NULL )
{
hit.point = reinterpret_cast<Vector3f&>( contactData.contactPoint );
hit.normal = reinterpret_cast<Vector3f&>( contactData.contactNormal );
hit.forwardDir = reinterpret_cast<Vector3f&>( contactData.longitudalDirection );
hit.sidewaysDir = reinterpret_cast<Vector3f&>( contactData.lateralDirection );
hit.force = contactData.contactForce;
hit.forwardSlip = contactData.longitudalSlip;
hit.sidewaysSlip = contactData.lateralSlip;
hit.collider = reinterpret_cast<Collider*>( otherShape->userData );
return true;
}
return false;
}
else
{
return false;
}
}
float WheelCollider::GetRpm() const
{
if( m_Shape )
return GET_SHAPE()->getAxleSpeed() / (kPI*2.0) * 60.0;
else
return 0.0F;
}
void WheelCollider::ScaleChanged()
{
NxWheelShape* shape = GET_SHAPE();
shape->setRadius( GetGlobalRadius() );
shape->setSuspensionTravel( GetGlobalSuspensionDistance() );
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
RefreshPhysicsInEditMode();
}
void WheelCollider::FetchPoseFromTransform()
{
FetchPoseFromTransformUtility( m_Center );
}
bool WheelCollider::GetRelativeToParentPositionAndRotation( Transform& transform, Transform& anyParent, Matrix4x4f& matrix )
{
return GetRelativeToParentPositionAndRotationUtility( transform, anyParent, m_Center, matrix );
}
void WheelCollider::TransformChanged( int changeMask )
{
if( m_Shape )
{
if( m_Shape->getActor().userData == NULL )
{
PROFILER_AUTO(gStaticColliderMove, this)
FetchPoseFromTransform();
}
else
{
Rigidbody* body = (Rigidbody*)m_Shape->getActor().userData;
Matrix4x4f matrix;
if (GetRelativeToParentPositionAndRotation( GetComponent(Transform), body->GetComponent(Transform), matrix ))
{
NxMat34 shapeMatrix;
shapeMatrix.setColumnMajor44( matrix.GetPtr () );
m_Shape->setLocalPose( shapeMatrix );
}
}
if( changeMask & Transform::kScaleChanged )
ScaleChanged();
m_Shape->getActor().wakeUp(); // wake actor up in case it was sleeping
RefreshPhysicsInEditMode();
}
}
void WheelCollider::InitializeClass ()
{
REGISTER_MESSAGE( WheelCollider, kTransformChanged, TransformChanged, int );
}
template<class TransferFunction>
void WheelCollider::Transfer (TransferFunction& transfer)
{
// NOTE: bypass Collider transfer, so that material and trigger flag are not
// serialized.
Super::Super::Transfer( transfer );
TRANSFER( m_Center );
TRANSFER_SIMPLE( m_Radius );
TRANSFER_SIMPLE( m_SuspensionDistance );
TRANSFER_SIMPLE( m_SuspensionSpring );
TRANSFER_SIMPLE( m_Mass );
TRANSFER( m_ForwardFriction );
TRANSFER( m_SidewaysFriction );
// Since we bypass Collider transfer, we need to transfer m_Enabled ourselves
transfer.Transfer (m_Enabled, "m_Enabled", kHideInEditorMask | kEditorDisplaysCheckBoxMask);
transfer.Align();
}
IMPLEMENT_CLASS_HAS_INIT( WheelCollider )
IMPLEMENT_OBJECT_SERIALIZE( WheelCollider )
#endif //ENABLE_PHYSICS
|