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
594
595
596
597
598
|
/*
* Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, 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.
*/
#ifndef __BASIC_IOS_COMMON_SRC_H__
#define __BASIC_IOS_COMMON_SRC_H__
namespace nvidia
{
namespace basicios
{
PX_CUDA_CALLABLE PX_INLINE void updateCollisionVelocity(const CollisionData& data, const PxVec3& normal, const PxVec3& position, PxVec3& velocity)
{
PxVec3 bodyVelocity = data.bodyLinearVelocity + data.bodyAngluarVelocity.cross(position - data.bodyCMassPosition);
velocity -= bodyVelocity;
float normalVelocity = normal.dot(velocity);
if (normalVelocity < 0.0f)
{
velocity -= normal * ((1.0f + data.materialRestitution) * normalVelocity);
}
velocity += bodyVelocity;
}
PX_CUDA_CALLABLE PX_INLINE void calculatePointSegmentSquaredDist(const PxVec3& a, const PxVec3& b, const PxVec3& point, float& distanceSquared, PxVec3& nearestPoint) // a, b - segment points
{
PxVec3 v, w, temp; //vectors
float c1, c2, ratio; //constants
v = a - b;
w = point - b;
float distSquared = 0;
c1 = w.dot(v);
if(c1 <= 0)
{
distSquared = (point.x - b.x) * (point.x - b.x) + (point.y - b.y) * (point.y - b.y) + (point.z - b.z) * (point.z - b.z);
nearestPoint = b;
}
else
{
c2 = v.dot(v);
if(c2 <= c1)
{
distSquared = (point.x - a.x) * (point.x - a.x) + (point.y - a.y) * (point.y - a.y) + (point.z - a.z) * (point.z - a.z);
nearestPoint = a;
}
else
{
ratio = c1 / c2;
temp = b + ratio * v;
distSquared = (point.x - temp.x) * (point.x - temp.x) + (point.y - temp.y) * (point.y - temp.y) + (point.z - temp.z) * (point.z - temp.z);
nearestPoint = temp;
}
}
distanceSquared = distSquared;
}
#ifdef __CUDACC__
__device__
#endif
PX_INLINE float checkTriangleCollision(const PxVec4* memTrimeshVerts, const uint32_t* memTrimeshIndices, const CollisionTriMeshData& data, float radius, PxVec3 localPosition, PxVec3 &normal)
{
float minDistSquared = PX_MAX_F32;
PxVec3 localNormal(0);
for(uint32_t j = 0 ; j < data.numTriangles; j++)
{
PxVec3 p0, p1, p2;
uint32_t i0, i1, i2;
i0 = SIM_FETCH(TrimeshIndices, data.firstIndex + 3 * j);
i1 = SIM_FETCH(TrimeshIndices, data.firstIndex + 3 * j + 1);
i2 = SIM_FETCH(TrimeshIndices, data.firstIndex + 3 * j + 2);
splitFloat4(p0, SIM_FETCH(TrimeshVerts, data.firstVertex + i0));
splitFloat4(p1, SIM_FETCH(TrimeshVerts, data.firstVertex + i1));
splitFloat4(p2, SIM_FETCH(TrimeshVerts, data.firstVertex + i2));
//localNormal = (p1 - p0).cross(p2 - p0);
//if(radius > 0) localNormal += localPosition;
PxBounds3 aabb( (p0.minimum(p1.minimum(p2))), (p0.maximum(p1.maximum(p2))) );
aabb.fattenFast( radius );
if( !aabb.contains(localPosition) ) continue;
p0 = p0 - localPosition;
p1 = p1 - localPosition;
p2 = p2 - localPosition;
PxVec3 a(p1 - p0);
PxVec3 b(p2 - p0);
PxVec3 n = a.cross(b);
n.normalize();
//check if point far away from the triangle's plane, then give up
if(n.x * p0.x + n.y * p0.y + n.z * p0.z > radius) continue;
//check if the nearest point is one of the triangle's vertices
PxVec3 closestPoint; // closest point
float det1p0p1, det2p0p2, det2p1p2, det0p0p1, det0p0p2, det1p1p2;
//i = 0
det1p0p1 = p0.dot(-(p1 - p0));
det2p0p2 = p0.dot(-(p2 - p0));
//i = 1
det0p0p1 = p1.dot(p1 - p0);
det2p1p2 = p1.dot(-(p2 - p1));
//i = 2
det0p0p2 = p2.dot(p2 - p0);
det1p1p2 = p2.dot(p2 - p1);
if(det1p0p1 <= 0 && det2p0p2 <= 0) closestPoint = p0;
else if(det0p0p1 <= 0 && det2p1p2 <= 0) closestPoint = p1;
else if(det0p0p2 <= 0 && det1p1p2 <= 0) closestPoint = p2;
else
{
//check if the nearest point is internal point of one of the triangle's edges
float det0p0p1p2, det1p0p1p2, det2p0p1p2;
det0p0p1p2 = det0p0p1 * det1p1p2 + det2p1p2 * p2.dot(p1 - p0);
det1p0p1p2 = det1p0p1 * det0p0p2 - det2p0p2 * p2.dot(p1 - p0);
det2p0p1p2 = det2p0p2 * det0p0p1 - det1p0p1 * p1.dot(p2 - p0);
if(det0p0p1p2 <= 0) closestPoint = (p1 * det1p1p2 + p2 * det2p1p2) / (det1p1p2 + det2p1p2);
else if(det1p0p1p2 <= 0) closestPoint = (p0 * det0p0p2 + p2 * det2p0p2) / (det0p0p2 + det2p0p2);
else if(det2p0p1p2 <= 0) closestPoint = (p0 * det0p0p1 + p1 * det1p0p1) / (det0p0p1 + det1p0p1);
//point is inside the triangle
else closestPoint = (p0 * det0p0p1p2 + p1 * det1p0p1p2 + p2 * det2p0p1p2) / (det0p0p1p2 + det1p0p1p2 + det2p0p1p2);
}
float distSquared = closestPoint.x * closestPoint.x + closestPoint.y * closestPoint.y + closestPoint.z * closestPoint.z;
if(distSquared > radius * radius)
{
continue;
}
if(distSquared < minDistSquared)
{
minDistSquared = distSquared;
localNormal = n;
}
}
normal = localNormal;
return minDistSquared;
}
INPLACE_TEMPL_ARGS_DEF
#ifdef __CUDACC__
__device__
#endif
PX_INLINE uint32_t handleCollisions(const SimulationParams* params, INPLACE_STORAGE_ARGS_DEF, PxVec3& position, PxVec3& velocity, PxVec3& normal)
{
const PxPlane* memConvexPlanes = params->convexPlanes;
const PxVec4* memConvexVerts = params->convexVerts;
const uint32_t* memConvexPolygonsData = params->convexPolygonsData;
// Algorithms are similar to CPU version
const PxVec4* memTrimeshVerts = params->trimeshVerts;
const uint32_t* memTrimeshIndices = params->trimeshIndices;
float collisionRadius = params->collisionDistance + params->collisionThreshold;
uint32_t numTriMeshes = params->trimeshes.getSize();
for (uint32_t i = 0; i < numTriMeshes; ++i)
{
CollisionTriMeshData data;
params->trimeshes.fetchElem(INPLACE_STORAGE_ARGS_VAL, data, i);
if (!data.aabb.contains(position)) //check coarse bounds
{
continue;
}
PxVec3 localPosition = data.inversePose.transform(position);
PxVec3 localNormal;
float minDistSquared = checkTriangleCollision(memTrimeshVerts, memTrimeshIndices, data, collisionRadius, localPosition, localNormal);
if (minDistSquared == PX_MAX_F32)
{
continue;
}
float penDepth = params->collisionDistance - PxSqrt(minDistSquared);
if( penDepth > 0 )
{
localPosition += localNormal * penDepth;
normal = data.pose.rotate(localNormal);
position = data.pose.transform(localPosition);
updateCollisionVelocity(data, normal, position, velocity);
}
return 1;
}
uint32_t numConvexMeshes = params->convexMeshes.getSize();
for (uint32_t i = 0; i < numConvexMeshes; ++i)
{
CollisionConvexMeshData data;
params->convexMeshes.fetchElem(INPLACE_STORAGE_ARGS_VAL, data, i);
if (!data.aabb.contains(position)) //check coarse bounds
{
continue;
}
PxVec3 localPosition = data.inversePose.transform(position);
float penDepth = UINT32_MAX;
PxVec3 localNormal(0);
bool insideConvex = true;
bool insidePolygon = true;
float distSquaredMin = UINT32_MAX;
PxVec3 nearestPointMin;
uint32_t polygonsDataOffset = data.polygonsDataOffset;
for (uint32_t polyId = 0; polyId < data.numPolygons; polyId++) // for each polygon
{
PxPlane plane;
SIM_FETCH_PLANE(plane, ConvexPlanes, data.firstPlane + polyId);
uint32_t vertCount = SIM_FETCH(ConvexPolygonsData, polygonsDataOffset);
float dist = (localPosition.dot(plane.n) + plane.d);
if (dist > 0) //outside convex
{
insideConvex = false;
if (dist > collisionRadius)
{
insidePolygon = false;
distSquaredMin = dist * dist;
break;
}
insidePolygon = true;
PxVec3 polygonNormal = plane.n;
uint32_t begVertId = SIM_FETCH(ConvexPolygonsData, polygonsDataOffset + vertCount);
PxVec3 begVert; splitFloat4(begVert, SIM_FETCH(ConvexVerts, data.firstVertex + begVertId));
for (uint32_t vertId = 1; vertId <= vertCount; ++vertId) //for each vertex
{
uint32_t endVertId = SIM_FETCH(ConvexPolygonsData, polygonsDataOffset + vertId);
PxVec3 endVert; splitFloat4(endVert, SIM_FETCH(ConvexVerts, data.firstVertex + endVertId));
PxVec3 segment = endVert - begVert;
PxVec3 segmentNormal = polygonNormal.cross(segment);
float sign = segmentNormal.dot(localPosition - begVert);
if (sign < 0)
{
insidePolygon = false;
float distSquared;
PxVec3 nearestPoint;
calculatePointSegmentSquaredDist(begVert, endVert, localPosition, distSquared, nearestPoint);
if (distSquared < distSquaredMin)
{
distSquaredMin = distSquared;
nearestPointMin = nearestPoint;
}
}
begVert = endVert;
}
if (insidePolygon)
{
penDepth = params->collisionDistance - dist;
localNormal = polygonNormal;
break;
}
}
if (insideConvex)
{
float penDepthPlane = params->collisionDistance - dist; //dist is negative inside
if (penDepthPlane < penDepth) //inside convex
{
penDepth = penDepthPlane;
localNormal = plane.n;
}
}
polygonsDataOffset += (vertCount + 1);
}
if (!insideConvex && !insidePolygon)
{
if (distSquaredMin > collisionRadius * collisionRadius)
{
continue; //no intersection, too far away
}
float dist = PxSqrt(distSquaredMin);
localNormal = localPosition - nearestPointMin;
localNormal *= (1 / dist); //normalize
penDepth = params->collisionDistance - dist;
}
if (penDepth > 0)
{
localPosition += localNormal * penDepth;
normal = data.pose.rotate(localNormal);
position = data.pose.transform(localPosition);
updateCollisionVelocity(data, normal, position, velocity);
}
return 1;
}
uint32_t numBoxes = params->boxes.getSize();
for (uint32_t i = 0; i < numBoxes; ++i)
{
CollisionBoxData data;
params->boxes.fetchElem(INPLACE_STORAGE_ARGS_VAL, data, i);
if (!data.aabb.contains(position))
{
continue;
}
PxVec3 localPosition = data.inversePose.transform(position);
PxVec3 closestPoint = PxVec3(PxClamp(localPosition.x, -data.halfSize.x, data.halfSize.x), PxClamp(localPosition.y, -data.halfSize.y, data.halfSize.y), PxClamp(localPosition.z, -data.halfSize.z, data.halfSize.z));
PxVec3 v = localPosition - closestPoint;
float vMagnitudeSquared = v.magnitudeSquared();
if(vMagnitudeSquared > collisionRadius * collisionRadius) continue; //no intersection
PxBounds3 bounds = PxBounds3(-data.halfSize, data.halfSize);
float penDepth;
PxVec3 localNormal(0);
if(vMagnitudeSquared > 0)
{
float vMagnitude = PxSqrt(vMagnitudeSquared);
localNormal = v * (1 / vMagnitude);
penDepth = params->collisionDistance - vMagnitude;
}
else
{
PxVec3 penDepth3D = PxVec3(
data.halfSize.x - PxAbs(localPosition.x),
data.halfSize.y - PxAbs(localPosition.y),
data.halfSize.z - PxAbs(localPosition.z)
);
float penDepth3Dmin = penDepth3D.minElement();
if (penDepth3Dmin == penDepth3D.x)
{
localNormal.x = localPosition.x < 0 ? -1.0f : 1.0f;
}
else if (penDepth3Dmin == penDepth3D.y)
{
localNormal.y = localPosition.y < 0 ? -1.0f : 1.0f;
}
else if (penDepth3Dmin == penDepth3D.z)
{
localNormal.z = localPosition.z < 0 ? -1.0f : 1.0f;
}
penDepth = params->collisionDistance + penDepth3Dmin;
}
normal = data.pose.rotate(localNormal);
if (penDepth > 0)
{
localPosition += localNormal * penDepth;
position = data.pose.transform(localPosition);
updateCollisionVelocity(data, normal, position, velocity);
}
return 1;
}
uint32_t numCapsules = params->capsules.getSize();
for (uint32_t i = 0; i < numCapsules; ++i)
{
CollisionCapsuleData data;
params->capsules.fetchElem(INPLACE_STORAGE_ARGS_VAL, data, i);
if (!data.aabb.contains(position))
{
continue;
}
PxVec3 localPosition = data.inversePose.transform(position);
// Capsule is Minkowski sum of sphere with segment
const float closestX = PxClamp(localPosition.x, -data.halfHeight, data.halfHeight);
PxVec3 localNormal(localPosition.x - closestX, localPosition.y, localPosition.z);
float distance = localNormal.magnitude();
float penDepth = (data.radius - distance);
// No intersection?
if (-penDepth > params->collisionThreshold)
{
continue;
}
if (distance > 0) // avoid division by zero
{
localNormal /= distance;
}
normal = data.pose.rotate(localNormal);
if (penDepth > 0)
{
localPosition = data.radius * localNormal;
localPosition.x += closestX;
position = data.pose.transform(localPosition);
updateCollisionVelocity(data, normal, position, velocity);
}
return 1;
}
uint32_t numSpheres = params->spheres.getSize();
for (uint32_t i = 0; i < numSpheres; ++i)
{
CollisionSphereData data;
params->spheres.fetchElem(INPLACE_STORAGE_ARGS_VAL, data, i);
if (!data.aabb.contains(position))
{
continue;
}
PxVec3 localNormal = data.inversePose.transform(position);
float distance = localNormal.magnitude();
float penDepth = (data.radius - distance);
// No intersection?
if (-penDepth > params->collisionThreshold)
{
continue;
}
localNormal /= distance;
normal = data.pose.rotate(localNormal);
if (penDepth > 0)
{
position = data.pose.transform(data.radius * localNormal);
updateCollisionVelocity(data, normal, position, velocity);
}
return 1;
}
uint32_t numHalfSpaces = params->halfSpaces.getSize();
for (uint32_t i = 0; i < numHalfSpaces; ++i)
{
CollisionHalfSpaceData data;
params->halfSpaces.fetchElem(INPLACE_STORAGE_ARGS_VAL, data, i);
float penDepth = (data.origin - position).dot(data.normal);
// No intersection?
if (-penDepth > params->collisionThreshold)
{
continue;
}
normal = data.normal;
if (penDepth > 0)
{
position += penDepth * data.normal;
updateCollisionVelocity(data, normal, position, velocity);
}
return 1;
}
return 0;
}
#ifdef __CUDACC__
__device__
#endif
PX_INLINE float calcParticleBenefit(
const InjectorParams& inj, const PxVec3& eyePos,
const PxVec3& pos, const PxVec3& vel, float life)
{
float benefit = inj.mLODBias;
//distance term
float distance = (eyePos - pos).magnitude();
benefit += inj.mLODDistanceWeight * (1.0f - PxMin(1.0f, distance / inj.mLODMaxDistance));
//velocity term, TODO: clamp velocity
float velMag = vel.magnitude();
benefit += inj.mLODSpeedWeight * velMag;
//life term
benefit += inj.mLODLifeWeight * life;
return PxClamp(benefit, 0.0f, 1.0f);
}
INPLACE_TEMPL_VA_ARGS_DEF(typename FieldAccessor)
#ifdef __CUDACC__
__device__
#endif
PX_INLINE float simulateParticle(
const SimulationParams* params, INPLACE_STORAGE_ARGS_DEF, SIM_INJECTOR_ARRAY injectorArray,
float deltaTime, PxVec3 gravity, PxVec3 eyePos,
bool isNewParticle, unsigned int srcIdx, unsigned int dstIdx,
SIM_FLOAT4* memPositionMass, SIM_FLOAT4* memVelocityLife, IofxActorIDIntl* memIofxActorIDs,
float* memLifeSpan, float* memLifeTime, unsigned int* memInjector, SIM_FLOAT4* memCollisionNormalFlags, uint32_t* memUserData,
FieldAccessor& fieldAccessor, unsigned int &injIndex
)
{
//read
PxVec3 position;
PxVec3 velocity;
float mass = splitFloat4(position, SIM_FETCH(PositionMass, srcIdx));
splitFloat4(velocity, SIM_FETCH(VelocityLife, srcIdx));
float lifeSpan = SIM_FETCH(LifeSpan, srcIdx);
unsigned int injector = SIM_FETCH(Injector, srcIdx);
IofxActorIDIntl iofxActorID = IofxActorIDIntl(SIM_FETCH(IofxActorIDs, srcIdx));
PxVec3 collisionNormal(0.0f);
uint32_t collisionFlags = 0;
float lifeTime = lifeSpan;
if (!isNewParticle)
{
using namespace nvidia::apex;
lifeTime = SIM_FETCH(LifeTime, srcIdx);
//collide using the old state
collisionFlags = handleCollisions INPLACE_TEMPL_ARGS_VAL (params, INPLACE_STORAGE_ARGS_VAL, position, velocity, collisionNormal);
//advance to a new state
PxVec3 velocityDelta = deltaTime * gravity;
fieldAccessor(srcIdx, velocityDelta);
velocity += velocityDelta;
position += deltaTime * velocity;
lifeTime = PxMax(lifeTime - deltaTime, 0.0f);
}
InjectorParams injParams;
SIM_FETCH_INJECTOR(injectorArray, injParams, injector);
injIndex = injParams.mLocalIndex;
// injParams.mLODBias == FLT_MAX if injector was released!
// and IOFX returns IofxActorIDIntl::NO_VOLUME for homeless/dead particles
bool validActorID = (injParams.mLODBias < FLT_MAX)
&& (isNewParticle || (iofxActorID.getVolumeID() != IofxActorIDIntl::NO_VOLUME))
&& position.isFinite() && velocity.isFinite();
if (!validActorID)
{
iofxActorID.setActorClassID(IofxActorIDIntl::IPX_ACTOR);
injIndex = UINT32_MAX;
}
//write
memLifeTime[dstIdx] = lifeTime;
if (!isNewParticle || dstIdx != srcIdx)
{
memPositionMass[dstIdx] = combineFloat4(position, mass);
memVelocityLife[dstIdx] = combineFloat4(velocity, lifeTime / lifeSpan);
}
if (!validActorID || dstIdx != srcIdx)
{
memIofxActorIDs[dstIdx] = iofxActorID;
}
if (dstIdx != srcIdx)
{
memLifeSpan[dstIdx] = lifeSpan;
memInjector[dstIdx] = injector;
memUserData[dstIdx] = SIM_FETCH(UserData, srcIdx);
}
memCollisionNormalFlags[dstIdx] = combineFloat4(collisionNormal, SIM_INT_AS_FLOAT(collisionFlags));
float benefit = -FLT_MAX;
if (validActorID && lifeTime > 0.0f)
{
benefit = calcParticleBenefit(injParams, eyePos, position, velocity, lifeTime / lifeSpan);
}
return benefit;
}
}
} // namespace nvidia
#endif
|