-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScene.cpp
232 lines (198 loc) · 5.92 KB
/
Scene.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
#include "Scene.h"
namespace rme
{
Scene::Scene()
{
std::vector<Object3D*> children;
warpA = NULL;// new rme::Sphere("warpA");
warpB = NULL;// rme::Sphere("warpB");
//control();
}
void Scene::add(Object3D *obj)
{
children.push_back(obj);
}
void Scene::remove(std::string name)
{
int index = -1;
int count = 0;
for (std::vector<Object3D*>::iterator it = children.begin(); it != children.end(); ++it)
{
if (children[count]->name == name)
{
index = count;
break;
}
count++;
}
if (index < 0)
{
std::cout << "Object to remove does not exist\n";
}
else
{
children.erase(children.begin() + count);
}
}
void Scene::spawn(Camera* camera, bool left, Controls* control)
{
Sphere *sphere;
if (left)
{
sphere = new rme::Sphere("warpA");
delete this->warpA;
}
else
{
sphere = new rme::Sphere("warpB");
delete this->warpB;
}
glm::vec2 yRot = rot2D(glm::vec2(0.0, 1.0), control->yRotation);
glm::vec2 xRot = rot2D(glm::vec2(0.0, yRot.y), control->xRotation);
glm::vec3 dir = glm::normalize(glm::vec3(xRot.x, yRot.x, xRot.y));
sphere->color = glm::vec3(0.0, 1.0, 0.0);
//sphere->material->color = glm::vec3(0.0, 1.0, 0.0);
sphere->radius = 2.75;
sphere->position = camera->position + 1.1f*sphere->radius*dir;
sphere->charge = 0.0;
sphere->velocity = dir*0.07f;
sphere->physics = true;
if (left)
{
this->warpA = sphere;
}
else
{
this->warpB = sphere;
}
}
void Scene::update(Controls* control)
{
// Collision detection and interaction
for (int i = 0; i < children.size(); i++)
{
Object3D *current = children[i];
current->age += 1.0;
if (current->physics) {
current->velocity += glm::vec3(0.0, -0.0005, 0.0);
}
// Camera collides but does not feel force
if (current->geometry == CAMERA)
{
// Move player with WASD
glm::vec2 direction = rot2D(glm::vec2(0.0, 1.0), control->xRotation);
glm::vec2 directionPerp = rot2D(glm::vec2(1.0, 0.0), control->xRotation);
glm::vec3 candidate = current->position + current->velocity;
float testDist = map(candidate, i);
glm::vec3 camNorm = normal(candidate, i);
if (testDist < current->radius)
{
if (control->w) current->velocity += 0.002f*glm::vec3(direction.x, 0.0, direction.y);
if (control->a) current->velocity += -0.002f*glm::vec3(directionPerp.x, 0.0, directionPerp.y);
if (control->s) current->velocity += -0.002f*glm::vec3(direction.x, 0.0, direction.y);
if (control->d) current->velocity += 0.002f*glm::vec3(directionPerp.x, 0.0, directionPerp.y);
// if (glm::dot(camNorm, glm::vec3(0.0, 1.0, 0.0)) > 0.0)
// {
current->velocity = 0.97f*(current->velocity - glm::dot(current->velocity, camNorm)*camNorm);
// }
if (control->space && glm::dot(camNorm, glm::vec3(0.0, 1.0, 0.0)) > 0.7)
{
current->velocity += glm::vec3(0.0, 0.06, 0.0);
current->position += current->velocity;
}
}
if (control->lmb)
{
spawn((Camera*)current, true, control);
control->lmb = false;
}
if (control->rmb)
{
spawn((Camera*)current, false, control);
control->rmb = false;
}
}
if (current->geometry != SPHERE) continue;
// Electromagnetic/Gravity like force
for (int j = 0; j < children.size(); j++)
{
Object3D *other = children[j];
if (other->geometry != SPHERE || i == j) continue;
glm::vec3 diff = current->position - other->position;
float radius = glm::length(diff);
float force = current->charge*other->charge / (radius*radius);
current->velocity += diff * force;
}
// Collision detection
float distance = map(current->position, i);
float delta = distance - current->radius;
// Collision
if (delta < 0.0f && current->collisions)
{
glm::vec3 norm = glm::vec3(normal(current->position, i));
current->velocity = 1.0f*glm::reflect(current->velocity, norm);
// current->correction += -1.0f*delta*glm::normalize(current->velocity);
}
}
// Update positions
for (int i = 0; i < children.size(); i++)
{
Object3D *current = children[i];
current->velocity *= 0.999;
current->position += (current->velocity + current->correction);
current->correction = glm::vec3(0.0);
}
}
float Scene::map(glm::vec3 p, int exclude)
{
float dist = 1000000.0;
for (int i = 0; i < children.size(); i++) {
if (i == exclude) continue;
switch (children[i]->geometry) {
case 4:
dist = glm::min(dist, sdSphere(p - children[i]->position, children[i]->radius));
break;
case 7:
dist = glm::min(dist, sdBoxInterior(p - children[i]->position, children[i]->shape));
break;
default:
dist = dist;
}
}
return dist;
}
glm::vec3 Scene::normal(glm::vec3 p, int exclude)
{
glm::vec3 eps = glm::vec3(0.002, 0.0, 0.0);
return glm::normalize(glm::vec3(
map(p + glm::vec3(eps.x, eps.y, eps.y), exclude) - map(p - glm::vec3(eps.x, eps.y, eps.y), exclude),
map(p + glm::vec3(eps.y, eps.x, eps.y), exclude) - map(p - glm::vec3(eps.y, eps.x, eps.y), exclude),
map(p + glm::vec3(eps.y, eps.y, eps.x), exclude) - map(p - glm::vec3(eps.y, eps.y, eps.x), exclude)));
}
glm::vec2 Scene::rot2D(glm::vec2 p, float angle)
{
float s = glm::sin(angle);
float c = glm::cos(angle);
return p * glm::mat2(c, s, -s, c);
}
///// Signed Distance Geometries ////
float Scene::sdSphere(glm::vec3 p, float s)
{
return glm::length(p) - s;
}
float Scene::sdTorus(glm::vec3 p, glm::vec2 t)
{
glm::vec2 q = glm::vec2(glm::length(glm::vec2(p.x, p.y)) - t.x, p.z);
return glm::length(q) - t.y;
}
float Scene::sdRoundBox(glm::vec3 p, glm::vec3 b, float r)
{
glm::vec3 d = glm::abs(p) - b;
return glm::min(glm::max(d.x, glm::max(d.y, d.z)), 0.0f) + glm::length(glm::max(d, glm::vec3(0.0))) - r;
}
float Scene::sdBoxInterior(glm::vec3 p, glm::vec3 b)
{
glm::vec3 d = glm::abs(p) - b;
return -(glm::min(glm::max(d.x, glm::max(d.y, d.z)), 0.0f) + glm::length(glm::max(d, glm::vec3(0.0))));
}
}