aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/PhysX/src/NpRigidBodyTemplate.h
blob: 28443fd5ef61acb0fa44671c729e058bc5fcf9bb (plain) (blame)
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  


#ifndef PX_PHYSICS_NP_RIGIDBODY_TEMPLATE
#define PX_PHYSICS_NP_RIGIDBODY_TEMPLATE

#include "NpRigidActorTemplate.h"
#include "ScbBody.h"
#include "NpPhysics.h"
#include "NpShape.h"
#include "NpScene.h"

namespace physx
{

PX_INLINE PxVec3 invertDiagInertia(const PxVec3& m)
{
	return PxVec3(	m.x == 0.0f ? 0.0f : 1.0f/m.x,
					m.y == 0.0f ? 0.0f : 1.0f/m.y,
					m.z == 0.0f ? 0.0f : 1.0f/m.z);
}


#if PX_ENABLE_DEBUG_VISUALIZATION
/*
given the diagonal of the body space inertia tensor, and the total mass
this returns the body space AABB width, height and depth of an equivalent box
*/
PX_INLINE PxVec3 getDimsFromBodyInertia(const PxVec3& inertiaMoments, PxReal mass)
{
	const PxVec3 inertia = inertiaMoments * (6.0f/mass);
	return PxVec3(	PxSqrt(PxAbs(- inertia.x + inertia.y + inertia.z)),
					PxSqrt(PxAbs(+ inertia.x - inertia.y + inertia.z)),
					PxSqrt(PxAbs(+ inertia.x + inertia.y - inertia.z)));
}
#endif


template<class APIClass>
class NpRigidBodyTemplate : public NpRigidActorTemplate<APIClass>
{
private:
	typedef		NpRigidActorTemplate<APIClass> RigidActorTemplateClass;
public:
// PX_SERIALIZATION
										NpRigidBodyTemplate(PxBaseFlags baseFlags) : RigidActorTemplateClass(baseFlags), mBody(PxEmpty)	{}
//~PX_SERIALIZATION
	virtual								~NpRigidBodyTemplate();

	//---------------------------------------------------------------------------------
	// PxRigidActor implementation
	//---------------------------------------------------------------------------------
	// The rule is: If an API method is used somewhere in here, it has to be redeclared, else GCC whines
	virtual			PxTransform			getGlobalPose() const = 0;

	//---------------------------------------------------------------------------------
	// PxRigidBody implementation
	//---------------------------------------------------------------------------------

	// Center of mass pose
	virtual			PxTransform 		getCMassLocalPose() const;

	// Mass
	virtual			void				setMass(PxReal mass);
	virtual			PxReal				getMass() const;
	virtual			PxReal				getInvMass() const;

	virtual			void				setMassSpaceInertiaTensor(const PxVec3& m);
	virtual			PxVec3				getMassSpaceInertiaTensor() const;
	virtual			PxVec3				getMassSpaceInvInertiaTensor() const;

	// Velocity
	virtual			PxVec3				getLinearVelocity()	const;
	virtual			PxVec3				getAngularVelocity() const;

	virtual			PxShape*			createShape(const PxGeometry& geometry, PxMaterial*const* material, PxU16 materialCount, PxShapeFlags shapeFlags);
	virtual			void				attachShape(PxShape& shape);

	//---------------------------------------------------------------------------------
	// Miscellaneous
	//---------------------------------------------------------------------------------
										NpRigidBodyTemplate(PxType concreteType, PxBaseFlags baseFlags, const PxActorType::Enum type, const PxTransform& bodyPose);

	PX_FORCE_INLINE	const Scb::Body&	getScbBodyFast()		const	{ return mBody;			}	// PT: important: keep returning an address here (else update prefetch in SceneQueryManager::addShapes)
	PX_FORCE_INLINE	Scb::Body&			getScbBodyFast()				{ return mBody;			}	// PT: important: keep returning an address here (else update prefetch in SceneQueryManager::addShapes)

	PX_FORCE_INLINE	Scb::Actor&			getScbActorFast()				{ return mBody;			}
	PX_FORCE_INLINE	const Scb::Actor&	getScbActorFast()		const	{ return mBody;			}

	// Flags
	virtual		void				setRigidBodyFlag(PxRigidBodyFlag::Enum, bool value);
	virtual		void				setRigidBodyFlags(PxRigidBodyFlags inFlags);
	PX_FORCE_INLINE	PxRigidBodyFlags	getRigidBodyFlagsFast() const
	{
		return getScbBodyFast().getFlags();
	}
	virtual		PxRigidBodyFlags	getRigidBodyFlags() const
	{
		NP_READ_CHECK(NpActor::getOwnerScene(*this));
		return getRigidBodyFlagsFast();
	}

