-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosedSpace.cpp
75 lines (63 loc) · 1.71 KB
/
ClosedSpace.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
#include "Simulation.h"
#include "rand.h"
#include "helpers.h"
class ClosedSpace : public Simulation
{
std::list<b2Body*> bodies;
b2Body* wall1;
b2Body* floor;
b2Body* wall2;
std::string title() {return "Closed Space";}
b2Body* makePlatform(b2Vec2 postion, float hx, float hy, float angle)
{
polygonDef wallDef;
wallDef.bodyDef.position = postion;
wallDef.bodyDef.angle = to_radians(angle);
wallDef.bodyDef.type = b2_staticBody;
wallDef.shape.SetAsBox(hx, hy);
wallDef.fixtureDef.friction = 0.4;
wallDef.fixtureDef.density = 1;
return make_shape(world(), wallDef);
}
InitParams prepare()
{
InitParams params;
params.videoMode = sf::VideoMode(800, 1000);
return params;
}
void initialize()
{
setAllEdges(false);
world().SetGravity(b2Vec2(0, 20));
wall1 = makePlatform(b2Vec2(5, 50), 2, 80, -5);
floor = makePlatform(b2Vec2(0, 100), 50, 2, -5);
wall2 = makePlatform(b2Vec2(50, 100), 2, 35, 5);
}
void render()
{
drawBody(*wall1, sf::Color::Black);
drawBody(*floor, sf::Color::Black);
drawBody(*wall2, sf::Color::Black);
drawBodies(bodies, sf::Color::Black, sf::Color::White, -2);
}
void think(unsigned long frame)
{
if(frame % 5 == 0)
{
polygonDef def;
def.bodyDef.position.Set(randcentered(60, 15), randcentered(-20, 15));
def.bodyDef.angle = to_radians(randcentered(10, 45));
def.bodyDef.linearVelocity = random_centered(b2Vec2(-20, 20), 5);
def.bodyDef.type = b2_dynamicBody;
def.shape.SetAsBox(1, 5);
def.fixtureDef.density = 1;
def.fixtureDef.friction = 0.25;
bodies.push_front(make_shape(world(), def));
}
bodies.remove_if([](b2Body* body)
{
return body->GetPosition().y > 110;
});
}
};
REGISTER_SIMULATION(ClosedSpace, "closed");