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
|
// Ripped from Magic Software
#include "Include\dRay.h"
#include "dxRay.h"
int Find(const dVector3 Origin, dVector3 Direction, dReal Length, const dVector3 CCPos, const dMatrix3 CCRot, dReal CCRadius, dReal CCLength, dReal T[2]){
dVector3 U, V, W;
Decompose(CCRot, U, V, W);
dVector3 CCOrigin;
CCOrigin[0] = CCPos[0] - (W[0] * CCLength / 2);
CCOrigin[1] = CCPos[1] - (W[1] * CCLength / 2);
CCOrigin[2] = CCPos[2] - (W[2] * CCLength / 2);
CCOrigin[3] = CCPos[3] - (W[3] * CCLength / 2);
dVector3 D;
D[0] = dDOT(U, Direction);
D[1] = dDOT(V, Direction);
D[2] = dDOT(W, Direction);
dReal DMag = Length;
dReal InvDMag = REAL(1.0) / DMag;
dVector3 Diff;
Diff[0] = Origin[0] - CCOrigin[0];
Diff[1] = Origin[1] - CCOrigin[1];
Diff[2] = Origin[2] - CCOrigin[2];
Diff[3] = Origin[3] - CCOrigin[3];
dVector3 P;
P[0] = dDOT(U, Diff);
P[1] = dDOT(V, Diff);
P[2] = dDOT(W, Diff);
dReal CCRadiusSq = CCRadius * CCRadius;
dReal Epsilon = 1e-12f;
if (dFabs(D[2]) >= REAL(1.0) - Epsilon){ // line is parallel to capsule axis
dReal Discr = CCRadiusSq - P[0] * P[0] - P[1] * P[1];
if (Discr >= REAL(0.0)){
dReal Root = dSqrt(Discr);
T[0] = (-P[2] + Root) * InvDMag;
T[1] = (CCLength - P[2] + Root) * InvDMag;
return 2;
}
else return 0;
}
// test intersection with infinite cylinder
dReal A = D[0] * D[0] + D[1] * D[1];
dReal B = P[0] * D[0] + P[1] * D[1];
dReal C = P[0] * P[0] + P[1] * P[1] - CCRadiusSq;
dReal Discr = B * B - A * C;
if (Discr < REAL(0.0)){ // line does not intersect infinite cylinder
return 0;
}
int Count = 0;
if (Discr > REAL(0.0)){ // line intersects infinite cylinder in two places
dReal Root = dSqrt(Discr);
dReal Inv = REAL(1.0) / A;
dReal TTemp = (-B - Root) * Inv;
dReal Tmp = P[2] + TTemp * D[2];
if (REAL(0.0) <= Tmp && Tmp <= CCLength){
T[Count++] = TTemp * InvDMag;
}
TTemp = (-B + Root) * Inv;
Tmp = P[2] + TTemp * D[2];
if (REAL(0.0) <= Tmp && Tmp <= CCLength){
T[Count++] = TTemp * InvDMag;
}
if (Count == 2){ // line intersects capsule wall in two places
return 2;
}
}
else{ // line is tangent to infinite cylinder
dReal TTemp = -B / A;
dReal Tmp = P[2] + TTemp * D[2];
if (REAL(0.0) <= Tmp && Tmp <= CCLength){
T[0] = TTemp * InvDMag;
return 1;
}
}
// test intersection with bottom hemisphere
// fA = 1
B += P[2] * D[2];
C += P[2] * P[2];
Discr = B * B - C;
if (Discr > REAL(0.0)){
dReal Root = dSqrt(Discr);
dReal TTemp = -B - Root;
dReal Tmp = P[2] + TTemp * D[2];
if (Tmp <= REAL(0.0)){
T[Count++] = TTemp * InvDMag;
if (Count == 2){
return 2;
}
}
TTemp = -B + Root;
Tmp = P[2] + TTemp * D[2];
if (Tmp <= REAL(0.0)){
T[Count++] = TTemp * InvDMag;
if (Count == 2){
return 2;
}
}
}
else if (Discr == REAL(0.0)){
dReal TTemp = -B;
dReal Tmp = P[2] + TTemp * D[2];
if (Tmp <= REAL(0.0)){
T[Count++] = TTemp * InvDMag;
if (Count == 2){
return 2;
}
}
}
// test intersection with top hemisphere
// fA = 1
B -= D[2] * CCLength;
C += CCLength * (CCLength - REAL(2.0) * P[2]);
Discr = B * B - C;
if (Discr > REAL(0.0)){
dReal Root = dSqrt(Discr);
dReal TTemp = -B - Root;
dReal Tmp = P[2] + TTemp * D[2];
if (Tmp >= CCLength){
T[Count++] = TTemp * InvDMag;
if (Count == 2){
return 2;
}
}
TTemp = -B + Root;
Tmp = P[2] + TTemp * D[2];
if (Tmp >= CCLength){
T[Count++] = TTemp * InvDMag;
if (Count == 2){
return 2;
}
}
}
else if (Discr == REAL(0.0)){
dReal TTemp = -B;
dReal Tmp = P[2] + TTemp * D[2];
if (Tmp >= CCLength){
T[Count++] = TTemp * InvDMag;
if (Count == 2){
return 2;
}
}
}
return Count;
}
int dCollideCCR(dxGeom* RayGeom, dxGeom* CCGeom, int Flags, dContactGeom* Contacts, int Stride){
const dVector3& CCPos = *(const dVector3*)dGeomGetPosition(CCGeom);
const dMatrix3& CCRot = *(const dMatrix3*)dGeomGetRotation(CCGeom);
dReal CCRadius, CCLength;
dGeomCCylinderGetParams(CCGeom, &CCRadius, &CCLength);
dVector3 Origin, Direction;
dGeomRayGet(RayGeom, Origin, Direction);
dReal Length = dGeomRayGetLength(RayGeom);
dReal T[2];
int Count = Find(Origin, Direction, Length, CCPos, CCRot, CCRadius, CCLength, T);
int ContactCount = 0;
for (int i = 0; i < Count; i++){
if (T[i] >= 0.0){
dContactGeom* Contact = CONTACT(Flags, Contacts, ContactCount, Stride);
Contact->pos[0] = Origin[0] + T[i] * Direction[0] * Length;
Contact->pos[1] = Origin[1] + T[i] * Direction[1] * Length;
Contact->pos[2] = Origin[2] + T[i] * Direction[2] * Length;
Contact->pos[3] = Origin[3] + T[i] * Direction[3] * Length;
//Contact->normal = 0;
Contact->depth = 0.0f;
Contact->g1 = RayGeom;
Contact->g2 = CCGeom;
ContactCount++;
}
}
return ContactCount;
}
|