	virtual void setMinCCDAdvanceCoefficient(PxReal advanceCoefficient);

	virtual PxReal getMinCCDAdvanceCoefficient() const;

	virtual void setMaxDepenetrationVelocity(PxReal maxDepenVel);

	virtual PxReal getMaxDepenetrationVelocity() const;

	virtual void setMaxContactImpulse(PxReal maxDepenVel);

	virtual PxReal getMaxContactImpulse() const;

protected:
					void				setCMassLocalPoseInternal(const PxTransform&);

					void				addSpatialForce(const PxVec3* force, const PxVec3* torque, PxForceMode::Enum mode);
					void				clearSpatialForce(PxForceMode::Enum mode, bool force, bool torque);

	PX_INLINE		void				updateBody2Actor(const PxTransform& newBody2Actor);

	PX_FORCE_INLINE void				setRigidBodyFlagsInternal(const PxRigidBodyFlags& currentFlags, const PxRigidBodyFlags& newFlags);

#if PX_ENABLE_DEBUG_VISUALIZATION
public:
					void				visualize(Cm::RenderOutput& out, NpScene* scene);
#endif

	PX_FORCE_INLINE bool				isKinematic()
	{
		return (APIClass::getConcreteType() == PxConcreteType::eRIGID_DYNAMIC) && (getScbBodyFast().getFlags() & PxRigidBodyFlag::eKINEMATIC);
	}

protected:
					Scb::Body 			mBody;
};


template<class APIClass>
NpRigidBodyTemplate<APIClass>::NpRigidBodyTemplate(PxType concreteType, PxBaseFlags baseFlags, PxActorType::Enum type, const PxTransform& bodyPose)
:	RigidActorTemplateClass(concreteType, baseFlags)
,	mBody(type, bodyPose)
{
}


template<class APIClass>
NpRigidBodyTemplate<APIClass>::~NpRigidBodyTemplate()
{
}

namespace
{
	PX_FORCE_INLINE static bool isSimGeom(PxGeometryType::Enum t)
	{
		return t != PxGeometryType::eTRIANGLEMESH && t != PxGeometryType::ePLANE && t != PxGeometryType::eHEIGHTFIELD;
	}
}

template<class APIClass>
PxShape* NpRigidBodyTemplate<APIClass>::createShape(const PxGeometry& geometry, PxMaterial*const* materials, PxU16 materialCount, PxShapeFlags shapeFlags)
{
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	PX_CHECK_AND_RETURN_NULL(!(shapeFlags & PxShapeFlag::eSIMULATION_SHAPE)
							 || isSimGeom(geometry.getType()) 
							 || isKinematic(),
							 "createShape: Triangle mesh, heightfield or plane geometry shapes configured as eSIMULATION_SHAPE are not supported for non-kinematic PxRigidDynamic instances.");

	NpShape* shape = static_cast<NpShape*>(NpPhysics::getInstance().createShape(geometry, materials, materialCount, true, shapeFlags));

	if ( shape != NULL )
	{
		RigidActorTemplateClass::mShapeManager.attachShape(*shape, *this);
		shape->releaseInternal();
	}
	return shape;
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::attachShape(PxShape& shape)
{
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	PX_CHECK_AND_RETURN(!(shape.getFlags() & PxShapeFlag::eSIMULATION_SHAPE) 
						|| isSimGeom(shape.getGeometryType()) 
						|| isKinematic(),
						"attachShape: Triangle mesh, heightfield or plane geometry shapes configured as eSIMULATION_SHAPE are not supported for non-kinematic PxRigidDynamic instances.");
	RigidActorTemplateClass::attachShape(shape);
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setCMassLocalPoseInternal(const PxTransform& body2Actor)
{
	//the point here is to change the mass distribution w/o changing the actors' pose in the world

	PxTransform newBody2World = getGlobalPose() * body2Actor;

	mBody.setBody2World(newBody2World, true);
	mBody.setBody2Actor(body2Actor);

	RigidActorTemplateClass::updateShaderComs();
}


template<class APIClass>
PxTransform NpRigidBodyTemplate<APIClass>::getCMassLocalPose() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));

