forked from 15-466/15-466-f21-base6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionSystem.cpp
66 lines (49 loc) · 1.76 KB
/
CollisionSystem.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
#include "CollisionSystem.hpp"
void CollisionSystem::Collidable::FixOverLap() {
for (size_t i = 0; i < system->elements.size(); i++) {
Collidable* other = system->elements[i];
if (this == system->elements[i] || !other->parent->draw)
continue;
glm::vec3 diff_vec = parent->position - other->parent->position;
float distance = length(diff_vec) - radius - other->radius;
if (distance < 0) {
//reset ball position
glm::vec3 diff_pos = (-distance) * glm::normalize(diff_vec);
parent->position += diff_pos;
}
}
}
int CollisionSystem::CheckOverLap(int CollidableID, float attackDegree, float attackRadius) {
float minDistance = attackRadius;
int target = 0;
Collidable* current = elements[CollidableID - 1];
glm::mat4x3 frame = current->parent->make_local_to_parent();
glm::vec3 forward = glm::normalize(frame[1]);
//std::cout << forward.x << " " << forward.y << " " << forward.z << std::endl;
for (size_t i = 0; i < elements.size(); i++) {
//get the other collider
Collidable* other = elements[i];
//skip self and inactive colliders
if (current == other || !other->parent->draw)
continue;
//calculate distance
glm::vec3 diff_vec = other->parent->position - current->parent->position;
float distance = length(diff_vec);
//skip out of range colliders
if (distance > attackRadius)
continue;
//calculate angle
diff_vec = glm::normalize(diff_vec);
float angle = glm::degrees(glm::acos(glm::dot(diff_vec, forward)));
//skip out of degree colliders
if (angle > attackDegree * 0.5f)
continue;
if (distance < minDistance) {
minDistance = distance;
target = (int)i + 1;
}
//std::cout << diff_vec.x << " " << diff_vec.y << " " << diff_vec.z << std::endl;
//std::cout << "" << angle << std::endl;
}
return target;
}