-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeSystem.cpp
256 lines (212 loc) · 5.75 KB
/
TreeSystem.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
#include "TreeSystem.h"
#include <list>
#include <cmath>
#include "rand.h"
#include "helpers.h"
void TreeSystem::make_ground(
float thickness,
float base_angle,
float delta_angle,
const sf::Color& base_color,
const ColorTransform& deltas)
{
const float density_factor = 1.5;
const float block_w = 5;
const float block_h = 1;
const float block_area = block_w * block_h;
const float underground_depth = 5;
auto phys = physicsDimensions();
auto phys2 = physicsHalfDimensions();
const float area = (thickness + underground_depth) * phys.x;
const unsigned density = (area * density_factor) / block_area;
blocks.reserve(blocks.size() + density + 1);
polygonDef def;
def.bodyDef.active = false;
def.bodyDef.fixedRotation = true;
def.bodyDef.type = b2_staticBody;
def.shape.SetAsBox(phys2.x, thickness/2);
def.bodyDef.angle = 0;
def.bodyDef.position.Set(phys2.x, phys.y - (thickness/2));
blocks.emplace_back(
make_shape(world(), def),
base_color);
def.shape.SetAsBox(block_w, block_h);
addProgress(1);
const b2Vec2 topLeft(0, phys.y - thickness);
const b2Vec2 bottomRight(phys.x, phys.y + underground_depth);
maxProgress(blocks.size() + density + 1);
setProgress(blocks.size());
for(int i = 0; i < density; ++i)
{
def.bodyDef.position = random_in(topLeft, bottomRight);
def.bodyDef.angle = to_radians(randcentered(base_angle, delta_angle));
add_block(def, deltas.apply(base_color));
addProgress(1);
}
}
class TreeNode
{
private:
static b2Vec2 root;
const TreeNode* parent;
b2Vec2 self;
float length;
float angle;
bool trunk;
bool inverse;
unsigned id;
public:
TreeNode(TreeNode& p, bool first):
parent(&p),
trunk(first && p.trunk),
inverse(!p.inverse),
id(p.id + 1)
{
if(first)
if(p.trunk)
set_dimensions(p.length * ratio1, 0);
else
set_dimensions(p.length * ratio1, randuniform(min_ba1, max_ba1));
else
if(p.trunk)
set_dimensions(p.length * ratio2, randuniform(min_ca, max_ca));
else
set_dimensions(p.length * ratio2, -randuniform(min_ba2, max_ba2));
}
TreeNode(b2Vec2 new_root, float length):
parent(nullptr),
trunk(true),
inverse(false),
id(0)
{
root = new_root;
set_dimensions(length, 90);
}
b2Vec2 top() const
{
return base() + self;
}
b2Vec2 base() const
{
if(parent)
return parent->top();
else
return root;
}
b2Vec2 center() const
{
return base() + b2Vec2(self.x / 2, self.y / 2);
}
float get_angle() const
{
return angle;
}
float get_length() const
{
return length;
}
unsigned get_id() const
{
return id;
}
void set_dimensions(float l, float a)
{
angle = (parent ? parent->angle : 0) + (to_radians(a) * (inverse ? - 1 : 1));
set_length(l);
}
void set_length(float l)
{
length = l;
self.Set(l * std::cos(angle), -l * std::sin(angle));
}
public:
static float ratio1, ratio2;
static float min_ca, max_ca;
static float min_ba1, max_ba1;
static float min_ba2, max_ba2;
};
float TreeNode::ratio1, TreeNode::ratio2,
TreeNode::min_ca, TreeNode::max_ca,
TreeNode::min_ba1, TreeNode::min_ba2,
TreeNode::max_ba1, TreeNode::max_ba2;
b2Vec2 TreeNode::root;
void TreeSystem::make_tree(
int iterations, b2Vec2 base,
float ratio1, float ratio2,
float min_core_angle, float max_core_angle,
float min_branch_angle_1, float max_branch_angle_1,
float min_branch_angle_2, float max_branch_angle_2,
float initial_size, float leaf_size,
const sf::Color& bark, const ColorTransform& bark_trans,
const std::vector<LeafSystem>& over_leaves,
const std::vector<LeafSystem>& under_leaves)
{
TreeNode::ratio1 = ratio1;
TreeNode::ratio2 = ratio2;
TreeNode::min_ca = min_core_angle;
TreeNode::max_ca = max_core_angle;
TreeNode::min_ba1 = min_branch_angle_1;
TreeNode::max_ba1 = max_branch_angle_1;
TreeNode::min_ba2 = min_branch_angle_2;
TreeNode::max_ba2 = max_branch_angle_2;
//Generate Tree
std::list<TreeNode> grown;
std::list<TreeNode> growing;
std::list<TreeNode> upcoming;
grown.emplace_back(base, initial_size);
growing.emplace_back(grown.back(), true);
for(unsigned i = 0; i < iterations; ++i)
{
for(TreeNode& node: growing)
{
upcoming.emplace_back(node, true);
upcoming.emplace_back(node, false);
}
grown.splice(grown.end(), growing);
growing.splice(growing.end(), upcoming);
}
grown.splice(grown.end(), growing);
auto make_leaves = [&](const std::vector<LeafSystem>& leaves)->void
{
polygonDef leafDef;
leafDef.bodyDef.active = false;
leafDef.bodyDef.type = b2_staticBody;
leafDef.bodyDef.fixedRotation = true;
for(const LeafSystem& system : leaves)
{
leafDef.shape.SetAsBox(5 * system.size, system.size);
for(const TreeNode& node : grown)
{
if(system.min_depth > node.get_id() && system.min_depth >= 0)
continue;
if(system.max_depth < node.get_id() && system.max_depth >= 0)
break;
const float length = node.get_length();
float num_leaves = (length * length * system.density / 2.5);
if(system.density < 0 || (num_leaves < 1 && randuniform(0, 1) < num_leaves))
num_leaves = 1.5;
const unsigned count_leaves = num_leaves;
const auto center = node.center();
for(unsigned i = 0; i < count_leaves; ++i)
{
leafDef.bodyDef.position = random_centered(center, system.density < 0 ? 0 : length / 2);
leafDef.bodyDef.angle = to_radians(randcentered(system.base_dangle, system.delta_dangle));
add_block(leafDef, system.transform.apply(system.color));
}
}
}
};
make_leaves(under_leaves);
polygonDef def;
def.bodyDef.active = false;
def.bodyDef.fixedRotation = true;
def.bodyDef.type = b2_staticBody;
for(TreeNode& node : grown)
{
def.bodyDef.position = node.center();
def.bodyDef.angle = -node.get_angle();
def.shape.SetAsBox(node.get_length() / 2, node.get_length() / 10);
add_block(def, bark_trans.apply(bark));
}
make_leaves(over_leaves);
}