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
|
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2008-2017 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#include "foundation/PxMathUtils.h"
#include "foundation/PxQuat.h"
#include "CmPhysXCommon.h"
#include "PsFoundation.h"
#include "PsUtilities.h"
#include "PsInlineArray.h"
#include "PxSimpleFactory.h"
#include "PxRigidStatic.h"
#include "PxSphereGeometry.h"
#include "PxBoxGeometry.h"
#include "PxCapsuleGeometry.h"
#include "PxConvexMeshGeometry.h"
#include "PxPlaneGeometry.h"
#include "PxRigidBodyExt.h"
#include "PxRigidStatic.h"
#include "PxScene.h"
#include "PxShape.h"
#include "PxRigidDynamic.h"
#include "CmPhysXCommon.h"
#include "PxPhysics.h"
using namespace physx;
using namespace shdfnd;
namespace
{
bool isDynamicGeometry(PxGeometryType::Enum type)
{
return type == PxGeometryType::eBOX
|| type == PxGeometryType::eSPHERE
|| type == PxGeometryType::eCAPSULE
|| type == PxGeometryType::eCONVEXMESH;
}
}
namespace physx
{
PxRigidDynamic* PxCreateDynamic(PxPhysics& sdk,
const PxTransform& transform,
PxShape& shape,
PxReal density)
{
PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateDynamic: transform is not valid.");
PxRigidDynamic* actor = sdk.createRigidDynamic(transform);
if(actor)
{
actor->attachShape(shape);
PxRigidBodyExt::updateMassAndInertia(*actor, density);
}
return actor;
}
PxRigidDynamic* PxCreateDynamic(PxPhysics& sdk,
const PxTransform& transform,
const PxGeometry& geometry,
PxMaterial& material,
PxReal density,
const PxTransform& shapeOffset)
{
PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateDynamic: transform is not valid.");
PX_CHECK_AND_RETURN_NULL(shapeOffset.isValid(), "PxCreateDynamic: shapeOffset is not valid.");
if(!isDynamicGeometry(geometry.getType()) || density <= 0.0f)
return NULL;
PxShape* shape = sdk.createShape(geometry, material, true);
if(!shape)
return NULL;
shape->setLocalPose(shapeOffset);
PxRigidDynamic* body = shape ? PxCreateDynamic(sdk, transform, *shape, density) : NULL;
shape->release();
return body;
}
PxRigidDynamic* PxCreateKinematic(PxPhysics& sdk,
const PxTransform& transform,
PxShape& shape,
PxReal density)
{
PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateKinematic: transform is not valid.");
bool isDynGeom = isDynamicGeometry(shape.getGeometryType());
if(isDynGeom && density <= 0.0f)
return NULL;
PxRigidDynamic* actor = sdk.createRigidDynamic(transform);
if(actor)
{
actor->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true);
if(!isDynGeom)
shape.setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
actor->attachShape(shape);
if(isDynGeom)
PxRigidBodyExt::updateMassAndInertia(*actor, density);
else
{
actor->setMass(1.f);
actor->setMassSpaceInertiaTensor(PxVec3(1.f,1.f,1.f));
}
}
return actor;
}
PxRigidDynamic* PxCreateKinematic(PxPhysics& sdk,
const PxTransform& transform,
const PxGeometry& geometry,
PxMaterial& material,
PxReal density,
const PxTransform& shapeOffset)
{
PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateKinematic: transform is not valid.");
PX_CHECK_AND_RETURN_NULL(shapeOffset.isValid(), "PxCreateKinematic: shapeOffset is not valid.");
bool isDynGeom = isDynamicGeometry(geometry.getType());
if(isDynGeom && density <= 0.0f)
return NULL;
PxShape* shape = sdk.createShape(geometry, material, true);
if(!shape)
return NULL;
shape->setLocalPose(shapeOffset);
PxRigidDynamic* body = PxCreateKinematic(sdk, transform, *shape, density);
shape->release();
return body;
}
PxRigidStatic* PxCreateStatic(PxPhysics& sdk,
const PxTransform& transform,
PxShape& shape)
{
PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateStatic: transform is not valid.");
PxRigidStatic* s = sdk.createRigidStatic(transform);
if(s)
s->attachShape(shape);
return s;
}
PxRigidStatic* PxCreateStatic(PxPhysics& sdk,
const PxTransform& transform,
const PxGeometry& geometry,
PxMaterial& material,
const PxTransform& shapeOffset)
{
PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateStatic: transform is not valid.");
PX_CHECK_AND_RETURN_NULL(shapeOffset.isValid(), "PxCreateStatic: shapeOffset is not valid.");
PxShape* shape = sdk.createShape(geometry, material, true);
if(!shape)
return NULL;
shape->setLocalPose(shapeOffset);
PxRigidStatic* s = PxCreateStatic(sdk, transform, *shape);
shape->release();
return s;
}
PxRigidStatic* PxCreatePlane(PxPhysics& sdk,
const PxPlane& plane,
PxMaterial& material)
{
PX_CHECK_AND_RETURN_NULL(plane.n.isFinite(), "PxCreatePlane: plane normal is not valid.");
if (!plane.n.isNormalized())
return NULL;
return PxCreateStatic(sdk, PxTransformFromPlaneEquation(plane), PxPlaneGeometry(), material);
}
PxShape* PxCloneShape(PxPhysics &physics, const PxShape& from, bool isExclusive)
{
Ps::InlineArray<PxMaterial*, 64> materials;
PxU16 materialCount = from.getNbMaterials();
materials.resize(materialCount);
from.getMaterials(materials.begin(), materialCount);
PxShape* to = physics.createShape(from.getGeometry().any(), materials.begin(), materialCount, isExclusive, from.getFlags());
to->setLocalPose(from.getLocalPose());
to->setContactOffset(from.getContactOffset());
to->setRestOffset(from.getRestOffset());
to->setSimulationFilterData(from.getSimulationFilterData());
to->setQueryFilterData(from.getQueryFilterData());
return to;
}
namespace
{
void copyStaticProperties(PxPhysics& physics, PxRigidActor& to, const PxRigidActor& from)
{
Ps::InlineArray<PxShape*, 64> shapes;
shapes.resize(from.getNbShapes());
PxU32 shapeCount = from.getNbShapes();
from.getShapes(shapes.begin(), shapeCount);
for(PxU32 i = 0; i < shapeCount; i++)
{
PxShape* s = shapes[i];
if(!s->isExclusive())
to.attachShape(*s);
else
{
PxShape* newShape = physx::PxCloneShape(physics, *s, true);
to.attachShape(*newShape);
newShape->release();
}
}
to.setActorFlags(from.getActorFlags());
to.setOwnerClient(from.getOwnerClient());
to.setClientBehaviorFlags(from.getClientBehaviorFlags());
to.setDominanceGroup(from.getDominanceGroup());
}
}
PxRigidStatic* PxCloneStatic(PxPhysics& physicsSDK,
const PxTransform& transform,
const PxRigidActor& from)
{
PxRigidStatic* to = physicsSDK.createRigidStatic(transform);
if(!to)
return NULL;
copyStaticProperties(physicsSDK, *to, from);
return to;
}
PxRigidDynamic* PxCloneDynamic(PxPhysics& physicsSDK,
const PxTransform& transform,
const PxRigidDynamic& from)
{
PxRigidDynamic* to = physicsSDK.createRigidDynamic(transform);
if(!to)
return NULL;
copyStaticProperties(physicsSDK, *to, from);
to->setRigidBodyFlags(from.getRigidBodyFlags());
to->setMass(from.getMass());
to->setMassSpaceInertiaTensor(from.getMassSpaceInertiaTensor());
to->setCMassLocalPose(from.getCMassLocalPose());
to->setLinearVelocity(from.getLinearVelocity());
to->setAngularVelocity(from.getAngularVelocity());
to->setLinearDamping(from.getAngularDamping());
to->setAngularDamping(from.getAngularDamping());
PxU32 posIters, velIters;
from.getSolverIterationCounts(posIters, velIters);
to->setSolverIterationCounts(posIters, velIters);
to->setMaxAngularVelocity(from.getMaxAngularVelocity());
to->setMaxDepenetrationVelocity(from.getMaxDepenetrationVelocity());
to->setSleepThreshold(from.getSleepThreshold());
to->setStabilizationThreshold(from.getStabilizationThreshold());
to->setMinCCDAdvanceCoefficient(from.getMinCCDAdvanceCoefficient());
to->setContactReportThreshold(from.getContactReportThreshold());
to->setMaxContactImpulse(from.getMaxContactImpulse());
return to;
}
namespace
{
PxTransform scalePosition(const PxTransform& t, PxReal scale)
{
return PxTransform(t.p*scale, t.q);
}
}
void PxScaleRigidActor(PxRigidActor& actor, PxReal scale, bool scaleMassProps)
{
PX_CHECK_AND_RETURN(scale > 0,
"PxScaleRigidActor requires that the scale parameter is greater than zero");
Ps::InlineArray<PxShape*, 64> shapes;
shapes.resize(actor.getNbShapes());
actor.getShapes(shapes.begin(), shapes.size());
for(PxU32 i=0;i<shapes.size();i++)
{
shapes[i]->setLocalPose(scalePosition(shapes[i]->getLocalPose(), scale));
PxGeometryHolder h = shapes[i]->getGeometry();
switch(h.getType())
{
case PxGeometryType::eSPHERE:
h.sphere().radius *= scale;
break;
case PxGeometryType::ePLANE:
break;
case PxGeometryType::eCAPSULE:
h.capsule().halfHeight *= scale;
h.capsule().radius *= scale;
break;
case PxGeometryType::eBOX:
h.box().halfExtents *= scale;
break;
case PxGeometryType::eCONVEXMESH:
h.convexMesh().scale.scale *= scale;
break;
case PxGeometryType::eTRIANGLEMESH:
h.triangleMesh().scale.scale *= scale;
break;
case PxGeometryType::eHEIGHTFIELD:
h.heightField().heightScale *= scale;
h.heightField().rowScale *= scale;
h.heightField().columnScale *= scale;
break;
case PxGeometryType::eINVALID:
case PxGeometryType::eGEOMETRY_COUNT:
PX_ASSERT(0);
}
shapes[i]->setGeometry(h.any());
}
if(!scaleMassProps)
return;
PxRigidDynamic* dynamic = (&actor)->is<PxRigidDynamic>();
if(!dynamic)
return;
PxReal scale3 = scale*scale*scale;
dynamic->setMass(dynamic->getMass()*scale3);
dynamic->setMassSpaceInertiaTensor(dynamic->getMassSpaceInertiaTensor()*scale3*scale*scale);
dynamic->setCMassLocalPose(scalePosition(dynamic->getCMassLocalPose(), scale));
}
}
|