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
|
//
// 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 "PxRaycastCCD.h"
using namespace physx;
#include "geometry/PxBoxGeometry.h"
#include "geometry/PxSphereGeometry.h"
#include "geometry/PxCapsuleGeometry.h"
#include "geometry/PxConvexMeshGeometry.h"
#include "geometry/PxConvexMesh.h"
#include "PxScene.h"
#include "PxRigidDynamic.h"
#include "extensions/PxShapeExt.h"
#include "PsArray.h"
namespace physx
{
class RaycastCCDManagerInternal
{
PX_NOCOPY(RaycastCCDManagerInternal)
public:
RaycastCCDManagerInternal(PxScene* scene) : mScene(scene) {}
~RaycastCCDManagerInternal(){}
bool registerRaycastCCDObject(PxRigidDynamic* actor, PxShape* shape);
void doRaycastCCD(bool doDynamicDynamicCCD);
struct CCDObject
{
PX_FORCE_INLINE CCDObject(PxRigidDynamic* actor, PxShape* shape, const PxVec3& witness) : mActor(actor), mShape(shape), mWitness(witness) {}
PxRigidDynamic* mActor;
PxShape* mShape;
PxVec3 mWitness;
};
private:
PxScene* mScene;
physx::shdfnd::Array<CCDObject> mObjects;
};
}
static PxVec3 getShapeCenter(PxShape* shape, const PxTransform& pose)
{
PxVec3 offset(0.0f);
if(shape->getGeometryType()==PxGeometryType::eCONVEXMESH)
{
PxConvexMeshGeometry geometry;
bool status = shape->getConvexMeshGeometry(geometry);
PX_UNUSED(status);
PX_ASSERT(status);
PxReal mass;
PxMat33 localInertia;
PxVec3 localCenterOfMass;
geometry.convexMesh->getMassInformation(mass, localInertia, localCenterOfMass);
offset += localCenterOfMass;
}
return pose.transform(offset);
}
static PX_FORCE_INLINE PxVec3 getShapeCenter(PxRigidActor* actor, PxShape* shape)
{
const PxTransform pose = PxShapeExt::getGlobalPose(*shape, *actor);
return getShapeCenter(shape, pose);
}
static PxReal computeInternalRadius(PxRigidActor* actor, PxShape* shape, const PxVec3& dir)
{
const PxBounds3 bounds = PxShapeExt::getWorldBounds(*shape, *actor);
const PxReal diagonal = (bounds.maximum - bounds.minimum).magnitude();
const PxReal offsetFromOrigin = diagonal * 2.0f;
PxTransform pose = PxShapeExt::getGlobalPose(*shape, *actor);
PxReal internalRadius = 0.0f;
const PxReal length = offsetFromOrigin*2.0f;
switch(shape->getGeometryType())
{
case PxGeometryType::eSPHERE:
{
PxSphereGeometry geometry;
bool status = shape->getSphereGeometry(geometry);
PX_UNUSED(status);
PX_ASSERT(status);
internalRadius = geometry.radius;
}
break;
case PxGeometryType::eBOX:
case PxGeometryType::eCAPSULE:
{
pose.p = PxVec3(0.0f);
const PxVec3 virtualOrigin = pose.p + dir * offsetFromOrigin;
PxRaycastHit hit;
PxU32 nbHits = PxGeometryQuery::raycast(virtualOrigin, -dir, shape->getGeometry().any(), pose, length, PxHitFlag::eDISTANCE, 1, &hit);
PX_UNUSED(nbHits);
PX_ASSERT(nbHits);
internalRadius = offsetFromOrigin - hit.distance;
}
break;
case PxGeometryType::eCONVEXMESH:
{
PxVec3 shapeCenter = getShapeCenter(shape, pose);
shapeCenter -= pose.p;
pose.p = PxVec3(0.0f);
const PxVec3 virtualOrigin = shapeCenter + dir * offsetFromOrigin;
PxRaycastHit hit;
PxU32 nbHits = PxGeometryQuery::raycast(virtualOrigin, -dir, shape->getGeometry().any(), pose, length, PxHitFlag::eDISTANCE, 1, &hit);
PX_UNUSED(nbHits);
PX_ASSERT(nbHits);
internalRadius = offsetFromOrigin - hit.distance;
}
break;
case PxGeometryType::ePLANE:
case PxGeometryType::eHEIGHTFIELD:
case PxGeometryType::eTRIANGLEMESH:
case PxGeometryType::eGEOMETRY_COUNT:
case PxGeometryType::eINVALID:
break;
}
return internalRadius;
}
class CCDRaycastFilterCallback : public PxQueryFilterCallback
{
public:
CCDRaycastFilterCallback(PxRigidActor* actor, PxShape* shape) : mActor(actor), mShape(shape){}
PxRigidActor* mActor;
PxShape* mShape;
virtual PxQueryHitType::Enum preFilter(const PxFilterData&, const PxShape* shape, const PxRigidActor* actor, PxHitFlags&)
{
if(mActor==actor && mShape==shape)
return PxQueryHitType::eNONE;
return PxQueryHitType::eBLOCK;
}
virtual PxQueryHitType::Enum postFilter(const PxFilterData&, const PxQueryHit&)
{
return PxQueryHitType::eNONE;
}
};
static bool CCDRaycast(PxScene* scene, PxRigidActor* actor, PxShape* shape, const PxVec3& origin, const PxVec3& unitDir, const PxReal distance, PxRaycastHit& hit, bool dyna_dyna)
{
const PxQueryFlags qf(dyna_dyna ? PxQueryFlags(PxQueryFlag::eSTATIC|PxQueryFlag::eDYNAMIC|PxQueryFlag::ePREFILTER) : PxQueryFlags(PxQueryFlag::eSTATIC));
const PxQueryFilterData filterData(PxFilterData(), qf);
CCDRaycastFilterCallback CB(actor, shape);
PxRaycastBuffer buf1;
scene->raycast(origin, unitDir, distance, buf1, PxHitFlag::eDISTANCE, filterData, &CB);
hit = buf1.block;
return buf1.hasBlock;
}
static PxRigidDynamic* canDoCCD(PxRigidActor& actor, PxShape* /*shape*/)
{
if(actor.getConcreteType()!=PxConcreteType::eRIGID_DYNAMIC)
return NULL; // PT: no need to do it for statics
PxRigidDynamic* dyna = static_cast<PxRigidDynamic*>(&actor);
const PxU32 nbShapes = dyna->getNbShapes();
if(nbShapes!=1)
return NULL; // PT: only works with simple actors for now
if(dyna->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC)
return NULL; // PT: no need to do it for kinematics
return dyna;
}
static bool doRaycastCCD(PxScene* scene, const RaycastCCDManagerInternal::CCDObject& object, PxTransform& newPose, PxVec3& newShapeCenter, bool dyna_dyna)
{
PxRigidDynamic* dyna = canDoCCD(*object.mActor, object.mShape);
if(!dyna)
return true;
bool updateCCDWitness = true;
const PxVec3 offset = newPose.p - newShapeCenter;
const PxVec3& origin = object.mWitness;
const PxVec3& dest = newShapeCenter;
PxVec3 dir = dest - origin;
const PxReal length = dir.magnitude();
if(length!=0.0f)
{
dir /= length;
const PxReal internalRadius = computeInternalRadius(object.mActor, object.mShape, dir);
PxRaycastHit hit;
if(internalRadius!=0.0f && CCDRaycast(scene, object.mActor, object.mShape, origin, dir, length, hit, dyna_dyna))
{
updateCCDWitness = false;
const PxReal radiusLimit = internalRadius * 0.75f;
if(hit.distance>radiusLimit)
{
newShapeCenter = origin + dir * (hit.distance - radiusLimit);
}
else
{
if(hit.actor->getConcreteType()==PxConcreteType::eRIGID_DYNAMIC)
return true;
newShapeCenter = origin;
}
newPose.p = offset + newShapeCenter;
const PxTransform shapeLocalPose = object.mShape->getLocalPose();
const PxTransform inverseShapeLocalPose = shapeLocalPose.getInverse();
const PxTransform newGlobalPose = newPose * inverseShapeLocalPose;
dyna->setGlobalPose(newGlobalPose);
}
}
return updateCCDWitness;
}
bool RaycastCCDManagerInternal::registerRaycastCCDObject(PxRigidDynamic* actor, PxShape* shape)
{
if(!actor || !shape)
return false;
mObjects.pushBack(CCDObject(actor, shape, getShapeCenter(actor, shape)));
return true;
}
void RaycastCCDManagerInternal::doRaycastCCD(bool doDynamicDynamicCCD)
{
const PxU32 nbObjects = mObjects.size();
for(PxU32 i=0;i<nbObjects;i++)
{
CCDObject& object = mObjects[i];
if(object.mActor->isSleeping())
continue;
PxTransform newPose = PxShapeExt::getGlobalPose(*object.mShape, *object.mActor);
PxVec3 newShapeCenter = getShapeCenter(object.mShape, newPose);
if(::doRaycastCCD(mScene, object, newPose, newShapeCenter, doDynamicDynamicCCD))
object.mWitness = newShapeCenter;
}
}
RaycastCCDManager::RaycastCCDManager(PxScene* scene)
{
mImpl = new RaycastCCDManagerInternal(scene);
}
RaycastCCDManager::~RaycastCCDManager()
{
delete mImpl;
}
bool RaycastCCDManager::registerRaycastCCDObject(PxRigidDynamic* actor, PxShape* shape)
{
return mImpl->registerRaycastCCDObject(actor, shape);
}
void RaycastCCDManager::doRaycastCCD(bool doDynamicDynamicCCD)
{
mImpl->doRaycastCCD(doDynamicDynamicCCD);
}
|