	return getScbBodyFast().getBody2Actor();
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setMass(PxReal mass)
{
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	PX_CHECK_AND_RETURN(PxIsFinite(mass), "PxRigidDynamic::setMass: invalid float");
	PX_CHECK_AND_RETURN(mass>=0, "PxRigidDynamic::setMass: mass must be non-negative!");
	PX_CHECK_AND_RETURN(this->getType() != PxActorType::eARTICULATION_LINK || mass > 0.0f, "PxRigidDynamic::setMassSpaceInertiaTensor: components must be > 0 for articualtions");

	getScbBodyFast().setInverseMass(mass > 0.0f ? 1.0f/mass : 0.0f);
}


template<class APIClass>
PxReal NpRigidBodyTemplate<APIClass>::getMass() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));
	const PxReal invMass = mBody.getInverseMass();

	return invMass > 0.0f ? 1.0f/invMass : 0.0f;
}

template<class APIClass>
PxReal NpRigidBodyTemplate<APIClass>::getInvMass() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));

	return mBody.getInverseMass();
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setMassSpaceInertiaTensor(const PxVec3& m)
{
 	PX_CHECK_AND_RETURN(m.isFinite(), "PxRigidDynamic::setMassSpaceInertiaTensor: invalid inertia");
	PX_CHECK_AND_RETURN(m.x>=0.0f && m.y>=0.0f && m.z>=0.0f, "PxRigidDynamic::setMassSpaceInertiaTensor: components must be non-negative");
	PX_CHECK_AND_RETURN(this->getType() != PxActorType::eARTICULATION_LINK || (m.x > 0.0f && m.y > 0.0f && m.z > 0.0f), "PxRigidDynamic::setMassSpaceInertiaTensor: components must be > 0 for articualtions");

	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	
	mBody.setInverseInertia(invertDiagInertia(m));
}


template<class APIClass>
PxVec3 NpRigidBodyTemplate<APIClass>::getMassSpaceInertiaTensor() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));

	return invertDiagInertia(mBody.getInverseInertia());
}

template<class APIClass>
PxVec3 NpRigidBodyTemplate<APIClass>::getMassSpaceInvInertiaTensor() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));

	return mBody.getInverseInertia();
}


template<class APIClass>
PxVec3 NpRigidBodyTemplate<APIClass>::getLinearVelocity() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));

	return mBody.getLinearVelocity();
}


template<class APIClass>
PxVec3 NpRigidBodyTemplate<APIClass>::getAngularVelocity() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));

	return mBody.getAngularVelocity();
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::addSpatialForce(const PxVec3* force, const PxVec3* torque, PxForceMode::Enum mode)
{
	PX_ASSERT(!(mBody.getFlags() & PxRigidBodyFlag::eKINEMATIC));

	switch (mode)
	{
		case PxForceMode::eFORCE:
		{
			PxVec3 linAcc, angAcc;
			if (force)
			{
				linAcc = (*force) * mBody.getInverseMass();
				force = &linAcc;
			}
			if (torque)
			{
				angAcc = mBody.getGlobalInertiaTensorInverse() * (*torque);
				torque = &angAcc;
			}
			mBody.addSpatialAcceleration(force, torque);
		}
		break;

		case PxForceMode::eACCELERATION:
			mBody.addSpatialAcceleration(force, torque);
		break;

		case PxForceMode::eIMPULSE:
		{
			PxVec3 linVelDelta, angVelDelta;
			if (force)
			{
				linVelDelta = ((*force) * mBody.getInverseMass());
				force = &linVelDelta;
			}
			if (torque)
			{
				angVelDelta = (mBody.getGlobalInertiaTensorInverse() * (*torque));
				torque = &angVelDelta;
			}
			mBody.addSpatialVelocity(force, torque);
		}
		break;

		case PxForceMode::eVELOCITY_CHANGE:
			mBody.addSpatialVelocity(force, torque);
		break;
	}
}

template<class APIClass>
void NpRigidBodyTemplate<APIClass>::clearSpatialForce(PxForceMode::Enum mode, bool force, bool torque)
{
	PX_ASSERT(!(mBody.getFlags() & PxRigidBodyFlag::eKINEMATIC));

	switch (mode)
	{
	case PxForceMode::eFORCE:
	case PxForceMode::eACCELERATION:
		mBody.clearSpatialAcceleration(force, torque);
		break;
	case PxForceMode::eIMPULSE:
	case PxForceMode::eVELOCITY_CHANGE:
		mBody.clearSpatialVelocity(force, torque);
		break;
	}
}


