forked from projeto-de-algoritmos/DC_2DCollisionDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionDetection.cpp
54 lines (43 loc) · 1.71 KB
/
CollisionDetection.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
#include "CollisionDetection.hpp"
#include "RunningManager.hpp"
#include "QuadTree.hpp"
void ColDetect::EfficientCollisionDetection(const std::vector<Ball *> & balls, int32_t table_width, int32_t table_height)
{
QuadTree tree = QuadTree(1, Rectangle(0, 0, table_width, table_height));
for(auto ball : balls){
RunningManager::IncrementOperationsPerfomed();
tree.insert(ball);
}
std::vector<Ball*> quadrantBalls;
for(auto ball : balls){
quadrantBalls = tree.retrieve(ball);
for(auto qball : quadrantBalls){
RunningManager::IncrementOperationsPerfomed();
if(ball == qball)
continue;
if(Ball::ballsAreColliding(ball, qball)){
ball->turnOnBallHighlight();
qball->turnOnBallHighlight();
if(RunningManager::PhysicsIsEnabled())
Ball::collideBalls(ball, qball);
}
}
}
}
void ColDetect::NaiveCollisionDetection(const std::vector<Ball *> & balls) {
// Algoritmo ingenuo O(N^2)
for (int i = 0; i < (int) balls.size(); i++) {
for (int j = i+1; j < (int) balls.size(); j++) {
// Sinaliza mais uma query sobre uma bola
RunningManager::IncrementOperationsPerfomed();
if (Ball::ballsAreColliding(balls[i], balls[j])) {
// Ativa o brilho de ambas as bolas
balls[i]->turnOnBallHighlight();
balls[j]->turnOnBallHighlight();
// Se a fisica estiver habilitada, colidir as duas bolas
if (RunningManager::PhysicsIsEnabled())
Ball::collideBalls(balls[i], balls[j]);
}
}
}
}