-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_planet.pde
390 lines (343 loc) · 8.62 KB
/
tree_planet.pde
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
/*
Based on: https://www.openprocessing.org/sketch/567018
Santiago Fiorino
*/
Planet p;
void setup() {
size(700, 700);
p = new Planet(width / 2, height / 2, 100);
}
void draw() {
background(233);
p.update();
p.display();
}
void mousePressed() {
if (p.isInBody()) {
p.addTree();
}
}
void mouseMoved() {
p.rotateAngle();
}
class Planet {
final int MAX_TREE_COUNT = 3;
float x, y, r, rotateTheta;
float oribitWidth, oribitHeight;
float ballAngle, ballSpeed;
ArrayList<Tree> trees;
Planet(float _x, float _y, float _r) {
x = _x;
y = _y;
trees = new ArrayList();
r = _r;
oribitWidth = 5 * _r;
oribitHeight = _r;
ballSpeed = 1.5;
rotateTheta = radians( - 25);
}
void update() {
// delete extra tree
int cnt = trees.size() - MAX_TREE_COUNT;
if (cnt >= 0) {
for (int i = 0; i < cnt; i++) {
trees.get(i).die();
}
}
// update the trees
for (int i = trees.size() - 1; i >= 0; i--) {
Tree t = trees.get(i);
if (t.isDead) {
trees.remove(t);
continue;
}
t.angle = angleByTheta(t.theta);
t.r = rByTheta(t.theta);
t.update();
}
// update the ball
ballAngle += ballSpeed;
ballAngle %= 360;
}
float rByTheta(float theta) {
float scale = noise(sin(theta) + 1, cos(theta) + 1, float(frameCount) / 100);
return r * (scale / 2 + 1);
}
float angleByTheta(float theta) {
final float offset = 0.06;
float theta1 = theta - offset, theta2 = theta + offset;
float a = rByTheta(theta1);
float b = rByTheta(theta2);
return atan2(a * sin(theta1) - b * sin(theta2), a * cos(theta1) - b * cos(theta2));
}
void displayTrace(float startAngle, float endAngle) {
pushMatrix();
// shake
float theta = map(noise(float(frameCount) / 100), 0, 1, 0, 25);
rotate(radians(theta));
// back
noFill();
stroke(233);
strokeWeight(12);
arc(0, 0, oribitWidth, oribitHeight, startAngle, endAngle);
// front
stroke(0);
strokeWeight(4);
arc(0, 0, oribitWidth, oribitHeight, startAngle, endAngle);
popMatrix();
}
void displayBody() {
fill(0);
strokeWeight(5);
stroke(233);
beginShape();
for (int i = 0; i <= 360; i += 5) {
float theta = radians(i);
float pr = rByTheta(theta);
float px = pr * cos(radians(i)), py = pr * sin(radians(i));
vertex(px, py);
}
endShape();
}
void displayBall() {
pushMatrix();
//shake
float seed = noise(float(frameCount) / 100);
float angle = map(seed, 0, 1, 0, 25);
float offsetY = map(seed, 0, 1, -20, 20);
rotate(radians(angle));
float theta = radians(ballAngle);
fill(240);
strokeWeight(5);
stroke(0);
ellipse(oribitWidth * cos(theta) / 2, oribitHeight * sin(theta) / 2 + offsetY, 35, 35);
popMatrix();
}
void display() {
translate(x, y);
rotate(rotateTheta);
// display the body and trace of the planet
displayTrace(PI, TWO_PI);
if (ballAngle < 180) {
displayBody();
displayTrace(0, PI);
displayBall();
} else {
displayBall();
displayBody();
displayTrace(0, PI);
}
// display the trees
for (Tree t : trees) {
t.display();
}
}
void addTree() {
float mx = mouseX, my = mouseY;
float theta = atan2(my - y, mx - x) - rotateTheta;
int type = int(random(3));
Tree t = new Tree(theta, type);
t.r = rByTheta(t.theta);
t.angle = angleByTheta(t.theta);
trees.add(t);
}
void rotateAngle() {
if (!isInBody()) {
float theta1 = atan2(mouseY - y, mouseX - x);
float theta2 = atan2(pmouseY - height / 2, pmouseX - width / 2);
rotateTheta += theta1 - theta2;
}
}
boolean isInBody() {
float mx = mouseX, my = mouseY;
float theta = atan2(my - y, mx - x);
float r = rByTheta(theta);
if (dist(mx, my, x, y) < r) {
return true;
}
return false;
}
}
class Tree {
static final int MAX_DEPTH = 4, MAX_LEN = 60, MIN_LEN = 30;
float theta, r, angle;
ArrayList<Branch> branches;
ArrayList<Flower> flowers;
PVector location;
boolean isDead, isBlow;
int type;
Tree(float _theta, int _type) {
theta = _theta;
branches = new ArrayList();
flowers = new ArrayList();
Branch root = new Branch(0, 0, HALF_PI, random(MIN_LEN, MAX_LEN), 0, null);
root.isGrowing = true;
type = _type;
location = new PVector();
addBraches(root);
}
void addBraches(Branch b) {
branches.add(b);
int cnt = int(random(2, 4));
if (b.level > MAX_DEPTH) {
for (int i = 0; i < cnt; i++) {
float offset = random( - 10, 10);
float c1 = random(255), c2 = random(255);
color c = type == 0 ? color(c1, c2, 255) : type == 1 ? color(c1, 255, c2) : color(255, c1, c2);
Flower f = new Flower(b, offset, c);
flowers.add(f);
}
} else {
float startAngle = -PI / 4, endAngle = PI / 4;
float angle = (endAngle - startAngle) / cnt;
for (int i = 0; i < cnt; i++) {
float offset = random(startAngle + angle * i, startAngle + angle * (i + 1));
Branch t = new Branch(b.to.x, b.to.y, b.theta - offset, b.len * 0.8, b.level + 1, b);
addBraches(t);
}
}
}
void die() {
for (Branch b : branches) {
if (b.level == MAX_DEPTH + 1) {
b.isDying = true;
}
}
}
void update() {
location.set(r * cos(theta), r * sin(theta));
if (branches.get(0).isDead) {
isDead = true;
}
for (Branch b : branches) {
b.update();
}
for (int i = 0; i < flowers.size(); i++) {
Flower f = flowers.get(i);
if (f.isFlying && !f.isDown) {
PVector gravity = PVector.sub(new PVector(0, -100), f.location);
if (abs(gravity.mag() - random(20, 80)) < 10) {
f.velocity.set(0, 0);
f.accerlation.set(0, 0);
f.isDown = true;
} else {
gravity.normalize();
gravity.mult(0.1);
f.applyForce(gravity);
}
}
f.update();
}
}
void blow() {
isBlow = true;
for (Flower f : flowers) {
f.down();
f.isFlying = true;
}
}
void display() {
pushMatrix();
translate(location.x, location.y);
rotate(angle);
for (Branch b : branches) {
if (PVector.dist(b.from, b.current) > 0.1) {
b.display();
}
}
for (Flower f : flowers) {
if (!f.isDown) {
f.display();
}
}
popMatrix();
}
}
class Branch {
Branch father;
PVector from, to, current;
float theta, len, sw;
boolean isDying, isGrowing, isGrown, isDead;
int level;
Branch(float fromX, float fromY, float _theta, float _len, int _level, Branch _father) {
theta = _theta;
len = _len;
level = _level;
sw = map(level, 0, Tree.MAX_DEPTH, 5, 2);
from = new PVector(fromX, fromY);
current = new PVector(fromX, fromY);
to = new PVector(fromX + len * cos(theta), fromY + len * sin(theta));
father = _father;
}
void update() {
if (isDying) {
current = PVector.lerp(current, from, 0.2);
if (PVector.dist(current, from) < 0.1) {
isDead = true;
if (father != null) {
father.isDying = true;
}
}
} else if (isGrowing) {
current = PVector.lerp(current, to, 0.1);
if (PVector.dist(current, to) < 0.1) {
isGrown = true;
}
} else if (!isGrowing && !isDying) {
if (father.isGrown) {
isGrowing = true;
}
}
}
void display() {
strokeWeight(sw);
stroke(0);
line(from.x, from.y, current.x, current.y);
}
}
class Flower {
PVector velocity, accerlation, location, origin;
boolean isGrown, isFlying, isDown;
Branch father;
color c;
Flower(Branch _father, float _offset, color _c) {
father = _father;
location = new PVector(father.to.x + _offset, father.to.y + _offset);
velocity = new PVector();
accerlation = new PVector();
origin = new PVector(0, -100);
c = _c;
}
void down() {
velocity = PVector.random2D();
velocity.mult(1.5);
}
void applyForce(PVector force) {
if (isGrown) {
accerlation.add(force);
}
}
void update() {
if (father.isGrown) {
isGrown = true;
}
if (father.isDying) {
if (!isDown && !isFlying) {
down();
isFlying = true;
}
}
if (isGrown) {
velocity.add(accerlation);
location.add(velocity);
accerlation.mult(0);
}
}
void display() {
if (isGrown) {
fill(c);
noStroke();
ellipse(location.x, location.y, 6, 6);
}
}
}