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
|
//
// 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.
#include <new>
#include "SnippetVehicleCreate.h"
#include "SnippetVehicleSceneQuery.h"
#include "SnippetVehicleFilterShader.h"
#include "SnippetVehicleTireFriction.h"
#include "PxPhysicsAPI.h"
namespace snippetvehicle
{
using namespace physx;
PxRigidStatic* createDrivablePlane(const PxFilterData& simFilterData, PxMaterial* material, PxPhysics* physics)
{
//Add a plane to the scene.
PxRigidStatic* groundPlane = PxCreatePlane(*physics, PxPlane(0,1,0,0), *material);
//Get the plane shape so we can set query and simulation filter data.
PxShape* shapes[1];
groundPlane->getShapes(shapes, 1);
//Set the query filter data of the ground plane so that the vehicle raycasts can hit the ground.
PxFilterData qryFilterData;
setupDrivableSurface(qryFilterData);
shapes[0]->setQueryFilterData(qryFilterData);
//Set the simulation filter data of the ground plane so that it collides with the chassis of a vehicle but not the wheels.
shapes[0]->setSimulationFilterData(simFilterData);
return groundPlane;
}
static PxConvexMesh* createConvexMesh(const PxVec3* verts, const PxU32 numVerts, PxPhysics& physics, PxCooking& cooking)
{
// Create descriptor for convex mesh
PxConvexMeshDesc convexDesc;
convexDesc.points.count = numVerts;
convexDesc.points.stride = sizeof(PxVec3);
convexDesc.points.data = verts;
convexDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX;
PxConvexMesh* convexMesh = NULL;
PxDefaultMemoryOutputStream buf;
if(cooking.cookConvexMesh(convexDesc, buf))
{
PxDefaultMemoryInputData id(buf.getData(), buf.getSize());
convexMesh = physics.createConvexMesh(id);
}
return convexMesh;
}
PxConvexMesh* createChassisMesh(const PxVec3 dims, PxPhysics& physics, PxCooking& cooking)
{
const PxF32 x = dims.x*0.5f;
const PxF32 y = dims.y*0.5f;
const PxF32 z = dims.z*0.5f;
PxVec3 verts[8] =
{
PxVec3(x,y,-z),
PxVec3(x,y,z),
PxVec3(x,-y,z),
PxVec3(x,-y,-z),
PxVec3(-x,y,-z),
PxVec3(-x,y,z),
PxVec3(-x,-y,z),
PxVec3(-x,-y,-z)
};
return createConvexMesh(verts,8,physics,cooking);
}
PxConvexMesh* createWheelMesh(const PxF32 width, const PxF32 radius, PxPhysics& physics, PxCooking& cooking)
{
PxVec3 points[2*16];
for(PxU32 i = 0; i < 16; i++)
{
const PxF32 cosTheta = PxCos(i*PxPi*2.0f/16.0f);
const PxF32 sinTheta = PxSin(i*PxPi*2.0f/16.0f);
const PxF32 y = radius*cosTheta;
const PxF32 z = radius*sinTheta;
points[2*i+0] = PxVec3(-width/2.0f, y, z);
points[2*i+1] = PxVec3(+width/2.0f, y, z);
}
return createConvexMesh(points,32,physics,cooking);
}
PxRigidDynamic* createVehicleActor
(const PxVehicleChassisData& chassisData,
PxMaterial** wheelMaterials, PxConvexMesh** wheelConvexMeshes, const PxU32 numWheels, const PxFilterData& wheelSimFilterData,
PxMaterial** chassisMaterials, PxConvexMesh** chassisConvexMeshes, const PxU32 numChassisMeshes, const PxFilterData& chassisSimFilterData,
PxPhysics& physics)
{
//We need a rigid body actor for the vehicle.
//Don't forget to add the actor to the scene after setting up the associated vehicle.
PxRigidDynamic* vehActor = physics.createRigidDynamic(PxTransform(PxIdentity));
//Wheel and chassis query filter data.
//Optional: cars don't drive on other cars.
PxFilterData wheelQryFilterData;
setupNonDrivableSurface(wheelQryFilterData);
PxFilterData chassisQryFilterData;
setupNonDrivableSurface(chassisQryFilterData);
//Add all the wheel shapes to the actor.
for(PxU32 i = 0; i < numWheels; i++)
{
PxConvexMeshGeometry geom(wheelConvexMeshes[i]);
PxShape* wheelShape=PxRigidActorExt::createExclusiveShape(*vehActor, geom, *wheelMaterials[i]);
wheelShape->setQueryFilterData(wheelQryFilterData);
wheelShape->setSimulationFilterData(wheelSimFilterData);
wheelShape->setLocalPose(PxTransform(PxIdentity));
}
//Add the chassis shapes to the actor.
for(PxU32 i = 0; i < numChassisMeshes; i++)
{
PxShape* chassisShape=PxRigidActorExt::createExclusiveShape(*vehActor, PxConvexMeshGeometry(chassisConvexMeshes[i]), *chassisMaterials[i]);
chassisShape->setQueryFilterData(chassisQryFilterData);
chassisShape->setSimulationFilterData(chassisSimFilterData);
chassisShape->setLocalPose(PxTransform(PxIdentity));
}
vehActor->setMass(chassisData.mMass);
vehActor->setMassSpaceInertiaTensor(chassisData.mMOI);
vehActor->setCMassLocalPose(PxTransform(chassisData.mCMOffset,PxQuat(PxIdentity)));
return vehActor;
}
void configureUserData(PxVehicleWheels* vehicle, ActorUserData* actorUserData, ShapeUserData* shapeUserDatas)
{
if(actorUserData)
{
vehicle->getRigidDynamicActor()->userData = actorUserData;
actorUserData->vehicle = vehicle;
}
if(shapeUserDatas)
{
PxShape* shapes[PX_MAX_NB_WHEELS + 1];
vehicle->getRigidDynamicActor()->getShapes(shapes, PX_MAX_NB_WHEELS + 1);
for(PxU32 i = 0; i < vehicle->mWheelsSimData.getNbWheels(); i++)
{
const PxI32 shapeId = vehicle->mWheelsSimData.getWheelShapeMapping(i);
shapes[shapeId]->userData = &shapeUserDatas[i];
shapeUserDatas[i].isWheel = true;
shapeUserDatas[i].wheelId = i;
}
}
}
void customizeVehicleToLengthScale(const PxReal lengthScale, PxRigidDynamic* rigidDynamic, PxVehicleWheelsSimData* wheelsSimData, PxVehicleDriveSimData* driveSimData)
{
//Rigid body center of mass and moment of inertia.
{
PxTransform t = rigidDynamic->getCMassLocalPose();
t.p *= lengthScale;
rigidDynamic->setCMassLocalPose(t);
PxVec3 moi = rigidDynamic->getMassSpaceInertiaTensor();
moi *= (lengthScale*lengthScale);
rigidDynamic->setMassSpaceInertiaTensor(moi);
}
//Wheels, suspensions, wheel centers, tire/susp force application points.
{
for(PxU32 i = 0; i < wheelsSimData->getNbWheels(); i++)
{
PxVehicleWheelData wheelData = wheelsSimData->getWheelData(i);
wheelData.mRadius *= lengthScale;
wheelData.mWidth *= lengthScale;
wheelData.mDampingRate *= lengthScale*lengthScale;
wheelData.mMaxBrakeTorque *= lengthScale*lengthScale;
wheelData.mMaxHandBrakeTorque *= lengthScale*lengthScale;
wheelData.mMOI *= lengthScale*lengthScale;
wheelsSimData->setWheelData(i, wheelData);
PxVehicleSuspensionData suspData = wheelsSimData->getSuspensionData(i);
suspData.mMaxCompression *= lengthScale;
suspData.mMaxDroop *= lengthScale;
wheelsSimData->setSuspensionData(i, suspData);
PxVec3 v = wheelsSimData->getWheelCentreOffset(i);
v *= lengthScale;
wheelsSimData->setWheelCentreOffset(i,v);
v = wheelsSimData->getSuspForceAppPointOffset(i);
v *= lengthScale;
wheelsSimData->setSuspForceAppPointOffset(i,v);
v = wheelsSimData->getTireForceAppPointOffset(i);
v *= lengthScale;
wheelsSimData->setTireForceAppPointOffset(i,v);
}
}
//Slow forward speed correction.
{
wheelsSimData->setSubStepCount(5.0f*lengthScale, 3, 1);
wheelsSimData->setMinLongSlipDenominator(4.0f*lengthScale);
}
//Engine
if(driveSimData)
{
PxVehicleEngineData engineData = driveSimData->getEngineData();
engineData.mMOI *= lengthScale*lengthScale;
engineData.mPeakTorque *= lengthScale*lengthScale;
engineData.mDampingRateFullThrottle *= lengthScale*lengthScale;
engineData.mDampingRateZeroThrottleClutchEngaged *= lengthScale*lengthScale;
engineData.mDampingRateZeroThrottleClutchDisengaged *= lengthScale*lengthScale;
driveSimData->setEngineData(engineData);
}
//Clutch.
if(driveSimData)
{
PxVehicleClutchData clutchData = driveSimData->getClutchData();
clutchData.mStrength *= lengthScale*lengthScale;
driveSimData->setClutchData(clutchData);
}
//Scale the collision meshes too.
{
PxShape* shapes[16];
const PxU32 nbShapes = rigidDynamic->getShapes(shapes, 16);
for(PxU32 i = 0; i < nbShapes; i++)
{
switch(shapes[i]->getGeometryType())
{
case PxGeometryType::eSPHERE:
{
PxSphereGeometry sphere;
shapes[i]->getSphereGeometry(sphere);
sphere.radius *= lengthScale;
shapes[i]->setGeometry(sphere);
}
break;
case PxGeometryType::ePLANE:
PX_ASSERT(false);
break;
case PxGeometryType::eCAPSULE:
{
PxCapsuleGeometry capsule;
shapes[i]->getCapsuleGeometry(capsule);
capsule.radius *= lengthScale;
capsule.halfHeight*= lengthScale;
shapes[i]->setGeometry(capsule);
}
break;
case PxGeometryType::eBOX:
{
PxBoxGeometry box;
shapes[i]->getBoxGeometry(box);
box.halfExtents *= lengthScale;
shapes[i]->setGeometry(box);
}
break;
case PxGeometryType::eCONVEXMESH:
{
PxConvexMeshGeometry convexMesh;
shapes[i]->getConvexMeshGeometry(convexMesh);
convexMesh.scale.scale *= lengthScale;
shapes[i]->setGeometry(convexMesh);
}
break;
case PxGeometryType::eTRIANGLEMESH:
{
PxTriangleMeshGeometry triMesh;
shapes[i]->getTriangleMeshGeometry(triMesh);
triMesh.scale.scale *= lengthScale;
shapes[i]->setGeometry(triMesh);
}
break;
case PxGeometryType::eHEIGHTFIELD:
{
PxHeightFieldGeometry hf;
shapes[i]->getHeightFieldGeometry(hf);
hf.columnScale *= lengthScale;
hf.heightScale *= lengthScale;
hf.rowScale *= lengthScale;
shapes[i]->setGeometry(hf);
}
break;
case PxGeometryType::eINVALID:
case PxGeometryType::eGEOMETRY_COUNT:
break;
}
}
}
}
void customizeVehicleToLengthScale(const PxReal lengthScale, PxRigidDynamic* rigidDynamic, PxVehicleWheelsSimData* wheelsSimData, PxVehicleDriveSimData4W* driveSimData)
{
customizeVehicleToLengthScale(lengthScale, rigidDynamic, wheelsSimData, static_cast<PxVehicleDriveSimData*>(driveSimData));
//Ackermann geometry.
if(driveSimData)
{
PxVehicleAckermannGeometryData ackermannData = driveSimData->getAckermannGeometryData();
ackermannData.mAxleSeparation *= lengthScale;
ackermannData.mFrontWidth *= lengthScale;
ackermannData.mRearWidth *= lengthScale;
driveSimData->setAckermannGeometryData(ackermannData);
}
}
} // namespace snippetvehicle
|