-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision.cpp
373 lines (297 loc) · 8.94 KB
/
collision.cpp
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
#include "stdafx.h"
#include "body.h"
#include "world.h"
#include "contact.h"
#include "collision.h"
BOOL CircleCircle( Body *a, Body *b ,ContactSolver *contact_info)
{
GeoCircle *A = (GeoCircle *)a->shape;
GeoCircle *B = (GeoCircle *)b->shape;
Vector2 normal = b->position - a->position;
float dist_sqr = normal.LengthSq( );
float radius = A->radius + B->radius;
// Not in contact
if(dist_sqr >= radius * radius)
{
contact_info->contact_count = 0;
return FALSE;
}
float distance = sqrt( dist_sqr);
contact_info->contact_count = 1;
if(distance == 0.0f)
{
contact_info->penetration = A->radius;
contact_info->normal = Vector2( 1, 0 );
contact_info->contacts [0] = a->position;
}
else
{
contact_info->penetration = radius - distance;
contact_info->normal = normal / distance;
contact_info->contacts[0] = contact_info->normal * A->radius + a->position;
}
return TRUE;
}
BOOL CirclePolygon( Body *a, Body *b ,ContactSolver *contact_info)
{
GeoCircle *A = (GeoCircle *)a->shape;
GeoPolygon *B = (GeoPolygon*)b->shape;
contact_info->contact_count = 0;
// Transofrmo a al espacio del poligono, que es mas facil de tratar que en el global
// o en el espacio del circulo
Vector2 center = a->position;
center = B->matWorld.Transpose( ) * (center - b->position);
// Calcular el lado donde se produce la minima penetracion
float separation = -1000000;
int faceNormal = 0;
for(int i = 0; i < B->m_vertexCount; ++i)
{
float s = dot( B->normals[i], center - B->vertices[i] );
if(s > A->radius)
return FALSE;
if(s > separation)
{
separation = s;
faceNormal = i;
}
}
// algortimo sacado de
// TODO: la pagina no la encuentro mas...
// Grab face's vertices
Vector2 v1 = B->vertices[faceNormal];
Vector2 v2 = B->vertices[faceNormal+1];
// Check to see if center is within polygon
if(separation < EPSILON)
{
contact_info->contact_count = 1;
contact_info->normal = -(B->matWorld * B->normals[faceNormal]);
contact_info->contacts[0] = contact_info->normal * A->radius + a->position;
contact_info->penetration = A->radius;
return TRUE;
}
// Determine which voronoi region of the edge center of circle lies within
float dot1 = dot( center - v1, v2 - v1 );
float dot2 = dot( center - v2, v1 - v2 );
contact_info->penetration = A->radius - separation;
// Closest to v1
if(dot1 <= 0.0f)
{
if((center-v1).LengthSq() > A->radius * A->radius)
return FALSE;
contact_info->contact_count = 1;
Vector2 n = v1 - center;
n = B->matWorld * n;
n.Normalize( );
contact_info->normal = n;
v1 = B->matWorld * v1 + b->position;
contact_info->contacts[0] = v1;
}
// Closest to v2
else if(dot2 <= 0.0f)
{
if((center- v2).LengthSq() > A->radius * A->radius)
return FALSE;
contact_info->contact_count = 1;
Vector2 n = v2 - center;
v2 = B->matWorld * v2 + b->position;
contact_info->contacts[0] = v2;
n = B->matWorld * n;
n.Normalize( );
contact_info->normal = n;
}
// Closest to face
else
{
Vector2 n = B->normals[faceNormal];
if(dot( center - v1, n ) > A->radius)
return FALSE;
n = B->matWorld * n;
contact_info->normal = -n;
contact_info->contacts[0] = contact_info->normal * A->radius + a->position;
contact_info->contact_count = 1;
}
return TRUE;
}
float FindAxisLeastPenetration( int *faceIndex, Body *a, Body *b )
{
GeoPolygon *A = (GeoPolygon*)a->shape;
GeoPolygon *B = (GeoPolygon*)b->shape;
float bestDistance = -100000;
int bestIndex = -1;
for(int i = 0; i < A->m_vertexCount; ++i)
{
// Retrieve a face normal from A
Vector2 n = A->normals[i];
Vector2 nw = A->matWorld * n;
// Transform face normal into B's model space
Matrix2 buT = B->matWorld.Transpose( );
n = buT * nw;
// Retrieve support point from B along -n
Vector2 s = B->GetSupport( -n );
// Retrieve vertex on face from A, transform into
// B's model space
Vector2 v = A->vertices[i];
v = A->matWorld * v + a->position;
v -= b->position;
v = buT * v;
// Compute penetration distance (in B's model space)
float d = dot( n, s - v );
// Store greatest distance
if(d > bestDistance)
{
bestDistance = d;
bestIndex = i;
}
}
*faceIndex = bestIndex;
return bestDistance;
}
void FindIncidentFace( Vector2 *v, Body *RefBody, Body *IncBody, int referenceIndex )
{
GeoPolygon *RefPoly = (GeoPolygon *)RefBody->shape;
GeoPolygon *IncPoly = (GeoPolygon *)IncBody->shape;
Vector2 referenceNormal = RefPoly->normals[referenceIndex];
// Calculate normal in incident's frame of reference
referenceNormal = RefPoly->matWorld * referenceNormal; // To world space
referenceNormal = IncPoly->matWorld.Transpose( ) * referenceNormal; // To incident's model space
// Find most anti-normal face on incident polygon
int incidentFace = 0;
float minDot = 100000;
for(int i = 0; i < IncPoly->m_vertexCount; ++i)
{
float d = dot( referenceNormal, IncPoly->normals[i] );
if(d < minDot)
{
minDot = d;
incidentFace = i;
}
}
// Assign face vertices for incidentFace
v[0] = IncPoly->matWorld * IncPoly->vertices[incidentFace] + IncBody->position;
incidentFace = incidentFace + 1 >= IncPoly->m_vertexCount ? 0 : incidentFace + 1;
v[1] = IncPoly->matWorld * IncPoly->vertices[incidentFace] + IncBody->position;
}
int Clip( Vector2 n, float c, Vector2 *face )
{
int sp = 0;
Vector2 out[2] = {
face[0],
face[1]
};
// Retrieve distances from each endpoint to the line
// d = ax + by - c
float d1 = dot( n, face[0] ) - c;
float d2 = dot( n, face[1] ) - c;
// If negative (behind plane) clip
if(d1 <= 0.0f) out[sp++] = face[0];
if(d2 <= 0.0f) out[sp++] = face[1];
// If the points are on different sides of the plane
if(d1 * d2 < 0.0f) // less than to ignore -0.0f
{
// Push interesection point
float alpha = d1 / (d1 - d2);
out[sp] = face[0] + (face[1] - face[0])*alpha;
++sp;
}
// Assign our new converted values
face[0] = out[0];
face[1] = out[1];
return sp;
}
BOOL PolygonPolygon( Body *a, Body *b ,ContactSolver *contact_info)
{
GeoPolygon *A = (GeoPolygon*)a->shape;
GeoPolygon *B = (GeoPolygon*)b->shape;
contact_info->contact_count = 0;
// Check for a separating axis with A's face planes
int faceA;
float penetrationA = FindAxisLeastPenetration( &faceA, a, b );
if(penetrationA >= 0.0f)
return FALSE;
// Check for a separating axis with B's face planes
int faceB;
float penetrationB = FindAxisLeastPenetration( &faceB, b, a );
if(penetrationB >= 0.0f)
return FALSE;
int referenceIndex;
bool flip; // Always point from a to b
Body *RefBody; // Reference
Body *IncBody; // Incident
// Determine which shape contains reference face
if(BiasGreaterThan( penetrationA, penetrationB ))
{
RefBody = a;
IncBody = b;
referenceIndex = faceA;
flip = false;
}
else
{
RefBody = b;
IncBody = a;
referenceIndex = faceB;
flip = true;
}
GeoPolygon *RefPoly = (GeoPolygon *)RefBody->shape; // Reference
GeoPolygon *IncPoly = (GeoPolygon *)IncBody->shape; // Incident
// World space incident face
Vector2 incidentFace[2];
FindIncidentFace( incidentFace, RefBody, IncBody, referenceIndex );
// y
// ^ ->n ^
// +---c ------posPlane--
// x < | i |\
// +---+ c-----negPlane--
// \ v
// r
//
// r : reference face
// i : incident poly
// c : clipped point
// n : incident normal
// Setup reference face vertices
Vector2 v1 = RefPoly->vertices[referenceIndex];
Vector2 v2 = RefPoly->vertices[referenceIndex+1];
// Transform vertices to world space
v1 = RefPoly->matWorld * v1 + RefBody->position;
v2 = RefPoly->matWorld * v2 + RefBody->position;
// Calculate reference face side normal in world space
Vector2 sidePlaneNormal = (v2 - v1);
sidePlaneNormal.Normalize( );
// Orthogonalize
Vector2 refFaceNormal( sidePlaneNormal.y, -sidePlaneNormal.x );
// ax + by = c
// c is distance from origin
float refC = dot( refFaceNormal, v1 );
float negSide = -dot( sidePlaneNormal, v1 );
float posSide = dot( sidePlaneNormal, v2 );
// Clip incident face to reference face side planes
if(Clip( -sidePlaneNormal, negSide, incidentFace ) < 2)
return FALSE; // Due to floating point error, possible to not have required points
if(Clip( sidePlaneNormal, posSide, incidentFace ) < 2)
return FALSE; // Due to floating point error, possible to not have required points
// Flip
contact_info->normal = flip ? -refFaceNormal : refFaceNormal;
// Keep points behind reference face
int cp = 0; // clipped points behind reference face
float separation = dot( refFaceNormal, incidentFace[0] ) - refC;
if(separation <= 0.0f)
{
contact_info->contacts[cp] = incidentFace[0];
contact_info->penetration = -separation;
++cp;
}
else
contact_info->penetration = 0;
separation = dot( refFaceNormal, incidentFace[1] ) - refC;
if(separation <= 0.0f)
{
contact_info->contacts[cp] = incidentFace[1];
contact_info->penetration += -separation;
++cp;
// Average penetration
contact_info->penetration /= (float)cp;
}
contact_info->contact_count = cp;
return TRUE;
}