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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
|
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
#define SHAPE_CONVEX_HULL 3
#define SHAPE_PLANE 4
#define SHAPE_CONCAVE_TRIMESH 5
#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
#define SHAPE_SPHERE 7
#pragma OPENCL EXTENSION cl_amd_printf : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
#ifdef cl_ext_atomic_counters_32
#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
#else
#define counter32_t volatile __global int*
#endif
#define GET_GROUP_IDX get_group_id(0)
#define GET_LOCAL_IDX get_local_id(0)
#define GET_GLOBAL_IDX get_global_id(0)
#define GET_GROUP_SIZE get_local_size(0)
#define GET_NUM_GROUPS get_num_groups(0)
#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
#define AtomInc(x) atom_inc(&(x))
#define AtomInc1(x, out) out = atom_inc(&(x))
#define AppendInc(x, out) out = atomic_inc(x)
#define AtomAdd(x, value) atom_add(&(x), value)
#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
#define AtomXhg(x, value) atom_xchg ( &(x), value )
#define max2 max
#define min2 min
typedef unsigned int u32;
typedef struct
{
union
{
float4 m_min;
float m_minElems[4];
int m_minIndices[4];
};
union
{
float4 m_max;
float m_maxElems[4];
int m_maxIndices[4];
};
} btAabbCL;
///keep this in sync with btCollidable.h
typedef struct
{
int m_numChildShapes;
float m_radius;
int m_shapeType;
int m_shapeIndex;
} btCollidableGpu;
typedef struct
{
float4 m_childPosition;
float4 m_childOrientation;
int m_shapeIndex;
int m_unused0;
int m_unused1;
int m_unused2;
} btGpuChildShape;
#define GET_NPOINTS(x) (x).m_worldNormalOnB.w
typedef struct
{
float4 m_pos;
float4 m_quat;
float4 m_linVel;
float4 m_angVel;
u32 m_collidableIdx;
float m_invMass;
float m_restituitionCoeff;
float m_frictionCoeff;
} BodyData;
typedef struct
{
float4 m_localCenter;
float4 m_extents;
float4 mC;
float4 mE;
float m_radius;
int m_faceOffset;
int m_numFaces;
int m_numVertices;
int m_vertexOffset;
int m_uniqueEdgesOffset;
int m_numUniqueEdges;
int m_unused;
} ConvexPolyhedronCL;
typedef struct
{
float4 m_plane;
int m_indexOffset;
int m_numIndices;
} btGpuFace;
#define SELECT_UINT4( b, a, condition ) select( b,a,condition )
#define make_float4 (float4)
#define make_float2 (float2)
#define make_uint4 (uint4)
#define make_int4 (int4)
#define make_uint2 (uint2)
#define make_int2 (int2)
__inline
float fastDiv(float numerator, float denominator)
{
return native_divide(numerator, denominator);
// return numerator/denominator;
}
__inline
float4 fastDiv4(float4 numerator, float4 denominator)
{
return native_divide(numerator, denominator);
}
__inline
float4 cross3(float4 a, float4 b)
{
return cross(a,b);
}
//#define dot3F4 dot
__inline
float dot3F4(float4 a, float4 b)
{
float4 a1 = make_float4(a.xyz,0.f);
float4 b1 = make_float4(b.xyz,0.f);
return dot(a1, b1);
}
__inline
float4 fastNormalize4(float4 v)
{
return fast_normalize(v);
}
///////////////////////////////////////
// Quaternion
///////////////////////////////////////
typedef float4 Quaternion;
__inline
Quaternion qtMul(Quaternion a, Quaternion b);
__inline
Quaternion qtNormalize(Quaternion in);
__inline
float4 qtRotate(Quaternion q, float4 vec);
__inline
Quaternion qtInvert(Quaternion q);
__inline
Quaternion qtMul(Quaternion a, Quaternion b)
{
Quaternion ans;
ans = cross3( a, b );
ans += a.w*b+b.w*a;
// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
ans.w = a.w*b.w - dot3F4(a, b);
return ans;
}
__inline
Quaternion qtNormalize(Quaternion in)
{
return fastNormalize4(in);
// in /= length( in );
// return in;
}
__inline
float4 qtRotate(Quaternion q, float4 vec)
{
Quaternion qInv = qtInvert( q );
float4 vcpy = vec;
vcpy.w = 0.f;
float4 out = qtMul(qtMul(q,vcpy),qInv);
return out;
}
__inline
Quaternion qtInvert(Quaternion q)
{
return (Quaternion)(-q.xyz, q.w);
}
__inline
float4 qtInvRotate(const Quaternion q, float4 vec)
{
return qtRotate( qtInvert( q ), vec );
}
__inline
float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)
{
return qtRotate( *orientation, *p ) + (*translation);
}
void trInverse(float4 translationIn, Quaternion orientationIn,
float4* translationOut, Quaternion* orientationOut)
{
*orientationOut = qtInvert(orientationIn);
*translationOut = qtRotate(*orientationOut, -translationIn);
}
void trMul(float4 translationA, Quaternion orientationA,
float4 translationB, Quaternion orientationB,
float4* translationOut, Quaternion* orientationOut)
{
*orientationOut = qtMul(orientationA,orientationB);
*translationOut = transform(&translationB,&translationA,&orientationA);
}
__inline
float4 normalize3(const float4 a)
{
float4 n = make_float4(a.x, a.y, a.z, 0.f);
return fastNormalize4( n );
}
__inline float4 lerp3(const float4 a,const float4 b, float t)
{
return make_float4( a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t,
0.f);
}
float signedDistanceFromPointToPlane(float4 point, float4 planeEqn, float4* closestPointOnFace)
{
float4 n = (float4)(planeEqn.x, planeEqn.y, planeEqn.z, 0);
float dist = dot3F4(n, point) + planeEqn.w;
*closestPointOnFace = point - dist * n;
return dist;
}
inline bool IsPointInPolygon(float4 p,
const btGpuFace* face,
__global const float4* baseVertex,
__global const int* convexIndices,
float4* out)
{
float4 a;
float4 b;
float4 ab;
float4 ap;
float4 v;
float4 plane = make_float4(face->m_plane.x,face->m_plane.y,face->m_plane.z,0.f);
if (face->m_numIndices<2)
return false;
float4 v0 = baseVertex[convexIndices[face->m_indexOffset + face->m_numIndices-1]];
b = v0;
for(unsigned i=0; i != face->m_numIndices; ++i)
{
a = b;
float4 vi = baseVertex[convexIndices[face->m_indexOffset + i]];
b = vi;
ab = b-a;
ap = p-a;
v = cross3(ab,plane);
if (dot(ap, v) > 0.f)
{
float ab_m2 = dot(ab, ab);
float rt = ab_m2 != 0.f ? dot(ab, ap) / ab_m2 : 0.f;
if (rt <= 0.f)
{
*out = a;
}
else if (rt >= 1.f)
{
*out = b;
}
else
{
float s = 1.f - rt;
out[0].x = s * a.x + rt * b.x;
out[0].y = s * a.y + rt * b.y;
out[0].z = s * a.z + rt * b.z;
}
return false;
}
}
return true;
}
void computeContactSphereConvex(int pairIndex,
int bodyIndexA, int bodyIndexB,
int collidableIndexA, int collidableIndexB,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* convexVertices,
__global const int* convexIndices,
__global const btGpuFace* faces,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int maxContactCapacity,
float4 spherePos2,
float radius,
float4 pos,
float4 quat
)
{
float4 invPos;
float4 invOrn;
trInverse(pos,quat, &invPos,&invOrn);
float4 spherePos = transform(&spherePos2,&invPos,&invOrn);
int shapeIndex = collidables[collidableIndexB].m_shapeIndex;
int numFaces = convexShapes[shapeIndex].m_numFaces;
float4 closestPnt = (float4)(0, 0, 0, 0);
float4 hitNormalWorld = (float4)(0, 0, 0, 0);
float minDist = -1000000.f;
bool bCollide = true;
for ( int f = 0; f < numFaces; f++ )
{
btGpuFace face = faces[convexShapes[shapeIndex].m_faceOffset+f];
// set up a plane equation
float4 planeEqn;
float4 n1 = face.m_plane;
n1.w = 0.f;
planeEqn = n1;
planeEqn.w = face.m_plane.w;
// compute a signed distance from the vertex in cloth to the face of rigidbody.
float4 pntReturn;
float dist = signedDistanceFromPointToPlane(spherePos, planeEqn, &pntReturn);
// If the distance is positive, the plane is a separating plane.
if ( dist > radius )
{
bCollide = false;
break;
}
if (dist>0)
{
//might hit an edge or vertex
float4 out;
float4 zeroPos = make_float4(0,0,0,0);
bool isInPoly = IsPointInPolygon(spherePos,
&face,
&convexVertices[convexShapes[shapeIndex].m_vertexOffset],
convexIndices,
&out);
if (isInPoly)
{
if (dist>minDist)
{
minDist = dist;
closestPnt = pntReturn;
hitNormalWorld = planeEqn;
}
} else
{
float4 tmp = spherePos-out;
float l2 = dot(tmp,tmp);
if (l2<radius*radius)
{
dist = sqrt(l2);
if (dist>minDist)
{
minDist = dist;
closestPnt = out;
hitNormalWorld = tmp/dist;
}
} else
{
bCollide = false;
break;
}
}
} else
{
if ( dist > minDist )
{
minDist = dist;
closestPnt = pntReturn;
hitNormalWorld.xyz = planeEqn.xyz;
}
}
}
if (bCollide && minDist > -10000)
{
float4 normalOnSurfaceB1 = qtRotate(quat,-hitNormalWorld);
float4 pOnB1 = transform(&closestPnt,&pos,&quat);
float actualDepth = minDist-radius;
if (actualDepth<=0.f)
{
pOnB1.w = actualDepth;
int dstIdx;
AppendInc( nGlobalContactsOut, dstIdx );
if (1)//dstIdx < maxContactCapacity)
{
__global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
c->m_worldNormalOnB = -normalOnSurfaceB1;
c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
c->m_batchIdx = pairIndex;
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
c->m_worldPosB[0] = pOnB1;
c->m_childIndexA = -1;
c->m_childIndexB = -1;
GET_NPOINTS(*c) = 1;
}
}
}//if (hasCollision)
}
int extractManifoldSequential(const float4* p, int nPoints, float4 nearNormal, int4* contactIdx)
{
if( nPoints == 0 )
return 0;
if (nPoints <=4)
return nPoints;
if (nPoints >64)
nPoints = 64;
float4 center = make_float4(0.f);
{
for (int i=0;i<nPoints;i++)
center += p[i];
center /= (float)nPoints;
}
// sample 4 directions
float4 aVector = p[0] - center;
float4 u = cross3( nearNormal, aVector );
float4 v = cross3( nearNormal, u );
u = normalize3( u );
v = normalize3( v );
//keep point with deepest penetration
float minW= FLT_MAX;
int minIndex=-1;
float4 maxDots;
maxDots.x = FLT_MIN;
maxDots.y = FLT_MIN;
maxDots.z = FLT_MIN;
maxDots.w = FLT_MIN;
// idx, distance
for(int ie = 0; ie<nPoints; ie++ )
{
if (p[ie].w<minW)
{
minW = p[ie].w;
minIndex=ie;
}
float f;
float4 r = p[ie]-center;
f = dot3F4( u, r );
if (f<maxDots.x)
{
maxDots.x = f;
contactIdx[0].x = ie;
}
f = dot3F4( -u, r );
if (f<maxDots.y)
{
maxDots.y = f;
contactIdx[0].y = ie;
}
f = dot3F4( v, r );
if (f<maxDots.z)
{
maxDots.z = f;
contactIdx[0].z = ie;
}
f = dot3F4( -v, r );
if (f<maxDots.w)
{
maxDots.w = f;
contactIdx[0].w = ie;
}
}
if (contactIdx[0].x != minIndex && contactIdx[0].y != minIndex && contactIdx[0].z != minIndex && contactIdx[0].w != minIndex)
{
//replace the first contact with minimum (todo: replace contact with least penetration)
contactIdx[0].x = minIndex;
}
return 4;
}
#define MAX_PLANE_CONVEX_POINTS 64
int computeContactPlaneConvex(int pairIndex,
int bodyIndexA, int bodyIndexB,
int collidableIndexA, int collidableIndexB,
__global const BodyData* rigidBodies,
__global const btCollidableGpu*collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* convexVertices,
__global const int* convexIndices,
__global const btGpuFace* faces,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int maxContactCapacity,
float4 posB,
Quaternion ornB
)
{
int resultIndex=-1;
int shapeIndex = collidables[collidableIndexB].m_shapeIndex;
__global const ConvexPolyhedronCL* hullB = &convexShapes[shapeIndex];
float4 posA;
posA = rigidBodies[bodyIndexA].m_pos;
Quaternion ornA;
ornA = rigidBodies[bodyIndexA].m_quat;
int numContactsOut = 0;
int numWorldVertsB1= 0;
float4 planeEq;
planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
float4 planeNormal = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);
float4 planeNormalWorld;
planeNormalWorld = qtRotate(ornA,planeNormal);
float planeConstant = planeEq.w;
float4 invPosA;Quaternion invOrnA;
float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;
{
trInverse(posA,ornA,&invPosA,&invOrnA);
trMul(invPosA,invOrnA,posB,ornB,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
}
float4 invPosB;Quaternion invOrnB;
float4 planeInConvexPos1; Quaternion planeInConvexOrn1;
{
trInverse(posB,ornB,&invPosB,&invOrnB);
trMul(invPosB,invOrnB,posA,ornA,&planeInConvexPos1,&planeInConvexOrn1);
}
float4 planeNormalInConvex = qtRotate(planeInConvexOrn1,-planeNormal);
float maxDot = -1e30;
int hitVertex=-1;
float4 hitVtx;
float4 contactPoints[MAX_PLANE_CONVEX_POINTS];
int numPoints = 0;
int4 contactIdx;
contactIdx=make_int4(0,1,2,3);
for (int i=0;i<hullB->m_numVertices;i++)
{
float4 vtx = convexVertices[hullB->m_vertexOffset+i];
float curDot = dot(vtx,planeNormalInConvex);
if (curDot>maxDot)
{
hitVertex=i;
maxDot=curDot;
hitVtx = vtx;
//make sure the deepest points is always included
if (numPoints==MAX_PLANE_CONVEX_POINTS)
numPoints--;
}
if (numPoints<MAX_PLANE_CONVEX_POINTS)
{
float4 vtxWorld = transform(&vtx, &posB, &ornB);
float4 vtxInPlane = transform(&vtxWorld, &invPosA, &invOrnA);//oplaneTransform.inverse()*vtxWorld;
float dist = dot(planeNormal,vtxInPlane)-planeConstant;
if (dist<0.f)
{
vtxWorld.w = dist;
contactPoints[numPoints] = vtxWorld;
numPoints++;
}
}
}
int numReducedPoints = numPoints;
if (numPoints>4)
{
numReducedPoints = extractManifoldSequential( contactPoints, numPoints, planeNormalInConvex, &contactIdx);
}
if (numReducedPoints>0)
{
int dstIdx;
AppendInc( nGlobalContactsOut, dstIdx );
if (dstIdx < maxContactCapacity)
{
resultIndex = dstIdx;
__global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
c->m_worldNormalOnB = -planeNormalWorld;
//c->setFrictionCoeff(0.7);
//c->setRestituitionCoeff(0.f);
c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
c->m_batchIdx = pairIndex;
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
c->m_childIndexA = -1;
c->m_childIndexB = -1;
switch (numReducedPoints)
{
case 4:
c->m_worldPosB[3] = contactPoints[contactIdx.w];
case 3:
c->m_worldPosB[2] = contactPoints[contactIdx.z];
case 2:
c->m_worldPosB[1] = contactPoints[contactIdx.y];
case 1:
c->m_worldPosB[0] = contactPoints[contactIdx.x];
default:
{
}
};
GET_NPOINTS(*c) = numReducedPoints;
}//if (dstIdx < numPairs)
}
return resultIndex;
}
void computeContactPlaneSphere(int pairIndex,
int bodyIndexA, int bodyIndexB,
int collidableIndexA, int collidableIndexB,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const btGpuFace* faces,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int maxContactCapacity)
{
float4 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
float radius = collidables[collidableIndexB].m_radius;
float4 posA1 = rigidBodies[bodyIndexA].m_pos;
float4 ornA1 = rigidBodies[bodyIndexA].m_quat;
float4 posB1 = rigidBodies[bodyIndexB].m_pos;
float4 ornB1 = rigidBodies[bodyIndexB].m_quat;
bool hasCollision = false;
float4 planeNormal1 = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);
float planeConstant = planeEq.w;
float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;
{
float4 invPosA;Quaternion invOrnA;
trInverse(posA1,ornA1,&invPosA,&invOrnA);
trMul(invPosA,invOrnA,posB1,ornB1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
}
float4 planeInConvexPos1; Quaternion planeInConvexOrn1;
{
float4 invPosB;Quaternion invOrnB;
trInverse(posB1,ornB1,&invPosB,&invOrnB);
trMul(invPosB,invOrnB,posA1,ornA1,&planeInConvexPos1,&planeInConvexOrn1);
}
float4 vtx1 = qtRotate(planeInConvexOrn1,-planeNormal1)*radius;
float4 vtxInPlane1 = transform(&vtx1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
float distance = dot3F4(planeNormal1,vtxInPlane1) - planeConstant;
hasCollision = distance < 0.f;//m_manifoldPtr->getContactBreakingThreshold();
if (hasCollision)
{
float4 vtxInPlaneProjected1 = vtxInPlane1 - distance*planeNormal1;
float4 vtxInPlaneWorld1 = transform(&vtxInPlaneProjected1,&posA1,&ornA1);
float4 normalOnSurfaceB1 = qtRotate(ornA1,planeNormal1);
float4 pOnB1 = vtxInPlaneWorld1+normalOnSurfaceB1*distance;
pOnB1.w = distance;
int dstIdx;
AppendInc( nGlobalContactsOut, dstIdx );
if (dstIdx < maxContactCapacity)
{
__global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
c->m_worldNormalOnB = -normalOnSurfaceB1;
c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
c->m_batchIdx = pairIndex;
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
c->m_worldPosB[0] = pOnB1;
c->m_childIndexA = -1;
c->m_childIndexB = -1;
GET_NPOINTS(*c) = 1;
}//if (dstIdx < numPairs)
}//if (hasCollision)
}
__kernel void primitiveContactsKernel( __global int4* pairs,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* vertices,
__global const float4* uniqueEdges,
__global const btGpuFace* faces,
__global const int* indices,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int numPairs, int maxContactCapacity)
{
int i = get_global_id(0);
int pairIndex = i;
float4 worldVertsB1[64];
float4 worldVertsB2[64];
int capacityWorldVerts = 64;
float4 localContactsOut[64];
int localContactCapacity=64;
float minDist = -1e30f;
float maxDist = 0.02f;
if (i<numPairs)
{
int bodyIndexA = pairs[i].x;
int bodyIndexB = pairs[i].y;
int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
{
float4 posB;
posB = rigidBodies[bodyIndexB].m_pos;
Quaternion ornB;
ornB = rigidBodies[bodyIndexB].m_quat;
int contactIndex = computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
rigidBodies,collidables,convexShapes,vertices,indices,
faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity, posB,ornB);
if (contactIndex>=0)
pairs[pairIndex].z = contactIndex;
return;
}
if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
{
float4 posA;
posA = rigidBodies[bodyIndexA].m_pos;
Quaternion ornA;
ornA = rigidBodies[bodyIndexA].m_quat;
int contactIndex = computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
rigidBodies,collidables,convexShapes,vertices,indices,
faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posA,ornA);
if (contactIndex>=0)
pairs[pairIndex].z = contactIndex;
return;
}
if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
{
computeContactPlaneSphere(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);
return;
}
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
{
computeContactPlaneSphere( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
rigidBodies,collidables,
faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);
return;
}
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
{
float4 spherePos = rigidBodies[bodyIndexA].m_pos;
float sphereRadius = collidables[collidableIndexA].m_radius;
float4 convexPos = rigidBodies[bodyIndexB].m_pos;
float4 convexOrn = rigidBodies[bodyIndexB].m_quat;
computeContactSphereConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
spherePos,sphereRadius,convexPos,convexOrn);
return;
}
if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
{
float4 spherePos = rigidBodies[bodyIndexB].m_pos;
float sphereRadius = collidables[collidableIndexB].m_radius;
float4 convexPos = rigidBodies[bodyIndexA].m_pos;
float4 convexOrn = rigidBodies[bodyIndexA].m_quat;
computeContactSphereConvex(pairIndex, bodyIndexB, bodyIndexA, collidableIndexB, collidableIndexA,
rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
spherePos,sphereRadius,convexPos,convexOrn);
return;
}
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
{
//sphere-sphere
float radiusA = collidables[collidableIndexA].m_radius;
float radiusB = collidables[collidableIndexB].m_radius;
float4 posA = rigidBodies[bodyIndexA].m_pos;
float4 posB = rigidBodies[bodyIndexB].m_pos;
float4 diff = posA-posB;
float len = length(diff);
///iff distance positive, don't generate a new contact
if ( len <= (radiusA+radiusB))
{
///distance (negative means penetration)
float dist = len - (radiusA+radiusB);
float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);
if (len > 0.00001)
{
normalOnSurfaceB = diff / len;
}
float4 contactPosB = posB + normalOnSurfaceB*radiusB;
contactPosB.w = dist;
int dstIdx;
AppendInc( nGlobalContactsOut, dstIdx );
if (dstIdx < maxContactCapacity)
{
__global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
c->m_worldNormalOnB = normalOnSurfaceB;
c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
c->m_batchIdx = pairIndex;
int bodyA = pairs[pairIndex].x;
int bodyB = pairs[pairIndex].y;
c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
c->m_worldPosB[0] = contactPosB;
c->m_childIndexA = -1;
c->m_childIndexB = -1;
GET_NPOINTS(*c) = 1;
}//if (dstIdx < numPairs)
}//if ( len <= (radiusA+radiusB))
return;
}//SHAPE_SPHERE SHAPE_SPHERE
}// if (i<numPairs)
}
// work-in-progress
__kernel void processCompoundPairsPrimitivesKernel( __global const int4* gpuCompoundPairs,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* vertices,
__global const float4* uniqueEdges,
__global const btGpuFace* faces,
__global const int* indices,
__global btAabbCL* aabbs,
__global const btGpuChildShape* gpuChildShapes,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int numCompoundPairs, int maxContactCapacity
)
{
int i = get_global_id(0);
if (i<numCompoundPairs)
{
int bodyIndexA = gpuCompoundPairs[i].x;
int bodyIndexB = gpuCompoundPairs[i].y;
int childShapeIndexA = gpuCompoundPairs[i].z;
int childShapeIndexB = gpuCompoundPairs[i].w;
int collidableIndexA = -1;
int collidableIndexB = -1;
float4 ornA = rigidBodies[bodyIndexA].m_quat;
float4 posA = rigidBodies[bodyIndexA].m_pos;
float4 ornB = rigidBodies[bodyIndexB].m_quat;
float4 posB = rigidBodies[bodyIndexB].m_pos;
if (childShapeIndexA >= 0)
{
collidableIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;
float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;
float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;
float4 newPosA = qtRotate(ornA,childPosA)+posA;
float4 newOrnA = qtMul(ornA,childOrnA);
posA = newPosA;
ornA = newOrnA;
} else
{
collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
}
if (childShapeIndexB>=0)
{
collidableIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
float4 newPosB = transform(&childPosB,&posB,&ornB);
float4 newOrnB = qtMul(ornB,childOrnB);
posB = newPosB;
ornB = newOrnB;
} else
{
collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
}
int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
int shapeTypeA = collidables[collidableIndexA].m_shapeType;
int shapeTypeB = collidables[collidableIndexB].m_shapeType;
int pairIndex = i;
if ((shapeTypeA == SHAPE_PLANE) && (shapeTypeB==SHAPE_CONVEX_HULL))
{
computeContactPlaneConvex( pairIndex, bodyIndexA,bodyIndexB, collidableIndexA,collidableIndexB,
rigidBodies,collidables,convexShapes,vertices,indices,
faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posB,ornB);
return;
}
if ((shapeTypeA == SHAPE_CONVEX_HULL) && (shapeTypeB==SHAPE_PLANE))
{
computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
rigidBodies,collidables,convexShapes,vertices,indices,
faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posA,ornA);
return;
}
if ((shapeTypeA == SHAPE_CONVEX_HULL) && (shapeTypeB == SHAPE_SPHERE))
{
float4 spherePos = rigidBodies[bodyIndexB].m_pos;
float sphereRadius = collidables[collidableIndexB].m_radius;
float4 convexPos = posA;
float4 convexOrn = ornA;
computeContactSphereConvex(pairIndex, bodyIndexB, bodyIndexA , collidableIndexB,collidableIndexA,
rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
spherePos,sphereRadius,convexPos,convexOrn);
return;
}
if ((shapeTypeA == SHAPE_SPHERE) && (shapeTypeB == SHAPE_CONVEX_HULL))
{
float4 spherePos = rigidBodies[bodyIndexA].m_pos;
float sphereRadius = collidables[collidableIndexA].m_radius;
float4 convexPos = posB;
float4 convexOrn = ornB;
computeContactSphereConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
spherePos,sphereRadius,convexPos,convexOrn);
return;
}
}// if (i<numCompoundPairs)
}
bool pointInTriangle(const float4* vertices, const float4* normal, float4 *p )
{
const float4* p1 = &vertices[0];
const float4* p2 = &vertices[1];
const float4* p3 = &vertices[2];
float4 edge1; edge1 = (*p2 - *p1);
float4 edge2; edge2 = ( *p3 - *p2 );
float4 edge3; edge3 = ( *p1 - *p3 );
float4 p1_to_p; p1_to_p = ( *p - *p1 );
float4 p2_to_p; p2_to_p = ( *p - *p2 );
float4 p3_to_p; p3_to_p = ( *p - *p3 );
float4 edge1_normal; edge1_normal = ( cross(edge1,*normal));
float4 edge2_normal; edge2_normal = ( cross(edge2,*normal));
float4 edge3_normal; edge3_normal = ( cross(edge3,*normal));
float r1, r2, r3;
r1 = dot(edge1_normal,p1_to_p );
r2 = dot(edge2_normal,p2_to_p );
r3 = dot(edge3_normal,p3_to_p );
if ( r1 > 0 && r2 > 0 && r3 > 0 )
return true;
if ( r1 <= 0 && r2 <= 0 && r3 <= 0 )
return true;
return false;
}
float segmentSqrDistance(float4 from, float4 to,float4 p, float4* nearest)
{
float4 diff = p - from;
float4 v = to - from;
float t = dot(v,diff);
if (t > 0)
{
float dotVV = dot(v,v);
if (t < dotVV)
{
t /= dotVV;
diff -= t*v;
} else
{
t = 1;
diff -= v;
}
} else
{
t = 0;
}
*nearest = from + t*v;
return dot(diff,diff);
}
void computeContactSphereTriangle(int pairIndex,
int bodyIndexA, int bodyIndexB,
int collidableIndexA, int collidableIndexB,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
const float4* triangleVertices,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int maxContactCapacity,
float4 spherePos2,
float radius,
float4 pos,
float4 quat,
int faceIndex
)
{
float4 invPos;
float4 invOrn;
trInverse(pos,quat, &invPos,&invOrn);
float4 spherePos = transform(&spherePos2,&invPos,&invOrn);
int numFaces = 3;
float4 closestPnt = (float4)(0, 0, 0, 0);
float4 hitNormalWorld = (float4)(0, 0, 0, 0);
float minDist = -1000000.f;
bool bCollide = false;
//////////////////////////////////////
float4 sphereCenter;
sphereCenter = spherePos;
const float4* vertices = triangleVertices;
float contactBreakingThreshold = 0.f;//todo?
float radiusWithThreshold = radius + contactBreakingThreshold;
float4 edge10;
edge10 = vertices[1]-vertices[0];
edge10.w = 0.f;//is this needed?
float4 edge20;
edge20 = vertices[2]-vertices[0];
edge20.w = 0.f;//is this needed?
float4 normal = cross3(edge10,edge20);
normal = normalize(normal);
float4 p1ToCenter;
p1ToCenter = sphereCenter - vertices[0];
float distanceFromPlane = dot(p1ToCenter,normal);
if (distanceFromPlane < 0.f)
{
//triangle facing the other way
distanceFromPlane *= -1.f;
normal *= -1.f;
}
hitNormalWorld = normal;
bool isInsideContactPlane = distanceFromPlane < radiusWithThreshold;
// Check for contact / intersection
bool hasContact = false;
float4 contactPoint;
if (isInsideContactPlane)
{
if (pointInTriangle(vertices,&normal, &sphereCenter))
{
// Inside the contact wedge - touches a point on the shell plane
hasContact = true;
contactPoint = sphereCenter - normal*distanceFromPlane;
} else {
// Could be inside one of the contact capsules
float contactCapsuleRadiusSqr = radiusWithThreshold*radiusWithThreshold;
float4 nearestOnEdge;
int numEdges = 3;
for (int i = 0; i < numEdges; i++)
{
float4 pa =vertices[i];
float4 pb = vertices[(i+1)%3];
float distanceSqr = segmentSqrDistance(pa,pb,sphereCenter, &nearestOnEdge);
if (distanceSqr < contactCapsuleRadiusSqr)
{
// Yep, we're inside a capsule
hasContact = true;
contactPoint = nearestOnEdge;
}
}
}
}
if (hasContact)
{
closestPnt = contactPoint;
float4 contactToCenter = sphereCenter - contactPoint;
minDist = length(contactToCenter);
if (minDist>FLT_EPSILON)
{
hitNormalWorld = normalize(contactToCenter);//*(1./minDist);
bCollide = true;
}
}
/////////////////////////////////////
if (bCollide && minDist > -10000)
{
float4 normalOnSurfaceB1 = qtRotate(quat,-hitNormalWorld);
float4 pOnB1 = transform(&closestPnt,&pos,&quat);
float actualDepth = minDist-radius;
if (actualDepth<=0.f)
{
pOnB1.w = actualDepth;
int dstIdx;
float lenSqr = dot3F4(normalOnSurfaceB1,normalOnSurfaceB1);
if (lenSqr>FLT_EPSILON)
{
AppendInc( nGlobalContactsOut, dstIdx );
if (dstIdx < maxContactCapacity)
{
__global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
c->m_worldNormalOnB = -normalOnSurfaceB1;
c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
c->m_batchIdx = pairIndex;
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
c->m_worldPosB[0] = pOnB1;
c->m_childIndexA = -1;
c->m_childIndexB = faceIndex;
GET_NPOINTS(*c) = 1;
}
}
}
}//if (hasCollision)
}
// work-in-progress
__kernel void findConcaveSphereContactsKernel( __global int4* concavePairs,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* vertices,
__global const float4* uniqueEdges,
__global const btGpuFace* faces,
__global const int* indices,
__global btAabbCL* aabbs,
__global struct b3Contact4Data* restrict globalContactsOut,
counter32_t nGlobalContactsOut,
int numConcavePairs, int maxContactCapacity
)
{
int i = get_global_id(0);
if (i>=numConcavePairs)
return;
int pairIdx = i;
int bodyIndexA = concavePairs[i].x;
int bodyIndexB = concavePairs[i].y;
int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
if (collidables[collidableIndexB].m_shapeType==SHAPE_SPHERE)
{
int f = concavePairs[i].z;
btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
float4 verticesA[3];
for (int i=0;i<3;i++)
{
int index = indices[face.m_indexOffset+i];
float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
verticesA[i] = vert;
}
float4 spherePos = rigidBodies[bodyIndexB].m_pos;
float sphereRadius = collidables[collidableIndexB].m_radius;
float4 convexPos = rigidBodies[bodyIndexA].m_pos;
float4 convexOrn = rigidBodies[bodyIndexA].m_quat;
computeContactSphereTriangle(i, bodyIndexB, bodyIndexA, collidableIndexB, collidableIndexA,
rigidBodies,collidables,
verticesA,
globalContactsOut, nGlobalContactsOut,maxContactCapacity,
spherePos,sphereRadius,convexPos,convexOrn, f);
return;
}
}
|