-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.cpp
668 lines (503 loc) · 12 KB
/
world.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include <GL/glew.h>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cassert>
#include "vec2.h"
#include "vertex_array.h"
#include "piece_pattern.h"
#include "texture.h"
#include "world.h"
namespace {
constexpr auto GRAVITY = 1.f;
constexpr auto DAMPING = .75f;
constexpr auto FRICTION = .6f;
constexpr auto SPAWN_INTERVAL = 30;
constexpr auto BLOCK_SIZE = 20;
}
struct body
{
body(const vec2& position)
: position(position)
, prev_position(position)
{ }
void update_position();
vec2 position;
vec2 prev_position;
};
using body_ptr = std::shared_ptr<body>;
struct spring
{
spring(vec2& p0, vec2& p1)
: p0(p0)
, p1(p1)
, rest_length((p0 - p1).length())
{ }
vec2 &p0, &p1;
float rest_length;
};
struct quad
{
vec2 &p0, uv0;
vec2 &p1, uv1;
vec2 &p2, uv2;
vec2 &p3, uv3;
};
class quad_collision
{
public:
quad_collision(quad& t0, quad& t1)
: t0_(t0), t1_(t1)
{ }
bool operator()();
const vec2& push_vector() const
{ return push_vector_; }
private:
template <bool First>
bool separating_axis_test(const vec2& from, const vec2& to);
quad &t0_, &t1_;
vec2 push_vector_;
};
class piece;
using piece_ptr = std::shared_ptr<piece>;
class piece
{
public:
piece(const piece& other);
piece(const piece_pattern& pattern);
void draw() const;
void update_positions();
void check_constraints(int width, int height);
void collide(piece& other);
void move(const vec2& p);
private:
void update_bounding_box();
rgb color_;
std::shared_ptr<gge::texture> texture_;
std::vector<body> bodies_;
std::vector<spring> springs_;
std::vector<quad> quads_;
vec2 min_pos_, max_pos_;
};
class piece_factory
{
public:
static piece_factory& get_instance();
piece_ptr make_piece(int type) const;
size_t get_num_types() const
{ return pieces_.size(); }
private:
piece_factory();
std::vector<piece_ptr> pieces_;
piece_factory(const piece_factory&) = delete;
piece_factory& operator=(const piece_factory&) = delete;
};
class world_impl
{
public:
world_impl(int width, int height);
void draw() const;
void update();
private:
void draw_walls() const;
std::vector<piece_ptr> pieces_;
int spawn_tic_;
int width_;
int height_;
gge::vertex_array_flat wall_va_;
};
//
// b o d y
//
void
body::update_position()
{
vec2 speed = DAMPING*(position - prev_position);
prev_position = position;
position += speed + vec2(0, -GRAVITY);
}
//
// q u a d _ c o l l i s i o n
//
static std::pair<float, float>
project_quad_to_axis(const vec2& dir, const quad& t)
{
float t0 = dir.dot(t.p0);
float t1 = dir.dot(t.p1);
float t2 = dir.dot(t.p2);
float t3 = dir.dot(t.p3);
float min = std::min(t0, std::min(t1, std::min(t2, t3)));
float max = std::max(t0, std::max(t1, std::max(t2, t3)));
return std::make_pair(min, max);
}
template <bool First>
bool
quad_collision::separating_axis_test(const vec2& from, const vec2& to)
{
vec2 normal = vec2(-(to.y - from.y), to.x - from.x).normalize();
std::pair<float, float> s0 = project_quad_to_axis(normal, t0_);
std::pair<float, float> s1 = project_quad_to_axis(normal, t1_);
static const float EPSILON = 1e-5;
if (s0.second < s1.first + EPSILON || s1.second < s0.first + EPSILON)
return true;
// update push_vector_
float push_length;
if (s0.second - s1.first < s1.second - s0.first) {
push_length = s0.second - s1.first;
} else {
normal = -normal;
push_length = s1.second - s0.first;
}
push_length *= .5*FRICTION;
if (First || push_length*push_length < push_vector_.length_squared())
push_vector_ = normal*(push_length/normal.length());
return false;
}
bool
quad_collision::operator()()
{
if (separating_axis_test<true>(t0_.p0, t0_.p1))
return false;
if (separating_axis_test<false>(t0_.p1, t0_.p2))
return false;
if (separating_axis_test<false>(t0_.p2, t0_.p3))
return false;
if (separating_axis_test<false>(t0_.p3, t0_.p0))
return false;
if (separating_axis_test<false>(t1_.p0, t1_.p1))
return false;
if (separating_axis_test<false>(t1_.p1, t1_.p2))
return false;
if (separating_axis_test<false>(t1_.p2, t1_.p3))
return false;
if (separating_axis_test<false>(t1_.p3, t1_.p0))
return false;
t0_.p0 -= push_vector_;
t0_.p1 -= push_vector_;
t0_.p2 -= push_vector_;
t0_.p3 -= push_vector_;
t1_.p0 += push_vector_;
t1_.p1 += push_vector_;
t1_.p2 += push_vector_;
t1_.p3 += push_vector_;
return true;
}
//
// p i e c e
//
piece::piece(const piece_pattern& pattern)
: color_(pattern.color)
, texture_(make_piece_texture(pattern))
{
texture_->set_wrap_s(GL_CLAMP);
texture_->set_wrap_t(GL_CLAMP);
texture_->set_mag_filter(GL_LINEAR);
texture_->set_min_filter(GL_LINEAR);
texture_->set_env_mode(GL_MODULATE);
std::map<std::pair<int, int>, body *> body_map;
std::set<std::pair<vec2 *, vec2 *>> spring_set;
auto add_body = [&] (int i, int j) -> body * {
auto it = body_map.find(std::make_pair(i, j));
if (it == body_map.end()) {
bodies_.push_back(body(vec2(j*BLOCK_SIZE, i*BLOCK_SIZE)));
it = body_map.insert(it, std::make_pair(std::make_pair(i, j), &bodies_.back()));
}
return it->second;
};
auto add_spring = [&] (vec2& v0, vec2& v1) {
if (spring_set.find(std::make_pair(&v0, &v1)) != spring_set.end())
return;
if (spring_set.find(std::make_pair(&v1, &v0)) != spring_set.end())
return;
springs_.push_back(spring(v0, v1));
spring_set.insert(std::make_pair(&v0, &v1));
};
bodies_.reserve((MAX_PIECE_ROWS + 1)*(MAX_PIECE_COLS + 1));
const float du = static_cast<float>(texture_->get_orig_width())/texture_->get_width()/MAX_PIECE_COLS;
const float dv = static_cast<float>(texture_->get_orig_height())/texture_->get_height()/MAX_PIECE_ROWS;
for (int i = 0; i < MAX_PIECE_ROWS; i++) {
for (int j = 0; j < MAX_PIECE_COLS; j++) {
if (pattern.pattern[i][j] != '#')
continue;
// bodies
body *b0 = add_body(i, j);
body *b1 = add_body(i, j + 1);
body *b2 = add_body(i + 1, j + 1);
body *b3 = add_body(i + 1, j);
// springs
vec2& v0 = b0->position;
vec2& v1 = b1->position;
vec2& v2 = b2->position;
vec2& v3 = b3->position;
add_spring(v0, v1);
add_spring(v1, v2);
add_spring(v2, v3);
add_spring(v3, v0);
add_spring(v0, v2);
add_spring(v1, v3);
// quads
const float u = du*j;
const float v = dv*i;
quads_.push_back(quad{v0, {u, v}, v1, {u + du, v}, v2, {u + du, v + dv}, v3, {u, v + dv}});
}
}
update_bounding_box();
printf("%lu bodies, %lu springs, %lu quads\n", bodies_.size(), springs_.size(), quads_.size());
}
piece::piece(const piece& other)
: color_(other.color_)
, texture_(other.texture_)
, bodies_(other.bodies_)
, min_pos_(other.min_pos_)
, max_pos_(other.max_pos_)
{
std::map<const vec2 *, size_t> pos_body;
for (size_t i = 0; i < other.bodies_.size(); i++)
pos_body[&other.bodies_[i].position] = i;
for (auto& i : other.springs_) {
vec2& p0 = bodies_[pos_body[&i.p0]].position;
vec2& p1 = bodies_[pos_body[&i.p1]].position;
springs_.push_back(spring(p0, p1));
}
for (auto& i : other.quads_) {
vec2& p0 = bodies_[pos_body[&i.p0]].position;
vec2& p1 = bodies_[pos_body[&i.p1]].position;
vec2& p2 = bodies_[pos_body[&i.p2]].position;
vec2& p3 = bodies_[pos_body[&i.p3]].position;
quads_.push_back(quad{p0, i.uv0, p1, i.uv1, p2, i.uv2, p3, i.uv3});
}
}
void
piece::update_positions()
{
for (auto& i : bodies_)
i.update_position();
update_bounding_box();
}
void
piece::check_constraints(int width, int height)
{
// springs
for (auto& i : springs_) {
vec2& p0 = i.p0;
vec2& p1 = i.p1;
vec2 dir = p1 - p0;
float l = dir.length();
float f = .5*(l - i.rest_length)/l;
p0 += f*dir;
p1 -= f*dir;
}
// body-wall collisions
const float bowl_radius = .5*width;
for (auto& i : bodies_) {
vec2& p = i.position;
if (p.y > bowl_radius) {
if (p.x < 0)
p.x += FRICTION*(-p.x);
if (p.x > width)
p.x += FRICTION*(width - p.x);
} else {
vec2 d = p - vec2(bowl_radius, bowl_radius);
float r = d.length();
if (r > bowl_radius)
p -= d*(FRICTION*(r - bowl_radius)/r);
}
}
update_bounding_box();
}
void
piece::move(const vec2& p)
{
for (auto& i : bodies_) {
i.position += p;
i.prev_position = i.position;
}
}
void
piece::collide(piece& other)
{
// bounding box
if (max_pos_.x < other.min_pos_.x)
return;
if (other.max_pos_.x < min_pos_.x)
return;
if (max_pos_.y < other.min_pos_.y)
return;
if (other.max_pos_.y < min_pos_.y)
return;
// quads
bool collided = false;
for (auto& q0 : quads_) {
for (auto& q1 : other.quads_) {
if (quad_collision(q0, q1)())
collided = true;
}
}
if (collided) {
update_bounding_box();
other.update_bounding_box();
}
}
void
piece::draw() const
{
glColor3f(color_.r, color_.g, color_.b);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_TEXTURE_2D);
texture_->bind();
gge::vertex_array_texuv va;
va.reserve(4*quads_.size());
for (auto& i : quads_) {
va.push_back({ i.p0.x, i.p0.y, i.uv0.x, i.uv0.y });
va.push_back({ i.p1.x, i.p1.y, i.uv1.x, i.uv1.y });
va.push_back({ i.p2.x, i.p2.y, i.uv2.x, i.uv2.y });
va.push_back({ i.p3.x, i.p3.y, i.uv3.x, i.uv3.y });
}
va.draw(GL_QUADS);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
}
void
piece::update_bounding_box()
{
assert(!bodies_.empty());
min_pos_ = max_pos_ = bodies_[0].position;
std::for_each(
bodies_.begin() + 1,
bodies_.end(),
[this] (const body& b) {
min_pos_.x = std::min(min_pos_.x, b.position.x);
max_pos_.x = std::max(max_pos_.x, b.position.x);
min_pos_.y = std::min(min_pos_.y, b.position.y);
max_pos_.y = std::max(max_pos_.y, b.position.y);
});
}
//
// p i e c e _ f a c t o r y
//
piece_factory&
piece_factory::get_instance()
{
static piece_factory instance;
return instance;
}
piece_factory::piece_factory()
{
static const piece_pattern patterns[]
{
{ { " ",
" ## ",
" ## ",
" " },
{0, 0, 1} },
{ { " # ",
" # ",
" # ",
" # " },
{0, 1, 0} },
{ { " # ",
" # ",
" ## ",
" " },
{0, 1, 1} },
{ { " # ",
" # ",
" ## ",
" " },
{1, 0, 0} },
{ { " # ",
" ## ",
" # ",
" " },
{1, 0, 1} },
{ { " # ",
" ## ",
" # ",
" " },
{1, 1, 0} },
{ { " # ",
" ## ",
" # ",
" " },
{1, 1, 1} } };
for (auto& i : patterns)
pieces_.push_back(std::make_shared<piece>(i));
}
piece_ptr
piece_factory::make_piece(int type) const
{
return std::make_shared<piece>(*pieces_[type]);
}
//
// w o r l d
//
world_impl::world_impl(int width, int height)
: spawn_tic_(SPAWN_INTERVAL)
, width_(width)
, height_(height)
{
wall_va_.push_back({ 0, static_cast<float>(height_) });
const float bowl_radius = .5*width_;
const int NUM_SEGS = 20;
float a = 0;
const float da = M_PI/(NUM_SEGS - 1);
for (int i = 0; i < NUM_SEGS; i++) {
float x = bowl_radius*(1 - cosf(a));
float y = bowl_radius*(1 - sinf(a));
wall_va_.push_back({ x, y });
a += da;
}
wall_va_.push_back({ static_cast<float>(width_), static_cast<float>(height_) });
}
void
world_impl::draw() const
{
draw_walls();
for (auto& i : pieces_)
i->draw();
}
void
world_impl::draw_walls() const
{
glColor3f(1, 1, 1);
wall_va_.draw(GL_LINE_LOOP);
}
void
world_impl::update()
{
if (!pieces_.empty()) {
for (auto& i : pieces_)
i->update_positions();
static const int NUM_ITERATIONS = 30;
for (int i = 0; i < NUM_ITERATIONS; i++) {
for (auto& i : pieces_)
i->check_constraints(width_, height_);
for (size_t j = 0; j < pieces_.size() - 1; j++)
for (size_t k = j + 1; k < pieces_.size(); k++)
pieces_[j]->collide(*pieces_[k]);
}
}
if (!--spawn_tic_) {
piece_factory& factory = piece_factory::get_instance();
piece_ptr piece = factory.make_piece(rand()%factory.get_num_types());
piece->move(vec2(rand()%(width_ - BLOCK_SIZE*MAX_PIECE_COLS), height_));
pieces_.push_back(piece);
spawn_tic_ = SPAWN_INTERVAL;
}
}
world::world(int width, int height)
: impl_(new world_impl(width, height))
{ }
world::~world() = default;
void
world::draw() const
{
impl_->draw();
}
void
world::update()
{
impl_->update();
}