#if PX_ENABLE_DEBUG_VISUALIZATION
template<class APIClass>
void NpRigidBodyTemplate<APIClass>::visualize(Cm::RenderOutput& out, NpScene* scene)
{
	RigidActorTemplateClass::visualize(out, scene);

	if (mBody.getActorFlags() & PxActorFlag::eVISUALIZATION)
	{
		Scb::Scene& scbScene = scene->getScene();
		const PxReal scale = scbScene.getVisualizationParameter(PxVisualizationParameter::eSCALE);

		//visualize actor frames
		const PxReal actorAxes = scale * scbScene.getVisualizationParameter(PxVisualizationParameter::eACTOR_AXES);
		if (actorAxes != 0.0f)
			out << getGlobalPose() << Cm::DebugBasis(PxVec3(actorAxes));

		const PxReal bodyAxes = scale * scbScene.getVisualizationParameter(PxVisualizationParameter::eBODY_AXES);
		if (bodyAxes != 0.0f)
			out << mBody.getBody2World() << Cm::DebugBasis(PxVec3(bodyAxes));

		const PxReal linVelocity = scale * scbScene.getVisualizationParameter(PxVisualizationParameter::eBODY_LIN_VELOCITY);
		if (linVelocity != 0.0f)
		{
			out << 0xffffff << PxMat44(PxIdentity) << Cm::DebugArrow(mBody.getBody2World().p, 
				mBody.getLinearVelocity() * linVelocity, 0.2f * linVelocity);
		}

		const PxReal angVelocity = scale * scbScene.getVisualizationParameter(PxVisualizationParameter::eBODY_ANG_VELOCITY);
		if (angVelocity != 0.0f)
		{
			out << 0x000000 << PxMat44(PxIdentity) << Cm::DebugArrow(mBody.getBody2World().p, 
				mBody.getAngularVelocity() * angVelocity, 0.2f * angVelocity);
		}
	}
}
#endif


PX_FORCE_INLINE void updateDynamicSceneQueryShapes(NpShapeManager& shapeManager, Sq::SceneQueryManager& sqManager)
{
	shapeManager.markAllSceneQueryForUpdate(sqManager);
	sqManager.get(Sq::PruningIndex::eDYNAMIC).invalidateTimestamp();
}


