-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.cpp
407 lines (357 loc) · 14.3 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
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
#include <cmath>
#include "Collision.h"
namespace sfcollision
{
struct interval{float min, max;};
////////////////////////////////////////////////////////////////////////////////
// STATIC FUNCTIONS ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Dot product of A and B
static inline float inner_product(const sf::Vector2f A, const sf::Vector2f B)
{
return A.x*B.x + A.y*B.y;
}
////////////////////////////////////////////////////////////////////////////////
// Returns vec as a unit vector
static inline sf::Vector2f normalize(sf::Vector2f vec)
{
float magnitude = std::sqrt(vec.x*vec.x + vec.y*vec.y);
vec.x /= magnitude;
vec.y /= magnitude;
return vec;
}
////////////////////////////////////////////////////////////////////////////////
// Rotate vector by +90deg to get a perpendicular Vector2f
static inline sf::Vector2f perpendicular(const sf::Vector2f vec)
{
// Normal vector transformed from edge using (x, y) => (-y, x).
// The transformation (x, y) => (y, -x) is also acceptable, but would
// would rotate it -90deg instead of +90deg.
return sf::Vector2f(-vec.y, vec.x);
}
////////////////////////////////////////////////////////////////////////////////
// Returns the distance between two points squared.
// Distance formula uses a square root, but this value is usually squared anyway
// so this squared value is returned to avoid wasting clock cycles.
static inline float distance_squared(const sf::Vector2f A, const sf::Vector2f B)
{
return (B.x - A.x)*(B.x - A.x) + (B.y - A.y)*(B.y - A.y);
}
////////////////////////////////////////////////////////////////////////////////
// Get the global position of the center of a CircleShape
static sf::Vector2f get_circle_center_position(const sf::CircleShape& circle)
{
sf::Vector2f local_center = circle.getPoint(0);
local_center.y += circle.getRadius(); // determined from testing
return circle.getTransform().transformPoint(local_center);
}
////////////////////////////////////////////////////////////////////////////////
static interval project(const sf::Vector2f& point, const sf::Vector2f& axis)
{
float dot = inner_product(point, normalize(axis));
return interval {dot, dot};
}
////////////////////////////////////////////////////////////////////////////////
static interval project(const sf::CircleShape& circle, const sf::Vector2f& axis)
{
interval I = project(get_circle_center_position(circle), normalize(axis));
I.min -= circle.getRadius();
I.max += circle.getRadius();
return I;
}
////////////////////////////////////////////////////////////////////////////////
static interval project(const sf::Shape& polygon, const sf::Vector2f& axis)
{
sf::Transform transform = polygon.getTransform();
unsigned point_count = polygon.getPointCount();
sf::Vector2f normalized_axis = normalize(axis);
sf::Vector2f point = transform.transformPoint(polygon.getPoint(0));
float dot = inner_product(point, normalized_axis);
interval I {dot, dot};
for (unsigned i = 1; i < point_count; ++i)
{
point = transform.transformPoint(polygon.getPoint(i));
dot = inner_product(point, normalized_axis);
if (dot < I.min)
{
I.min = dot;
}
else if (dot > I.max)
{
I.max = dot;
}
}
return I;
}
////////////////////////////////////////////////////////////////////////////////
static interval project(const sf::Sprite& sprite, const sf::Vector2f& axis)
{
sf::Transform transform = sprite.getTransform();
sf::FloatRect local_rect = sprite.getLocalBounds();
sf::Vector2f normalized_axis = normalize(axis);
std::vector<sf::Vector2f> points {
sf::Vector2f(local_rect.left, local_rect.top), // top left
sf::Vector2f(local_rect.left + local_rect.width, local_rect.top), // top right
sf::Vector2f(local_rect.left + local_rect.width, local_rect.top + local_rect.height), // bottom right
sf::Vector2f(local_rect.left, local_rect.top + local_rect.height) // bottom left
};
sf::Vector2f point = transform.transformPoint(points[0]);
float dot = inner_product(point, normalized_axis);
interval I {dot, dot};
for (unsigned i = 1; i < points.size(); ++i)
{
point = transform.transformPoint(points[i]);
dot = inner_product(point, normalized_axis);
if (dot < I.min)
{
I.min = dot;
}
else if (dot > I.max)
{
I.max = dot;
}
}
return I;
}
////////////////////////////////////////////////////////////////////////////////
static inline bool is_overlap(const interval& A, const interval& B)
{
return !(A.max < B.min || B.max < A.min);
}
////////////////////////////////////////////////////////////////////////////////
// return all four points that make up the box of a Sprite
static std::vector<sf::Vector2f> get_sprite_points(const sf::Sprite& sprite)
{
sf::FloatRect local_rect = sprite.getLocalBounds();
return std::vector<sf::Vector2f> {
sf::Vector2f(local_rect.left, local_rect.top), // top left
sf::Vector2f(local_rect.left + local_rect.width, local_rect.top), // top right
sf::Vector2f(local_rect.left + local_rect.width, local_rect.top + local_rect.height), // bottom right
sf::Vector2f(local_rect.left, local_rect.top + local_rect.height) // bottom left
};
}
////////////////////////////////////////////////////////////////////////////////
template <typename T>
static bool test_projection_overlap_using_polygon(const sf::Shape& polygon, const T& other)
{
unsigned point_count = polygon.getPointCount();
sf::Transform transform = polygon.getTransform();
for (unsigned i = 0; i < point_count; ++i)
{
sf::Vector2f edge = transform.transformPoint(polygon.getPoint((i+1) % point_count))
- transform.transformPoint(polygon.getPoint(i));
sf::Vector2f perp = perpendicular(edge);
interval projA = project(polygon, perp);
interval projB = project(other, perp);
if (!is_overlap(projA, projB))
{
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool test_projection_overlap_using_circle(const sf::CircleShape& circle, const sf::Shape& polygon)
{
unsigned point_count = polygon.getPointCount();
sf::Transform transform = polygon.getTransform();
sf::Vector2f circle_center = get_circle_center_position(circle);
for (unsigned i = 0; i < point_count; ++i)
{
sf::Vector2f poly_point = transform.transformPoint(polygon.getPoint(i));
sf::Vector2f edge = poly_point - circle_center;
interval projA = project(polygon, edge);
interval projB = project(circle, edge);
if (!is_overlap(projA, projB))
{
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
template <typename T>
static bool test_projection_overlap_using_sprite(const sf::Sprite& sprite, const T& other)
{
sf::Transform transform = sprite.getTransform();
auto points = get_sprite_points(sprite);
for (unsigned i = 0; i < points.size(); ++i)
{
sf::Vector2f edge = transform.transformPoint(points[i+1 % points.size()])
- transform.transformPoint(points[i]);
sf::Vector2f perp = perpendicular(edge);
interval projA = project(sprite, perp);
interval projB = project(other, perp);
if (!is_overlap(projA, projB))
{
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Shape& A, const sf::Shape& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Vector2f& A, const sf::Vector2f& B)
{
return A == B;
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Vector2f& A, const sf::RectangleShape& B)
{
return test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Vector2f& A, const sf::CircleShape& B)
{
float R = B.getRadius();
sf::Vector2f B_pos = get_circle_center_position(B);
float Dsq = distance_squared(A, B_pos);
float Rsq = R*R;
return Dsq <= Rsq;
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Vector2f& A, const sf::ConvexShape& B)
{
return test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Vector2f& A, const sf::Sprite& B)
{
return test_projection_overlap_using_sprite(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::RectangleShape& A, const sf::Vector2f& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::RectangleShape& A, const sf::RectangleShape& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::RectangleShape& A, const sf::CircleShape& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_circle(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::RectangleShape& A, const sf::ConvexShape& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::RectangleShape& A, const sf::Sprite& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_sprite(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::CircleShape& A, const sf::Vector2f& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::CircleShape& A, const sf::RectangleShape& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::CircleShape& A, const sf::CircleShape& B)
{
float RA = A.getRadius();
float RB = B.getRadius();
float Dsq = distance_squared(get_circle_center_position(A),
get_circle_center_position(B));
return Dsq <= (RA+RB)*(RA+RB);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::CircleShape& A, const sf::ConvexShape& B)
{
return test_projection_overlap_using_circle(A, B) &&
test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::CircleShape& A, const sf::Sprite& B)
{
bool is_projection_collision_circle_sprite = true;
std::vector<sf::Vector2f> points = get_sprite_points(B);
sf::Transform transform = B.getTransform();
sf::Vector2f circle_center = get_circle_center_position(A);
for (unsigned i = 0; i < points.size(); ++i)
{
sf::Vector2f point = transform.transformPoint(points[i]);
sf::Vector2f edge = point - circle_center;
interval projA = project(B, edge);
interval projB = project(A, edge);
if (!is_overlap(projA, projB))
{
is_projection_collision_circle_sprite = false;
}
}
return is_projection_collision_circle_sprite &&
test_projection_overlap_using_sprite(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::ConvexShape& A, const sf::Vector2f& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::ConvexShape& A, const sf::RectangleShape& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::ConvexShape& A, const sf::CircleShape& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::ConvexShape& A, const sf::ConvexShape& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_polygon(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::ConvexShape& A, const sf::Sprite& B)
{
return test_projection_overlap_using_polygon(A, B) &&
test_projection_overlap_using_sprite(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Sprite& A, const sf::Vector2f& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Sprite& A, const sf::RectangleShape& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Sprite& A, const sf::CircleShape& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Sprite& A, const sf::ConvexShape& B)
{
return check_collision(B, A);
}
////////////////////////////////////////////////////////////////////////////////
bool check_collision(const sf::Sprite& A, const sf::Sprite& B)
{
return test_projection_overlap_using_sprite(A, B) &&
test_projection_overlap_using_sprite(B, A);
}
} // !sfcollision