-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.cpp
More file actions
271 lines (219 loc) · 7.71 KB
/
assignment3.cpp
File metadata and controls
271 lines (219 loc) · 7.71 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// assignment3.cpp
// MSIM495
//
// Created by Aaron Ruel on 2/6/18.
// Copyright (c) 2018 AAR. All rights reserved.
//
#include "assignment3.h"
#include "playground.h"
#include "physics.h"
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#include <math.h>
namespace A3q1 {
Physics::ParticleForceRegistrar particle_force_registrar;
Physics::Particle target;
Physics::Particle attraction_point;
Physics::ParticleGravity gravity(Physics::Vector3{0, -1, 0});
Physics::real frame_time = 1.0/30.0;
void draw_reference_points() {
for (int i = -3; i < 3; ++i) {
for (int j = -3; j < 3; ++j) {
Graphics::draw_sphere(Physics::Particle(
Physics::Vector3{
static_cast<Physics::real>(i*10.0),
0,
static_cast<Physics::real>(j*10.0)
}
), 0.1);
}
}
}
void step_physics() {
printf("%f\n", Graphics::get_seconds_per_frame());
Physics::Vector3 new_g = attraction_point.get_position() - target.get_position();
Physics::real distance = target.get_position().distance(attraction_point.get_position());
gravity.set_gravity(new_g * ( 1 / powf(distance, 2) ));
particle_force_registrar.update_forces(frame_time);
target.integrate(frame_time);
}
void draw_target() {
Graphics::draw_pyramid(target);
Graphics::draw_sphere(attraction_point, 0.1);
}
int main(int argc, char ** argv) {
Graphics::Graphics(800, 600, argc, argv);
// Physics
target.set_position(Physics::Vector3(0, 1, 0));
target.set_mass(1);
target.set_velocity(Physics::Vector3(-1, 0, 0));
attraction_point.set_position(Physics::Vector3(2, 2, -4));
particle_force_registrar.add(
&target,
(Physics::ParticleForceGenerator*)&gravity
);
// Graphics
Graphics::push_draw_pipeline(Graphics::draw_ground);
Graphics::push_draw_pipeline(draw_reference_points);
Graphics::push_draw_pipeline(draw_target);
Graphics::push_draw_pipeline(step_physics);
Graphics::start();
return 0;
};
}
namespace A3q2 {
Physics::ParticleForceRegistrar particle_force_registrar;
Physics::Particle target;
Physics::Particle suspend_point;
Physics::ParticleStiffSpring buoyancy(
&suspend_point,
2000,
2
);
Physics::ParticleGravity gravity(Physics::Vector3{0, -2.5, 0});
Physics::real frame_time = 1.0/30.0;
void draw_reference_points() {
for (int i = -3; i < 3; ++i) {
for (int j = -3; j < 3; ++j) {
Graphics::draw_sphere(Physics::Particle(
Physics::Vector3{
static_cast<Physics::real>(i*10.0),
0,
static_cast<Physics::real>(j*10.0)
}
), 0.1);
}
}
}
void step_physics() {
particle_force_registrar.update_forces(frame_time);
target.integrate(frame_time);
}
void draw_target() {
Graphics::draw_sphere(target, 0.5);
Graphics::draw_sphere(suspend_point, 0.1);
}
int main(int argc, char ** argv) {
Graphics::Graphics(800, 600, argc, argv);
// Physics
target.set_position(Physics::Vector3(0, 0, 0));
target.set_mass(1);
target.set_damping(0.6);
suspend_point.set_position(Physics::Vector3(0, 5, 0));
particle_force_registrar.add(
&target,
(Physics::ParticleForceGenerator*)&buoyancy
);
particle_force_registrar.add(
&target,
(Physics::ParticleForceGenerator*)&gravity
);
// Graphics
Graphics::push_draw_pipeline(Graphics::draw_ground);
Graphics::push_draw_pipeline(draw_reference_points);
Graphics::push_draw_pipeline(draw_target);
Graphics::push_draw_pipeline(step_physics);
Graphics::start();
return 0;
};
}
namespace A3q3 {
class Claustrophobe : public Physics::Particle {
Physics::real personal_space = 2;
public:
Physics::ParticleGravity gravity;
Physics::ParticleSpring spring;
Claustrophobe(Physics::Vector3 position)
: gravity(Physics::Vector3{0,0,0}), spring(nullptr, 0, 0) {
set_mass(10);
set_position(position);
}
Physics::ParticleSpring * get_spring() {
return &spring;
}
void register_forces(
std::vector<Claustrophobe> * c_list,
Physics::ParticleForceRegistrar * pfr
) {
// this pointer doesn't assign correctly in constructor
spring = Physics::ParticleSpring(this, 10, 10);
// register force towards middle
Physics::Vector3 vec = get_position();
vec.invert();
vec.normalize();
gravity.set_gravity(vec);
pfr->add(this, &this->gravity);
// register personal space spring forces
auto i = c_list->begin();
for (; i != c_list->end(); ++i) {
if (&(*i) != this) {
Claustrophobe * other = &(*i);
Physics::real distance = get_position().distance(i->get_position());
if (
distance < personal_space
&& !pfr->check_force_registered(this, other->get_spring())
) {
pfr->add(other, &this->spring);
}
}
}
};
void update(Physics::real duration) {
integrate(duration);
}
};
Physics::ParticleForceRegistrar particle_force_registrar;
Physics::Particle target;
Physics::Particle attraction_point;
std::vector<Claustrophobe> c_list;
Physics::real frame_time = 1.0/60.0;
void load_claustrophobes() {
for (int i = -3; i < 3; ++i) {
for (int j = -3; j < 3; ++j) {
c_list.push_back(Claustrophobe(
Physics::Vector3{
static_cast<Physics::real>(i*10.0),
0,
static_cast<Physics::real>(j*10.0)
}
));
}
}
}
void draw_claustrophobes() {
auto i = c_list.begin();
for (; i != c_list.end(); ++i) {
Graphics::draw_sphere(*i, 0.1);
}
}
void process_claustrophobes() {
auto i = c_list.begin();
for (; i != c_list.end(); ++i) {
i->register_forces(&c_list, &particle_force_registrar);
}
}
void step_physics() {
particle_force_registrar.update_forces(frame_time);
auto i = c_list.begin();
for (; i != c_list.end(); ++i) {
i->integrate(frame_time);
}
particle_force_registrar.clear();
}
void draw_target() {
}
int main(int argc, char ** argv) {
Graphics::Graphics(800, 600, argc, argv);
load_claustrophobes();
// Physics
// Graphics
Graphics::push_draw_pipeline(Graphics::draw_ground);
Graphics::push_draw_pipeline(draw_claustrophobes);
Graphics::push_draw_pipeline(draw_target);
Graphics::push_draw_pipeline(process_claustrophobes);
Graphics::push_draw_pipeline(step_physics);
Graphics::start();
return 0;
};
}