-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicollidable.cpp
139 lines (112 loc) · 4.9 KB
/
icollidable.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
#include <icollidable.h>
#include "glm/ext.hpp"
#include "glm/vec3.hpp"
bool ICollidable::sphereWithBounds(const ICollidable * sphere,const ICollidable * box) const {
BoundingSphere sphereBounds = sphere->getBoundingSphere();
BoundingRect boxBounds = box->getBoundingRect();
float * Bmin = glm::value_ptr(boxBounds.bottom);
float * Bmax = glm::value_ptr(boxBounds.top);
float * C = glm::value_ptr(sphereBounds.center);
float r = (float) sphereBounds.radius;
float dmin;
float r2 = SQR( r );
int i, face;
dmin = 0;
for( i = 0; i < 3; i++ ) {
if( C[i] < Bmin[i] ) dmin += SQR(C[i] - Bmin[i] ); else
if( C[i] > Bmax[i] ) dmin += SQR( C[i] - Bmax[i] );
}
if( dmin <= r2 ) return true;
return false;
}
bool ICollidable::sphereCollision(const ICollidable * firstSphere,const ICollidable * secondSphere) const {
BoundingSphere sphereBounds = firstSphere->getBoundingSphere();
BoundingSphere secondSphereBounds = secondSphere->getBoundingSphere();
return glm::distance(sphereBounds.center,secondSphereBounds.center) <= (sphereBounds.radius + secondSphereBounds.radius);
}
bool ICollidable::boundsWithBounds(const ICollidable *box, const ICollidable *boxTwo) const {
BoundingRect boxBounds = box->getBoundingRect();
BoundingRect secondBounds = boxTwo->getBoundingRect();
return(boxBounds.top.x > secondBounds.bottom.x &&
boxBounds.bottom.x < secondBounds.top.x &&
boxBounds.top.y > secondBounds.bottom.y &&
boxBounds.bottom.y < secondBounds.top.y &&
boxBounds.top.z > secondBounds.bottom.z &&
boxBounds.bottom.z < secondBounds.top.z);
}
bool ICollidable::collides(const ICollidable *collidable) const {
t_collision_model currentCollisionModel = collisionModel();
t_collision_model otherCollisionModel = collidable->collisionModel();
if (!collisionEnabled() || !collidable->collisionEnabled()) {
return false;
}
switch(currentCollisionModel) {
case COLLISION_MODEL_BOUNDS: {
switch (otherCollisionModel) {
case COLLISION_MODEL_BOUNDS:
return boundsWithBounds(this,collidable);
case COLLISION_MODEL_SPHERE:
return sphereWithBounds(collidable,this);
case COLLISION_MODEL_PLANE:
return planeWithBounds(collidable,this);
}
break;
}
case COLLISION_MODEL_PLANE: {
switch (otherCollisionModel) {
case COLLISION_MODEL_BOUNDS:
return planeWithBounds(this,collidable);
case COLLISION_MODEL_SPHERE:
return planeWithSphere(this,collidable);
case COLLISION_MODEL_PLANE:
return false;
}
break;
}
case COLLISION_MODEL_SPHERE: {
switch (otherCollisionModel) {
case COLLISION_MODEL_BOUNDS:
return sphereWithBounds(this,collidable);
case COLLISION_MODEL_SPHERE:
return sphereCollision(this,collidable);
case COLLISION_MODEL_PLANE:
return planeWithSphere(collidable,this);
}
break;
}
default: {
return false;
}
}
return false;
}
bool ICollidable::collisionEnabled() const {
return false;
}
bool ICollidable::planeWithBounds(const ICollidable *plane, const ICollidable *box) const {
BoundingPlane boundingPlane = plane->getBoundingPlane();
BoundingRect boundingRect = box->getBoundingRect();
glm::vec3 middle = (boundingRect.bottom + boundingRect.top) / 2.0f;
// currently this is only axis aligned
glm::vec3 verteces[] = {
glm::vec3(boundingRect.top.x-middle.x, 0, 0 ),
glm::vec3(0, boundingRect.top.y-middle.y, 0),
glm::vec3(0, 0, boundingRect.top.z-middle.z)
};
double radius = glm::length(verteces[0]) * abs( glm::dot( glm::normalize(boundingPlane.mNormal), glm::normalize(verteces[0]) ) ) +
glm::length(verteces[1]) * abs( glm::dot( glm::normalize(boundingPlane.mNormal), glm::normalize(verteces[1]) ) ) +
glm::length(verteces[2]) * abs( glm::dot( glm::normalize(boundingPlane.mNormal), glm::normalize(verteces[2]) ) );
double distance = glm::dot( boundingPlane.mNormal, middle ) - boundingPlane.d;
return abs(distance) <= radius;
}
bool ICollidable::planeWithSphere(const ICollidable * plane,const ICollidable *sphere) const {
BoundingPlane boundingPlane = plane->getBoundingPlane();
BoundingSphere boundingSphere = sphere->getBoundingSphere();
float separation = glm::dot(boundingSphere.center, boundingPlane.mNormal)
- boundingPlane.d;
return separation <= boundingSphere.radius;
}
bool ICollidable::collidedWith(ICollidable * collidable) {
// NO-OP
return true;
}