-
Notifications
You must be signed in to change notification settings - Fork 0
/
convex.cpp
458 lines (381 loc) · 13.2 KB
/
convex.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
#include "convex.h"
#include <iostream>
//Default constructor
Convex::Convex(sf::Vector2f velocity) : sf::ConvexShape()
{
_mass = 5;
_velocity = velocity;
_angularVelocity = 2;
_momentOfInertia = _mass;
}
//Move each point by velocity * dt
void Convex::move(sf::Time* dt)
{
for (int i = 0; i < getPointCount(); i++)
{
setPoint(i, getPoint(i) + _velocity * dt->asSeconds());
}
}
/*
Rotates every point of the convex around its center of mass
*/
void Convex::rotateAroundMassCenter(sf::Time* dt)
{
sf::Vector2f temp;
for (int i = 0; i < getPointCount(); i++)
{
temp = getPoint(i);
temp -= _massCenter;
temp.x = (getPoint(i).x - _massCenter.x) * cos(_angularVelocity * dt->asSeconds()) - (getPoint(i).y - _massCenter.y) * sin(_angularVelocity * dt->asSeconds());
temp.y = (getPoint(i).x - _massCenter.x) * sin(_angularVelocity * dt->asSeconds()) + (getPoint(i).y - _massCenter.y) * cos(_angularVelocity * dt->asSeconds());
temp += _massCenter;
setPoint(i, temp);
}
}
/*
Calculates the center of mass' components
X1+X2+...Xn / amount of points
Y1+Y2+...Yn / amount of points
*/
void Convex::setMassCenter()
{
sf::Vector2f temp(0,0);
for (int i = 0; i < getPointCount(); i++)
{
temp.x += getPoint(i).x;
temp.y += getPoint(i).y;
}
temp.x = temp.x / getPointCount();
temp.y = temp.y / getPointCount();
_massCenter = temp;
}
void Convex::applyChanges(int index, sf::Vector2f* collisionNormal)
{
float radius = getRadius(index);
sf::Vector2f collisionPoint(getPoint(index).x - _massCenter.x,
getPoint(index).y - _massCenter.y);
_momentOfInertia = _mass * pow(radius, 2);
float impulse = getImpulse(&collisionPoint, collisionNormal);
_velocity.x += impulse / _mass * collisionNormal->x;
_velocity.y += impulse / _mass * collisionNormal->y;
_angularVelocity = _angularVelocity + impulse / _momentOfInertia * (collisionPoint.x * collisionNormal->y - collisionPoint.y * collisionNormal->x);
}
/*
Applies the changes caused by the collision to both collided convexes
*/
void Convex::applyChanges(sf::Vector2f* collisionNormal, float impulse, sf::Vector2f* collisionPoint, Convex* shapeA, Convex* shapeB)
{
shapeA->setMassCenter();
shapeB->setMassCenter();
sf::Vector2f radiusA(collisionPoint->x - shapeA->_massCenter.x,
collisionPoint->y - shapeA->_massCenter.y);
sf::Vector2f radiusB(collisionPoint->x - shapeB->_massCenter.x,
collisionPoint->y - shapeB->_massCenter.y);
float radiusAlength = sqrt(pow(radiusA.x, 2) + pow(radiusA.y, 2));
float radiusBlength = sqrt(pow(radiusB.x, 2) + pow(radiusB.y, 2));
//float Ja = _mass * pow(radiusAlength, 2);
//float Jb = shapeB->_mass * pow(radiusBlength, 2);
float Ja = 44;
float Jb = 45;
shapeA->_velocity.x += impulse / shapeA->_mass * collisionNormal->x;
shapeA->_velocity.y += impulse / shapeA->_mass * collisionNormal->y;
shapeB->_velocity.x -= impulse / shapeB->_mass * collisionNormal->x;
shapeB->_velocity.y -= impulse / shapeB->_mass * collisionNormal->y;
shapeA->_angularVelocity += impulse / Ja * vectorProduct(radiusA.x,
radiusA.y,
collisionNormal->x,
collisionNormal->y);
shapeB->_angularVelocity -= impulse / Ja * vectorProduct(radiusB.x,
radiusB.y,
collisionNormal->x,
collisionNormal->y);
}
/*
Checks the position of every point of the vertex, if
any of them are outside of the window and still heading
there changes their direction
momentOfInertia = mass * r^2
*/
void Convex::borderCollision(sf::RenderWindow* window)
{
float impulse;
float Px, Py;
for (int i = 0; i < getPointCount(); i++)
{
if (getPoint(i).x < 0 && _velocity.x < 0)
{
sf::Vector2f collisionNormal(1, 0);
applyChanges(i, &collisionNormal);
}
if (getPoint(i).x > window->getSize().x && _velocity.x > 0)
{
sf::Vector2f collisionNormal(-1, 0);
applyChanges(i, &collisionNormal);
}
if (getPoint(i).y < 0 && _velocity.y < 0)
{
sf::Vector2f collisionNormal(0, 1);
applyChanges(i, &collisionNormal);
}
if (getPoint(i).y > window->getSize().y && _velocity.y > 0)
{
sf::Vector2f collisionNormal(0, -1);
applyChanges(i, &collisionNormal);
}
_momentOfInertia = _mass;
}
}
/*
Calculates the distance between point[i]
and the convex's center of mass
*/
float Convex::getRadius(int pointIndex)
{
float radius = sqrt(pow(getPoint(pointIndex).x - getPosition().x, 2) +
pow(getPoint(pointIndex).y - getPosition().y, 2));
return radius;
}
/*
Calculates the impulse between the convex and a border represented by the collisionNormal
*/
float Convex::getImpulse(sf::Vector2f* collisionPoint, sf::Vector2f* collisionNormal)
{
float impulse = -(1 + 0.5) * ((_velocity.x - _angularVelocity * collisionPoint->y) * collisionNormal->x +
(_velocity.y + _angularVelocity * collisionPoint->x) * collisionNormal->y) /
((1 / _mass) + (pow(collisionPoint->x * collisionNormal->y - collisionPoint->y * collisionNormal->x, 2)) / _momentOfInertia);
return impulse;
}
/*
Calculates the impulse between two convexes
*/
float Convex::getImpulse(sf::Vector2f* collisionPoint, sf::Vector2f* collisionNormal, Convex* shapeA, Convex* shapeB)
{
shapeA->setMassCenter();
shapeB->setMassCenter();
sf::Vector2f radiusAP(collisionPoint->x - shapeA->_massCenter.x,
collisionPoint->y - shapeA->_massCenter.y);
sf::Vector2f radiusBP(collisionPoint->x - shapeB->_massCenter.x,
collisionPoint->y - shapeB->_massCenter.y);
float radiusAlength = sqrt(pow(radiusAP.x, 2) + pow(radiusAP.y, 2));
float radiusBlength = sqrt(pow(radiusBP.x, 2) + pow(radiusBP.y, 2));
float Ja = 44;
float Jb = 45;
sf::Vector2f angularVelocityByRadiusAP(-(shapeA->_angularVelocity * radiusAP.y), -(-(shapeA->_angularVelocity * radiusAP.x)));
sf::Vector2f angularVelocityByRadiusBP(-(shapeB->_angularVelocity * radiusBP.y), -(-(shapeB->_angularVelocity * radiusBP.x)));
sf::Vector2f Vap(shapeA->_velocity.x + angularVelocityByRadiusAP.x, shapeA->_velocity.y + angularVelocityByRadiusAP.y);
sf::Vector2f Vab(shapeB->_velocity.x + angularVelocityByRadiusBP.x, shapeB->_velocity.y + angularVelocityByRadiusBP.y);
sf::Vector2f relativeSpeed = Vap - Vab;
float impulse = -(1 + 0.5) * ((shapeA->_velocity.x - shapeA->_angularVelocity * radiusAP.y - shapeB->_velocity.x + shapeB->_angularVelocity * radiusBP.y) * collisionNormal->x +
(shapeA->_velocity.y + shapeA->_angularVelocity * radiusAP.x - shapeB->_velocity.y - shapeB->_angularVelocity * radiusBP.x) * collisionNormal->y)
/
((1 / shapeA->_mass) + (1 / shapeB->_mass) + (pow(radiusAP.x * collisionNormal->y - radiusAP.y * collisionNormal->x, 2) / Ja)
+ (pow(radiusBP.x * collisionNormal->y - radiusBP.y * collisionNormal->x, 2) / Jb));
if (impulse < 0)
{
collisionNormal->x *= -1;
collisionNormal->y *= -1;
float impulse = -(1 + 0.5) * ((shapeA->_velocity.x - shapeA->_angularVelocity * radiusAP.y - shapeB->_velocity.x + shapeB->_angularVelocity * radiusBP.y) * collisionNormal->x +
(shapeA->_velocity.y + shapeA->_angularVelocity * radiusAP.x - shapeB->_velocity.y - shapeB->_angularVelocity * radiusBP.x) * collisionNormal->y)
/
((1 / shapeA->_mass) + (1 / shapeB->_mass) + (pow(radiusAP.x * collisionNormal->y - radiusAP.y * collisionNormal->x, 2) / Ja)
+ (pow(radiusBP.x * collisionNormal->y - radiusBP.y * collisionNormal->x, 2) / Jb));
}
if (impulse < 0)
{
std::cout << collisionNormal->x << ", " << collisionNormal->y << std::endl;
}
return impulse;
}
void Convex::drawCollisionPoint(sf::Vector2f* point, sf::RenderWindow* window)
{
sf::CircleShape circle(2);
circle.setPosition(point->x, point->y);
window->draw(circle);
}
float Convex::vectorProduct(float x1, float y1, float x2, float y2)
{
float vectorProduct = x1 * y2 - y1 * x2;
return vectorProduct;
}
/*
Checks if the 'value' is between startPoint and endPoint
*/
bool Convex::isBetween(float value, float startPoint, float endPoint)
{
if (value < startPoint && value > endPoint)
{
return true;
}
if (value > startPoint && value < endPoint)
{
return true;
}
return false;
}
bool Convex::hasCollided(Convex* other)
{
sf::Vector2f collisionPoint, betweenCorners;
for (int i = 0; i < other->getPointCount(); i++)
{
for (int j = 0; j < getPointCount(); j++)
{
if (j < getPointCount() - 1)
{
sf::Vector2f collisionPoint(other->getPoint(i).x - getPoint(j).x,
other->getPoint(i).y - getPoint(j).y);
sf::Vector2f betweenCorners(getPoint(j + 1).x - getPoint(j).x,
getPoint(j + 1).y - getPoint(j).y);
}
else
{
sf::Vector2f collisionPoint(other->getPoint(i).x - getPoint(j).x,
other->getPoint(i).y - getPoint(j).y);
sf::Vector2f betweenCorners(getPoint(0).x - getPoint(j).x,
getPoint(0).y - getPoint(j).y);
}
if (vectorProduct(betweenCorners.x, betweenCorners.y, collisionPoint.x, collisionPoint.y) > 0)
{
return false;
}
}
}
for (int i = 0; i < this->getPointCount(); i++)
{
for (int j = 0; j < other->getPointCount(); j++)
{
if (j < other->getPointCount() - 1)
{
sf::Vector2f collisionPoint(this->getPoint(i).x - other->getPoint(j).x,
this->getPoint(i).y - other->getPoint(j).y);
sf::Vector2f betweenCorners(other->getPoint(j + 1).x - other->getPoint(j).x,
other->getPoint(j + 1).y - other->getPoint(j).y);
}
else
{
sf::Vector2f collisionPoint(this->getPoint(i).x - other->getPoint(j).x,
this->getPoint(i).y - other->getPoint(j).y);
sf::Vector2f betweenCorners(other->getPoint(0).x -other->getPoint(j).x,
other->getPoint(0).y - other->getPoint(j).y);
}
if (vectorProduct(betweenCorners.x, betweenCorners.y, collisionPoint.x, collisionPoint.y) > 0)
{
return false;
}
}
}
return true;
}
/*
Calculates the coordinates of the collision point
*/
void Convex::convexCollision(Convex* other, sf::RenderWindow* window)
{
sf::Vector2f collisionNormal;
float collisionX, collisionY;
float x1, x2, x3, x4, y1, y2, y3, y4;
if (hasCollided(other))
{
for (int i = 0; i < getPointCount(); i++)
{
//Retrieves the coordinates of the point i
x1 = this->getPoint(i).x;
y1 = this->getPoint(i).y;
//If the index 'i' equals the amount of points
//the other end of the line should be the starting point
if (i == getPointCount() - 1)
{
x2 = this->getPoint(0).x;
y2 = this->getPoint(0).y;
}
else
{
x2 = this->getPoint(i + 1).x;
y2 = this->getPoint(i + 1).y;
}
for (int j = 0; j < other->getPointCount(); j++)
{
x3 = other->getPoint(j).x;
y3 = other->getPoint(j).y;
//If the index 'j' equals the amount of points
//the other end of the line should be the starting point
if (i == getPointCount() - 1)
{
x4 = other->getPoint(0).x;
y4 = other->getPoint(0).y;
}
else
{
x4 = other->getPoint(i + 1).x;
y4 = other->getPoint(i + 1).y;
}
collisionX = vectorProduct(vectorProduct(x1, y1, x2, y2),
vectorProduct(x1, 1, x2, 1),
vectorProduct(x3, y3, x4, y4),
vectorProduct(x3, 1, x4, 1))
/
vectorProduct(vectorProduct(x1, 1, x2, 1),
vectorProduct(y1, 1, y2, 1),
vectorProduct(x3, 1, x4, 1),
vectorProduct(y3, 1, y4, 1));
collisionY = vectorProduct(vectorProduct(x1, y1, x2, y2),
vectorProduct(y1, 1, y2, 1),
vectorProduct(x3, y3, x4, y4),
vectorProduct(y3, 1, y4, 1))
/
vectorProduct(vectorProduct(x1, 1, x2, 1),
vectorProduct(y1, 1, y2, 1),
vectorProduct(x3, 1, x4, 1),
vectorProduct(y3, 1, y4, 1));
//Checking that the collision point is actually between the corners
//of the convexes
if (isBetween(collisionX, x1, x2) && isBetween(collisionX, x3, x4) &&
isBetween(collisionY, y1, y2) && isBetween(collisionY, y3, y4))
{
//If the collision point is between these points use them to calculate the
//collisionNormal's components
if (isBetween(collisionX, x1, x2) && isBetween(collisionY, y1, y2))
{
float x = x2 - x1;
float y = y2 - y1;
float length = sqrt(pow(x, 2) + pow(y, 2));
float unitX = x / length;
float unitY = y / length;
collisionNormal.x = -unitY;
collisionNormal.y = unitX;
float impulse = getImpulse(&sf::Vector2f(collisionX, collisionY), &collisionNormal, other, this);
applyChanges(&collisionNormal, impulse, &sf::Vector2f(collisionX, collisionY), other, this);
}
else
{
float x = x4 - x3;
float y = y4 - y3;
float length = sqrt(pow(x, 2) + pow(y, 2));
float unitX = x / length;
float unitY = y / length;
collisionNormal.x = -unitY;
collisionNormal.y = unitX;
float impulse = getImpulse(&sf::Vector2f(collisionX, collisionY), &collisionNormal, this, other);
applyChanges(&collisionNormal, impulse, &sf::Vector2f(collisionX, collisionY), this, other);
}
sf::CircleShape circle(5);
circle.setFillColor(sf::Color::Red);
circle.setPosition(collisionX, collisionY);
window->draw(circle);
}
}
}
}
}
/*
Iterates the position of each point by velocity, and angular velocity.
In addition checks whether or not the convex has collided with the borders
of the window, also handles the drawing of the object.
*/
void Convex::iterate(sf::RenderWindow* window, sf::Time* dt)
{
window->draw(*this);
rotateAroundMassCenter(dt);
borderCollision(window);
setMassCenter();
move(dt);
}