-
Notifications
You must be signed in to change notification settings - Fork 1
/
detectable.cpp
464 lines (395 loc) · 15.6 KB
/
detectable.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
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
#include "detectable.h"
#include "mathfunctions.h"
#include <cmath>
Detectable::Detectable()
{
upperLeft = Point(0,0);
upperRight = Point(0,0);
lowerRight = Point(0,0);
lowerLeft = Point(0,0);
center = Point(0,0);
width = 0;
height = 0;
dType = RECTANGLE;
substance = EMPTY;
fixed = true;
moves = false;
}
Detectable::Detectable(Point upperLeft, Point lowerRight, Substance substance, DetectableType type, bool fixed, bool moves)
{
this->upperLeft = upperLeft;
this->lowerRight = lowerRight;
this->substance = substance;
this->dType = type;
this->fixed = fixed;
this->moves = moves;
width = lowerRight.getX() - upperLeft.getX();
height = upperLeft.getY() - lowerRight.getY();
upperRight = Point(lowerRight.getX(), upperLeft.getY());
lowerLeft = Point(upperLeft.getX(), lowerRight.getY());
center = Point(upperLeft.getX() + width / 2.0, upperLeft.getY() - height / 2.0);
}
Detectable::Detectable(Point upperLeft, double width, double height, Substance substance, DetectableType type, bool fixed, bool moves)
{
this->upperLeft = upperLeft;
this->width = width;
this->height = height;
this->dType = type;
this->substance = substance;
this->fixed = fixed;
this->moves = moves;
lowerRight = Point(upperLeft.getX() + width, upperLeft.getY() - height);
upperRight = Point(lowerRight.getX(), upperLeft.getY());
lowerLeft = Point(upperLeft.getX(), lowerRight.getY());
center = Point(upperLeft.getX() + width / 2.0, upperLeft.getY() - height / 2.0);
}
Detectable::Detectable(Point center, double radius, Substance substance, DetectableType type, bool fixed, bool moves)
{
this->center = center;
this->dType = type;
this->substance = substance;
this->fixed = fixed;
this->moves = moves;
width = radius * 2.0;
height = radius * 2.0;
upperLeft = Point(center.getX() - radius, center.getY() + radius);
lowerRight = Point(center.getX() + radius, center.getY() - radius);
upperRight = Point(lowerRight.getX(), upperLeft.getY());
lowerLeft = Point(upperLeft.getX(), lowerRight.getY());
}
double Detectable::getWidth()
{
return width;
}
double Detectable::getHeight()
{
return height;
}
double Detectable::getRadius()
{
return width / 2.0;
}
DetectableType Detectable::getDetectableType()
{
return dType;
}
Substance Detectable::getSubstance()
{
return substance;
}
Point Detectable::getCenter()
{
return center;
}
Point Detectable::getUpperLeft()
{
return upperLeft;
}
Point Detectable::getLowerRight()
{
return lowerRight;
}
void Detectable::circleOnCircleResponce(Detectable* c1, Detectable* c2, double distance, double angle)
{
//Signify that the two circles were touched by each other so they can handle their special functions.
c1->wasTouchedBy(c2, c1->center, c2->center, 0,0);
c2->wasTouchedBy(c1, c2->center, c1->center, 0,0);
//If either the two circles has no substance, then we move neither object.
if(c1->substance == EMPTY || c2->substance == EMPTY)
return;
if(!c1->fixed && c2->fixed)
{
c1->moveBy(-cos(angle) * distance, -sin(angle) * distance);
}
else if(!c1->fixed && !c2->fixed)
{
c1->moveBy(-cos(angle) * distance / 2.0, -sin(angle) * distance / 2.0);
c2->moveBy(cos(angle) * distance / 2.0, sin(angle) * distance / 2.0);
}
else if(c1->fixed && !c2->fixed)
{
c2->moveBy(cos(angle) * distance, sin(angle) * distance);
}
else
{
//c1->moveBy(-cos(angle) * distance / 2.0, -sin(angle) * distance / 2.0);
//c2->moveBy(cos(angle) * distance / 2.0, sin(angle) * distance / 2.0);
}
}
void Detectable::circleOnRectResponce(Detectable* circle, Detectable* rect, double xMovement, double yMovement)
{
//Signify that the circle was touched by the rect and the rect was touched by the circle.
circle->wasTouchedBy(rect, circle->center, rect->center, xMovement, yMovement);
rect->wasTouchedBy(circle, rect->center, circle->center, -xMovement, -yMovement);
//If either the circle or rectangle has no substance, then we move neither object.
if(circle->substance == EMPTY || rect->substance == EMPTY)
return;
//If the circle is moveable but the rect isnt, move the circle.
if(!circle->fixed && rect->fixed)
{
circle->moveBy(xMovement, yMovement);
}
//If the circle and rect are both moveable, move them both in opposite directions.
else if(!circle->fixed && !rect->fixed)
{
circle->moveBy(xMovement / 2.0, yMovement / 2.0);
rect->moveBy( -xMovement / 2.0, -yMovement / 2.0);
}
//If the circle is not movable but the rect is, move the rect.
else if(circle->fixed && !rect->fixed)
{
rect->moveBy(-xMovement, -yMovement);
}
//If the circle and rect are not movable, just move them apart in opposite directions.
else
{
circle->moveBy(xMovement / 2.0, yMovement / 2.0);
rect->moveBy(-xMovement / 2.0, -yMovement / 2.0);
}
}
void Detectable::circleOnRectDetection(Detectable* circle, Detectable* rect)
{
//Initial high level check. Get the distance between the two centers.
double distanceBetweenCenters = distBetween(circle->center, rect->center);
//If the distance between the two centers is bigger than the circles radius plus the
//bigger between the rect's width and height, then there was no collision. Stop here.
if(distanceBetweenCenters > circle->getRadius() + max(rect->width, rect->height))
return;
//Now that we have done a broad check and the two objects were close enough,
//let's get more precise to see if there really was a collision.
bool isAbove = circle->center.getY() > rect->upperLeft.getY();
bool isBelow = circle->center.getY() < rect->lowerRight.getY();
bool isToLeft = circle->center.getX() < rect->upperLeft.getX();
bool isToRight = circle->center.getX() > rect->lowerRight.getX();
//If the circle is above and within the X values of the rectangle.
if(isAbove && !isToLeft && !isToRight)
{
//Check the Y distance between the center of the Rect and center of the Circle.
double distance = distBetween(Point(0, circle->center.getY()), Point(0, rect->center.getY()));
//Get the length of the circle's radius plus half the rects height.
double radiusPlusHeight = circle->getRadius() + rect->height / 2.0;
if(distance < radiusPlusHeight)
{
//There was a collision.
circleOnRectResponce(circle, rect, 0, radiusPlusHeight - distance);
}
//There was no collision.
return;
}
//If the circle is below and within the X values of the rectangle.
else if(isBelow && !isToLeft && !isToRight)
{
//Check the Y distance between the center of the Rect and center of the Circle.
double distance = distBetween(Point(0, circle->center.getY()), Point(0, rect->center.getY()));
//Get the length of the circle's radius plus half the rects height.
double radiusPlusHeight = circle->getRadius() + rect->height / 2.0;
if(distance < radiusPlusHeight)
{
//There was a collision.
circleOnRectResponce(circle, rect, 0, -(radiusPlusHeight - distance));
}
//There was no collision.
return;
}
//If the circle is to the left and within the Y values of the rectangle.
else if(isToLeft && !isAbove && !isBelow)
{
//Check the X distance between the center of the Rect and center of the Circle.
double distance = distBetween(Point(circle->center.getX(), 0), Point(rect->center.getX(), 0));
//Get the length of the circle's radius plus half the rects width.
double radiusPlusWidth = circle->getRadius() + rect->width / 2.0;
if(distance < radiusPlusWidth)
{
//There was a collision.
circleOnRectResponce(circle, rect, -(radiusPlusWidth - distance), 0);
}
//There was no collision.
return;
}
//If the circle is to the right and within the Y values of the rectangle.
else if(isToRight && !isAbove && !isBelow)
{
//Check the X distance between the center of the Rect and center of the Circle.
double distance = distBetween(Point(circle->center.getX(), 0), Point(rect->center.getX(), 0));
//Get the length of the circle's radius plus half the rects width.
double radiusPlusWidth = circle->getRadius() + rect->width / 2.0;
if(distance < radiusPlusWidth)
{
//There was a collision.
circleOnRectResponce(circle, rect, radiusPlusWidth - distance, 0);
}
//There was no collision.
return;
}
//If the circle is above and to the left of the rectangle.
else if(isAbove && isToLeft)
{
//Check the distance the center of the circle is to the upperLeft corner of the rect.
double distance = distBetween(circle->center, rect->upperLeft);
if(circle->getRadius() > distance)
{
//There was a collision
double angle = calcAngle(rect->upperLeft, circle->center);
circleOnRectResponce(circle, rect, (circle->getRadius() - distance) * cos(angle), (circle->getRadius() - distance) * sin(angle));
}
//There was no collision.
return;
}
//If the circle is above and to the right of the rectangle.
else if(isAbove && isToRight)
{
//Check the distance the center of the circle is to the upperRight corner of the rect.
double distance = distBetween(circle->center, rect->upperRight);
if(circle->getRadius() > distance)
{
//There was a collision
double angle = calcAngle(rect->upperLeft, circle->center);
circleOnRectResponce(circle, rect, (circle->getRadius() - distance) * cos(angle), (circle->getRadius() - distance) * sin(angle));
}
//There was no collision.
return;
}
//If the circle is below and to the left of the rectangle.
else if(isBelow && isToLeft)
{
//Check the distance the center of the circle is to the lowerLeft corner of the rect.
double distance = distBetween(circle->center, rect->lowerLeft);
if(circle->getRadius() > distance)
{
//There was a collision
double angle = calcAngle(rect->upperLeft, circle->center);
circleOnRectResponce(circle, rect, (circle->getRadius() - distance) * cos(angle), (circle->getRadius() - distance) * sin(angle));
}
//There was no collision.
return;
}
//If the circle is below and to the right of the rectangle.
else if(isBelow && isToRight)
{
//Check the distance the center of the circle is to the lowerRight corner of the rect.
double distance = distBetween(circle->center, rect->lowerRight);
if(circle->getRadius() > distance)
{
//There was a collision
double angle = calcAngle(rect->upperLeft, circle->center);
circleOnRectResponce(circle, rect, (circle->getRadius() - distance) * cos(angle), (circle->getRadius() - distance) * sin(angle));
}
//There was no collision.
return;
}
//The center of the circle is within the rectangle.
else
{
//See if the circle's center is above and if is to the right of the rect's center.
bool aboveCenter = circle->center.getY() > rect->center.getY();
bool rightOfCenter = circle->center.getX() > rect->center.getX();
//If the circle is above and to the right of the rect's center.
if(aboveCenter && rightOfCenter)
{
//Check to see how far the circle is in the rect in x and y direction.
double xDistance = rect->upperRight.getX() - circle->center.getX();
double yDistance = rect->upperRight.getY() - circle->center.getY();
if(xDistance > yDistance)
{
circleOnRectResponce(circle, rect, 0, yDistance + circle->getRadius());
}
else
{
circleOnRectResponce(circle, rect, xDistance + circle->getRadius(), 0);
}
}
//If the circle is above and to the left of the rect's center.
else if(aboveCenter && !rightOfCenter)
{
//Check to see how far the circle is in the rect in x and y direction.
double xDistance = circle->center.getX() - rect->upperLeft.getX();
double yDistance = rect->upperLeft.getY() - circle->center.getY();
if(xDistance > yDistance)
{
circleOnRectResponce(circle, rect, 0, yDistance + circle->getRadius());
}
else
{
circleOnRectResponce(circle, rect, -(xDistance + circle->getRadius()), 0);
}
}
//If the circle is below and to the right of the rect's center.
else if(!aboveCenter && rightOfCenter)
{
//Check to see how far the circle is in the rect in x and y direction.
double xDistance = rect->lowerRight.getX() - circle->center.getX();
double yDistance = circle->center.getY() - lowerRight.getY();
if(xDistance > yDistance)
{
circleOnRectResponce(circle, rect, 0, -(yDistance + circle->getRadius()));
}
else
{
circleOnRectResponce(circle, rect, xDistance + circle->getRadius(), 0);
}
}
//If the circle is below and to the left of the rect's center.
else
{
//Check to see how far the circle is in the rect in x and y direction.
double xDistance = circle->center.getX() - rect->lowerLeft.getX();
double yDistance = circle->center.getY() - lowerLeft.getY();
if(xDistance > yDistance)
{
circleOnRectResponce(circle, rect, 0, -(yDistance + circle->getRadius()));
}
else
{
circleOnRectResponce(circle, rect, -(xDistance + circle->getRadius()), 0);
}
}
}
}
void Detectable::rectOnRectDetection(Detectable* r1, Detectable* r2)
{
}
void Detectable::circleOnCircleDetection(Detectable* c1, Detectable* c2)
{
double distance = distBetween(c1->center, c2->center);
//If the distance between the two circles is within the two radiuss added together,
//then the two circles collided.
if(distance <= c1->getRadius() + c2->getRadius())
{
circleOnCircleResponce(c1, c2, c1->getRadius() + c2->getRadius() - distance, calcAngle(c1->center, c2->center));
}
}
void Detectable::handleDetection(Detectable* d2)
{
Detectable* d1 = this;
//If both objects are rectangles, then do the rectOnRectDetection
if(d1->dType == RECTANGLE && d2->dType == RECTANGLE)
{
rectOnRectDetection(d1, d2);
}
//If the first object is a circle and the other a rectangle, then do circleonRectDetection
else if(d1->dType == CIRCLE && d2->dType == RECTANGLE)
{
circleOnRectDetection(d1, d2);
}
//If the first is a rectangle and the other a circle, do circleOnRectDetection.
else if(d1->dType == RECTANGLE && d2->dType == CIRCLE)
{
circleOnRectDetection(d2, d1);
}
//If both objects are circles, do circle on circle detection.
else
{
circleOnCircleDetection(d1, d2);
}
}
void Detectable::moveBy(double dx, double dy)
{
upperLeft.move(dx, dy);
upperRight.move(dx, dy);
lowerLeft.move(dx, dy);
lowerRight.move(dx, dy);
center.move(dx, dy);
}
void Detectable::wasTouchedBy(Detectable* d, Point loc1, Point loc2, double xAmount, double yAmount)
{
}