-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrebuchet.cpp
More file actions
210 lines (176 loc) · 6.74 KB
/
trebuchet.cpp
File metadata and controls
210 lines (176 loc) · 6.74 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
//
// trebuchet.cpp
// MSIM495
//
#include "trebuchet.h"
#include "playground.h"
#include "physics.h"
#include <stdlib.h>
#define CONTACT_OBJECTS 10
#define $(contents) [](){contents}
namespace Trebuchet {
Physics::ParticleWorld world(
CONTACT_OBJECTS
);
std::vector<Physics::Particle*> particles;
bool physics_enabled = false;
bool projectile_released = false;
Physics::ParticleRod rod;
Physics::ParticleRod rod2;
Physics::ParticleCable arm;
Physics::ParticleRod strength;
Physics::real height_offset = 2.f;
Physics::real counterweight = 1000;
Physics::real score = 0.f;
Physics::ParticleGravity gravity(Physics::Vector3(0,-10,0));
Physics::ParticleGravity inverse(Physics::Vector3(0, 10,0));
Physics::Particle * anchor = new Physics::Particle();
Physics::Particle * pendulum = new Physics::Particle();
Physics::Particle * hook = new Physics::Particle();
Physics::Particle * projectile = new Physics::Particle();
void initialize() {
world.pass_particles(&particles);
// Set positions
anchor->set_position(Physics::Vector3(0, 2, 0));
pendulum->set_position(Physics::Vector3(2, 2, 0));
hook->set_position(Physics::Vector3(-2, 2, 0));
projectile->set_position(Physics::Vector3(-2, 1, 0));
// Set mass
anchor->set_mass(0);
pendulum->set_mass(counterweight);
hook->set_mass(1);
projectile->set_mass(1);
// Make available to renderer
particles.push_back(anchor);
particles.push_back(pendulum);
particles.push_back(hook);
particles.push_back(projectile);
// Force aggregators
world.registry.add(pendulum, &gravity);
world.registry.add(hook, &gravity);
world.registry.add(projectile, &gravity);
// Particle collision constructs
// - Will aggregate mass with two end rods
// connecting to the anchor and one rod
// connecting each end of the bar
rod.left = anchor;
rod.right = pendulum;
rod.max_length = 2;
rod2.left = anchor;
rod2.right = hook;
rod2.max_length = 2;
strength.left = hook;
strength.right = pendulum;
strength.max_length = 4;
arm.left = hook;
arm.right = projectile;
arm.max_length = 1;
arm.restitution = 0.5;
// Push collision constructs
world.contact_generators.push_back(&rod);
world.contact_generators.push_back(&rod2);
world.contact_generators.push_back(&strength);
world.contact_generators.push_back(&arm);
}
void release_projectile() {
auto i = world.contact_generators.begin();
for (; i != world.contact_generators.end(); ++i) {
if (*i == &arm) {
world.contact_generators.erase(i);
projectile_released = true;
break;
}
}
}
void draw_objects() {
Util::map(particles, [](Physics::Particle * particle){
Graphics::draw_sphere(*particle, 0.2);
});
// Draw Base
Graphics::draw_3d_rect(
Physics::Particle(),
1, 0.3, 1
);
// Draw stem
Graphics::draw_3d_rect(
Physics::Particle(),
0.2, 4, 0.2
);
// Draw bar
Physics::Vector3 pen_pos = pendulum->get_position();
Physics::Vector3 pen_dir = anchor->get_position().direction(pen_pos);
Physics::real angle = pen_dir.angle(Physics::Vector3(1, 0, 0));
Graphics::draw_3d_rect(
Physics::Particle(0, 2, 0),
4, 0.2, 0.2,
0, 0, -1,
Physics::rads_to_degs(angle)
);
}
void draw_score() {
Physics::Vector3 proj_pos = projectile->get_position();
if (proj_pos.x > 0 && proj_pos.y > 0) score += 1;
Graphics::orthographic_render([](unsigned window_width, unsigned window_height){
char * score_text = (char *)malloc(255);
char * counterweight_text = (char*)malloc(255);
sprintf(score_text, "Score: %d", (int)score);
sprintf(counterweight_text, "Counterweight: %d g", (int)counterweight);
Graphics::render_text((const char *)score_text, Physics::Vector3(50, window_height - 60, 0));
Graphics::render_text((const char *)counterweight_text, Physics::Vector3(50, window_height-80, 0));
Graphics::render_text("Press Enter to Start", Physics::Vector3(50, window_height-100, 0));
Graphics::render_text("'r': Release 'e': Reset", Physics::Vector3(50, window_height-120, 0));
Graphics::render_text("'i': Increase Weight", Physics::Vector3(50, window_height-140, 0));
Graphics::render_text("'u': Decrease Weight", Physics::Vector3(50, window_height-160, 0));
free((void*)score_text);
free((void*)counterweight_text);
});
}
void calculate_physics() {
Physics::real duration = Graphics::get_seconds_per_frame();
if (physics_enabled) world.run_physics(duration);
}
void reset() {
physics_enabled = false;
projectile_released = false;
particles.clear();
world.registry.clear();
world.contact_generators.clear();
score = 0.f;
Util::map(particles, [](Physics::Particle * particle){
particle->clear();
});
initialize();
}
void debug() {
}
void destruct() {
Util::map(particles, [](Physics::Particle * particle){
delete particle;
});
}
int main(int argc, char ** argv) {
Graphics::Graphics(800, 600, argc, argv);
Graphics::register_fire($(physics_enabled = !physics_enabled;), ENTER_KEY);
Graphics::register_fire(release_projectile, 'r');
Graphics::register_fire(reset, 'e');
Graphics::register_fire($(
counterweight += 10;
pendulum->set_mass(counterweight);
), 'i');
Graphics::register_fire($(
if (counterweight - 10 == 0) return;
counterweight -= 10;
pendulum->set_mass(counterweight);
), 'u');
initialize();
Graphics::push_draw_pipeline(draw_score);
Graphics::push_draw_pipeline(Graphics::draw_ground);
Graphics::push_draw_pipeline(Graphics::draw_reference_points);
Graphics::push_draw_pipeline(draw_objects);
Graphics::push_draw_pipeline(calculate_physics);
Graphics::push_draw_pipeline(debug);
Graphics::start();
destruct();
return 0;
}
}