template<class APIClass>
PX_FORCE_INLINE void NpRigidBodyTemplate<APIClass>::setRigidBodyFlagsInternal(const PxRigidBodyFlags& currentFlags, const PxRigidBodyFlags& newFlags)
{
	PxRigidBodyFlags filteredNewFlags = newFlags;
	//Test to ensure we are not enabling both CCD and kinematic state on a body. This is unsupported
	if((filteredNewFlags & PxRigidBodyFlag::eENABLE_CCD) && (filteredNewFlags & PxRigidBodyFlag::eKINEMATIC))
	{
		physx::shdfnd::getFoundation().error(physx::PxErrorCode::eINVALID_PARAMETER, __FILE__, __LINE__, 
			"RigidBody::setRigidBodyFlag: kinematic bodies with CCD enabled are not supported! CCD will be ignored.");
		filteredNewFlags &= PxRigidBodyFlags(~PxRigidBodyFlag::eENABLE_CCD);
	}

	if ((filteredNewFlags & PxRigidBodyFlag::eENABLE_CCD) && (filteredNewFlags & PxRigidBodyFlag::eENABLE_SPECULATIVE_CCD))
	{
		physx::shdfnd::getFoundation().error(physx::PxErrorCode::eINVALID_PARAMETER, __FILE__, __LINE__,
			"RigidBody::setRigidBodyFlag: eENABLE_CCD can't be raised as the same time as eENABLE_SPECULATIVE_CCD! eENABLE_SPECULATIVE_CCD will be ignored.");
		filteredNewFlags &= PxRigidBodyFlags(~PxRigidBodyFlag::eENABLE_SPECULATIVE_CCD);
	}

	Scb::Body& body = getScbBodyFast();
	NpScene* scene = NpActor::getAPIScene(*this);

	const bool isKinematic = currentFlags & PxRigidBodyFlag::eKINEMATIC;
	const bool willBeKinematic = filteredNewFlags & PxRigidBodyFlag::eKINEMATIC;
	const bool kinematicSwitchingToDynamic = isKinematic && (!willBeKinematic);
	const bool dynamicSwitchingToKinematic = (!isKinematic) && willBeKinematic;

	if(kinematicSwitchingToDynamic)
	{
		NpShapeManager& shapeManager = this->getShapeManager();
		PxU32 nbShapes = shapeManager.getNbShapes();
		NpShape*const* shapes = shapeManager.getShapes();
		bool hasTriangleMesh = false;
		for(PxU32 i=0;i<nbShapes;i++)
		{
			if((shapes[i]->getFlags() & PxShapeFlag::eSIMULATION_SHAPE) && (shapes[i]->getGeometryTypeFast()==PxGeometryType::eTRIANGLEMESH || shapes[i]->getGeometryTypeFast()==PxGeometryType::ePLANE || shapes[i]->getGeometryTypeFast()==PxGeometryType::eHEIGHTFIELD))
			{
				hasTriangleMesh = true;
				break;
			}
		}
		if(hasTriangleMesh)
		{
			physx::shdfnd::getFoundation().error(physx::PxErrorCode::eINVALID_PARAMETER, __FILE__, __LINE__, "RigidBody::setRigidBodyFlag: dynamic meshes/planes/heightfields are not supported!");
			return;
		}

		PxTransform bodyTarget;
		if ((currentFlags & PxRigidBodyFlag::eUSE_KINEMATIC_TARGET_FOR_SCENE_QUERIES) && body.getKinematicTarget(bodyTarget) && scene)
		{
			updateDynamicSceneQueryShapes(shapeManager, scene->getSceneQueryManagerFast());
		}

		body.clearSimStateDataForPendingInsert();
	}
	else if (dynamicSwitchingToKinematic)
	{
		if (this->getType() != PxActorType::eARTICULATION_LINK)
		{
			body.transitionSimStateDataForPendingInsert();
		}
		else
		{
			//We're an articulation, raise an issue
			physx::shdfnd::getFoundation().error(physx::PxErrorCode::eINVALID_PARAMETER, __FILE__, __LINE__, "RigidBody::setRigidBodyFlag: kinematic articulation links are not supported!");
			return;
		}
	}

	const bool kinematicSwitchingUseTargetForSceneQuery = isKinematic && willBeKinematic && 
														((currentFlags & PxRigidBodyFlag::eUSE_KINEMATIC_TARGET_FOR_SCENE_QUERIES) != (filteredNewFlags & PxRigidBodyFlag::eUSE_KINEMATIC_TARGET_FOR_SCENE_QUERIES));
	if (kinematicSwitchingUseTargetForSceneQuery)
	{
		PxTransform bodyTarget;
		if (body.getKinematicTarget(bodyTarget) && scene)
		{
			updateDynamicSceneQueryShapes(this->getShapeManager(), scene->getSceneQueryManagerFast());
		}
	}

	body.setFlags(filteredNewFlags);
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setRigidBodyFlag(PxRigidBodyFlag::Enum flag, bool value)
{
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));

	Scb::Body& body = getScbBodyFast();
	const PxRigidBodyFlags currentFlags = body.getFlags();
	const PxRigidBodyFlags newFlags = value ? currentFlags | flag : currentFlags & (~PxRigidBodyFlags(flag));

	setRigidBodyFlagsInternal(currentFlags, newFlags);
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setRigidBodyFlags(PxRigidBodyFlags inFlags)
{
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));

	Scb::Body& body = getScbBodyFast();
	const PxRigidBodyFlags currentFlags = body.getFlags();

	setRigidBodyFlagsInternal(currentFlags, inFlags);
}


template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setMinCCDAdvanceCoefficient(PxReal minCCDAdvanceCoefficient)
{
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	mBody.setMinCCDAdvanceCoefficient(minCCDAdvanceCoefficient);
}

template<class APIClass>
PxReal NpRigidBodyTemplate<APIClass>::getMinCCDAdvanceCoefficient() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));
	return mBody.getMinCCDAdvanceCoefficient();
}

template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setMaxDepenetrationVelocity(PxReal maxDepenVel)
{
	PX_CHECK_AND_RETURN(maxDepenVel > 0.0f, "PxRigidDynamic::setMaxDepenetrationVelocity: maxDepenVel must be greater than zero.");
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	mBody.setMaxPenetrationBias(-maxDepenVel);
}

template<class APIClass>
PxReal NpRigidBodyTemplate<APIClass>::getMaxDepenetrationVelocity() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));
	return -mBody.getMaxPenetrationBias();
}

template<class APIClass>
void NpRigidBodyTemplate<APIClass>::setMaxContactImpulse(const PxReal maxImpulse)
{
	PX_CHECK_AND_RETURN(maxImpulse >= 0.f, "NpRigidBody::setMaxImpulse: impulse limit must be greater than or equal to zero.");
	NP_WRITE_CHECK(NpActor::getOwnerScene(*this));
	mBody.setMaxContactImpulse(maxImpulse);
}

template<class APIClass>
PxReal NpRigidBodyTemplate<APIClass>::getMaxContactImpulse() const
{
	NP_READ_CHECK(NpActor::getOwnerScene(*this));
	return mBody.getMaxContactImpulse();
}



}

#endif