-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionManager.cpp
More file actions
114 lines (78 loc) · 2.67 KB
/
CollisionManager.cpp
File metadata and controls
114 lines (78 loc) · 2.67 KB
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
#include "CollisionManager.h"
#include "Profiler.h"
void CollisionManager::checkCollisions(Scene* scene) {
PROFILE_START
//TODO: Spatial partitioning
Scene::ComponentList* list = scene->getComponents(Component::COLLIDER);
for (unsigned int i = 0; (int)i < (int)(list->size() - 1); ++i) {
for (unsigned int j = i+1; j < list->size(); ++j) {
ColliderComponent* a = static_cast<ColliderComponent*>(list->at(i));
ColliderComponent* b = static_cast<ColliderComponent*>(list->at(j));
if (!(a->getLayers() & b->getMask()) &&
!(b->getLayers() & a->getMask()))
return;
switch (a->getType()) {
case ColliderComponent::SPHERE:
switch (b->getType()) {
case ColliderComponent::SPHERE:
sphereVsSphere(static_cast<SphereColliderComponent*>(a), static_cast<SphereColliderComponent*>(b));
break;
}
break;
}
}
}
PROFILE_END
}
RaycastResult CollisionManager::raycast(Ray ray, Scene* scene) {
PROFILE_START
RaycastResult best = {
nullptr,
glm::vec3(0.0f),
glm::vec3(0.0f),
false
};
//TODO: Spatial partitioning
Scene::ComponentList* list = scene->getComponents(Component::COLLIDER);
float best_length = 0.0f;
for (unsigned int i = 0; i < list->size(); ++i) {
ColliderComponent* collider = static_cast<ColliderComponent*>(list->at(i));
if (!(collider->getLayers() & ray.mask))
continue;
RaycastResult current = collider->raycast(ray);
if (current.hit && (!best.hit || glm::length(current.position - ray.start) < best_length))
best = current;
}
PROFILE_END
return best;
}
void CollisionManager::sphereVsSphere(SphereColliderComponent* a, SphereColliderComponent* b) {
GameObject* ago = a->getGameObject();
GameObject* bgo = b->getGameObject();
//if (ago == nullptr || bgo == nullptr)
// return;
glm::vec3 diff(ago->getWorldPosition() - bgo->getWorldPosition());
float length = glm::length(diff);
if (length < a->getRadius() + b->getRadius()) { //Collision
glm::vec3 push = glm::normalize(diff) * (a->getRadius() + b->getRadius() - length);
glm::vec3 position = bgo->getWorldPosition() + glm::normalize(diff) * b->getRadius() - push;
Event e(Event::COLLISION);
e.collision.position.x = position.x;
e.collision.position.y = position.y;
e.collision.position.z = position.z;
if (a->getMask() & b->getLayers()) {
e.collision.other = bgo;
e.collision.push.x = push.x;
e.collision.push.y = push.y;
e.collision.push.z = push.z;
ago->dispatchEvent(e);
}
if (b->getMask() & a->getLayers()) {
e.collision.other = a->getGameObject();
e.collision.push.x = -push.x;
e.collision.push.y = -push.y;
e.collision.push.z = -push.z;
bgo->dispatchEvent(e);
}
}
}