-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManifold.h
251 lines (231 loc) · 8.33 KB
/
Manifold.h
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
#pragma once
#include "Vector2.h"
#include "Geom.h"
#include "Collision.h"
#include <limits>
struct Manifold
{
Manifold()
{
body1 = body2 = 0;
isMerged = 0;
collisionsCount = -1;
}
Manifold(RigidBody *body1, RigidBody *body2)
{
this->body1 = body1;
this->body2 = body2;
isMerged = 1;
collisionsCount = 0;
}
void MergeCollision(Collision *newbie)
{
Collision *closest = 0;
float bestdepth = std::numeric_limits<float>::max();
for (int collisionIndex = 0; collisionIndex < collisionsCount; collisionIndex++)
{
Collision *col = &collisions[collisionIndex];
if (newbie->Equals(col, 2.0f))
{
float depth = (newbie->delta1 - col->delta1).SquareLen() + (newbie->delta2 - col->delta2).SquareLen();
if (depth < bestdepth)
{
bestdepth = depth;
closest = col;
}
}
}
if (closest)
{
closest->Refresh(newbie);
}
else
{
assert(collisionsCount < 8);
newbie->isMerged = 1;
newbie->isNewlyCreated = 1;
collisions[collisionsCount++] = *newbie;
}
}
void Update()
{
for (int collisionIndex = 0; collisionIndex < collisionsCount; collisionIndex++)
{
collisions[collisionIndex].isMerged = 0;
collisions[collisionIndex].isNewlyCreated = 0;
}
Vector2f separatingAxis;
if (ComputeSeparatingAxis(separatingAxis))
{
GenerateContacts(separatingAxis);
}
for (int collisionIndex = 0; collisionIndex < collisionsCount;)
{
if (!collisions[collisionIndex].isMerged)
{
collisions[collisionIndex] = collisions[collisionsCount - 1];
collisionsCount--;
}
else
{
collisionIndex++;
}
}
}
RigidBody *body1;
RigidBody *body2;
bool isMerged;
Collision collisions[4]; //in 2d there's always 2 collisions max and 2 more may occur temporarily before merging
int collisionsCount;
private:
bool ComputeSeparatingAxis(Vector2f &separatingAxis)
{
Vector2f axis[4];
axis[0] = body1->coords.xVector;
axis[1] = body1->coords.yVector;
axis[2] = body2->coords.xVector;
axis[3] = body2->coords.yVector;
bool found = 0;
float bestquaddepth = 1e5f;
Vector2f bestaxis;
float min0;
float max0;
for (int i = 0; i < 4; i++)
{
float min1, max1;
float min2, max2;
body1->geom.GetAxisProjectionRange(axis[i], min1, max1);
body2->geom.GetAxisProjectionRange(axis[i], min2, max2);
if ((min1 > max2) || (min2 > max1))
{
return 0;
}
min0 = std::max(min1, min2);
max0 = std::min(max1, max2);
if (min0 > max0) return 0;
float delta = (std::min(max2 - min1, max1 - min2) * axis[i]).SquareLen();
if (bestquaddepth > delta)
{
bestquaddepth = delta;
bestaxis = axis[i];
found = 1;
}
}
separatingAxis = bestaxis;
return found;
}
void GenerateContacts(Vector2f separatingAxis)
{
if (separatingAxis * (body1->coords.pos - body2->coords.pos) < 0.0f)
separatingAxis.Invert();
const int maxSupportPoints = 2;
Vector2f supportPoints1[maxSupportPoints];
Vector2f supportPoints2[maxSupportPoints];
float linearTolerance = 2.0f;
float angularTolerance = 0.05f;
int supportPointsCount1 = body1->geom.GetSupportPointSet(-separatingAxis, supportPoints1);
int supportPointsCount2 = body2->geom.GetSupportPointSet(separatingAxis, supportPoints2);
if ((supportPointsCount1 == 2) && (((supportPoints1[0] - supportPoints1[1])).SquareLen() < linearTolerance * linearTolerance))
{
supportPoints1[0] = (supportPoints1[0] + supportPoints1[1]) * 0.5f;
supportPointsCount1 = 1;
}
if ((supportPointsCount2 == 2) && (((supportPoints2[0] - supportPoints2[1])).SquareLen() < linearTolerance * linearTolerance))
{
supportPoints2[0] = (supportPoints2[0] + supportPoints2[1]) * 0.5f;
supportPointsCount2 = 1;
}
if ((supportPointsCount1 == 1) && (supportPointsCount2 == 1))
{
Vector2f delta = supportPoints2[0] - supportPoints1[0];
//float eps = (delta ^ separatingAxis).SquareLen();
if (delta * separatingAxis > 0.0f)
{
Collision newbie(supportPoints1[0], supportPoints2[0], separatingAxis, body1, body2);
MergeCollision(&newbie);
}
}
else
if ((supportPointsCount1 == 1) && (supportPointsCount2 == 2))
{
Vector2f n = (supportPoints2[1] - supportPoints2[0]).GetPerpendicular();
Vector2f point;
ProjectPointToLine(supportPoints1[0], supportPoints2[0], n, separatingAxis, point);
if ((((point - supportPoints2[0]) * (supportPoints2[1] - supportPoints2[0])) > 0.0f) &&
(((point - supportPoints2[1]) * (supportPoints2[0] - supportPoints2[1])) > 0.0f))
{
Collision newbie(supportPoints1[0], point, separatingAxis, body1, body2);
MergeCollision(&newbie);
}
}
else
if ((supportPointsCount1 == 2) && (supportPointsCount2 == 1))
{
Vector2f n = (supportPoints1[1] - supportPoints1[0]).GetPerpendicular();
Vector2f point;
ProjectPointToLine(supportPoints2[0], supportPoints1[0], n, separatingAxis, point);
if ((((point - supportPoints1[0]) * (supportPoints1[1] - supportPoints1[0])) > 0.0f) &&
(((point - supportPoints1[1]) * (supportPoints1[0] - supportPoints1[1])) > 0.0f))
{
Collision newbie(point, supportPoints2[0], separatingAxis, body1, body2);
MergeCollision(&newbie);
}
}
else
if ((supportPointsCount2 == 2) && (supportPointsCount1 == 2))
{
struct TempColInfo
{
Vector2f point1, point2;
};
TempColInfo tempCol[4];
int tempCols = 0;
for (int i = 0; i < 2; i++)
{
Vector2f n = (supportPoints2[1] - supportPoints2[0]).GetPerpendicular();
if ((supportPoints1[i] - supportPoints2[0]) * n > 0.0)
{
Vector2f point;
ProjectPointToLine(supportPoints1[i], supportPoints2[0], n, separatingAxis, point);
if ((((point - supportPoints2[0]) * (supportPoints2[1] - supportPoints2[0])) >= 0.0f) &&
(((point - supportPoints2[1]) * (supportPoints2[0] - supportPoints2[1])) > 0.0f))
{
tempCol[tempCols].point1 = supportPoints1[i];
tempCol[tempCols].point2 = point;
tempCols++;
// TryToAdd(new(collisionManager->GetNewNode()) RigidRigid::Collision(supportPoint1[i], point, separatingAxis, proxy1, proxy2));
}
}
}
for (int i = 0; i < 2; i++)
{
Vector2f n = (supportPoints1[1] - supportPoints1[0]).GetPerpendicular();
if ((supportPoints2[i] - supportPoints1[0]) * n > 0.0)
{
Vector2f point;
ProjectPointToLine(supportPoints2[i], supportPoints1[0], n, separatingAxis, point);
if ((((point - supportPoints1[0]) * (supportPoints1[1] - supportPoints1[0])) >= 0.0f) &&
(((point - supportPoints1[1]) * (supportPoints1[0] - supportPoints1[1])) > 0.0f))
{
tempCol[tempCols].point1 = point;
tempCol[tempCols].point2 = supportPoints2[i];
tempCols++;
}
}
}
if (tempCols == 1) //buggy but must work
{
Collision newbie(tempCol[0].point1, tempCol[0].point2, separatingAxis, body1, body2);
MergeCollision(&newbie);
}
if (tempCols >= 2) //means only equality, but clamp to two points
{
Collision newbie1(tempCol[0].point1, tempCol[0].point2, separatingAxis, body1, body2);
MergeCollision(&newbie1);
Collision newbie2(tempCol[1].point1, tempCol[1].point2, separatingAxis, body1, body2);
MergeCollision(&newbie2);
//}
}
}
}
};