3
3
WIND
4
4
Jeff Thompson | 2015 | www.jeffreythompson.org
5
5
6
- A simple simulation of wind blowing a tree. This is essentially
7
- Brownian Motion applied to an angle – the angle of the tree (which
8
- is bent by the wind) moves back-and-forth randomly, just like
9
- an ant wandering around.
6
+ A simple simulation of wind blowing a tree. The angle of the
7
+ tree (which is bent by the "wind") moves back-and-forth randomly
8
+ using Perlin noise, a more realistic way to get random motion.
10
9
11
- A fancier version, commented out, using a recursive function to
12
- draw a Pythagoras Tree.
10
+ Invented in the 1980s by Ken Perlin, it has been used in movies
11
+ and games for 35+ years to generate clouds, terrain, and other
12
+ organic features.
13
+
14
+ Our "tree" is really just a line and a circle: a fancier version,
15
+ commented out, using a recursive function to draw a Pythagoras
16
+ Tree instead of a line-and-circle.
13
17
14
18
CHALLENGES:
15
19
+ How might you visualize the wind onscreen?
16
20
+ Can you draw grass or other elements that are effected by the
17
21
same global wind?
22
+ + Can you draw a leaf at the end of each branch? What about just
23
+ the branches where recursion reaches the minimum length?
18
24
19
25
*/
20
26
21
- float trunkLen = 150 ; // length of the trunk
22
- float angle = 0 ; // starting wind angle
27
+ float trunkLen = 150 ; // length of the trunk
28
+ float windInc = 0.01 ; // how quickly the wind changes speed (try changing)
23
29
24
30
// for Pythagoras Tree version
25
- float branchAngle = 30 ; // angle between branches
26
- int minSize = 10 ; // minimum size of branches
31
+ float branchAngle = 30 ; // angle between two branches
32
+ int minSize = 10 ; // minimum size of branches
33
+
34
+ float windSpeed = 0 ; // speed, which will = angle of bend
35
+ float noisePos = 0 ; // "position" in the Perlin noise
27
36
28
37
29
38
void setup () {
@@ -39,7 +48,7 @@ void draw() {
39
48
// draw a simple tree
40
49
pushMatrix ();
41
50
translate (width / 2 , height );
42
- rotate (radians (angle ));
51
+ rotate (radians (windSpeed ));
43
52
stroke (10 );
44
53
line (0 ,0 , 0 ,- trunkLen);
45
54
fill (255 , 100 );
@@ -51,28 +60,34 @@ void draw() {
51
60
// of fractal pattern using a recursive function
52
61
/*
53
62
translate(width/2, height);
54
- rotate(radians(angle ));
63
+ rotate(radians(windSpeed ));
55
64
stroke(255);
56
65
line(0,0, 0,-trunkLen);
57
66
branch(trunkLen);
58
67
*/
59
68
60
- // update angle with random wind speed
61
- angle += random (- 0.5 ,0.5 );
62
- if (angle > 45 ) angle = 45 ; // don't go too far
63
- else if (angle < 0 ) angle = 0 ;
69
+ // update wind speed using 1D Perlin noise
70
+ // noise() returns a value 0 to 1, so mult by 45 means
71
+ // the wind speed will be 0-45º
72
+ windSpeed = noise (noisePos) * 45 ;
73
+ noisePos += windInc;
74
+
75
+ // random() will generate movement that is jerky and unrealistic
76
+ // try this instead and see what happens
77
+ // windSpeed += random(-0.6,0.5);
64
78
}
65
79
66
80
67
81
// a "recursive" function to draw the tree
82
+ // for more information on recursive functions, see Basics > Recursion
68
83
void branch (float s ) {
69
84
70
85
// store previous and reduce branch length
71
86
float prevS = s;
72
87
s *= 0.5 * sqrt (2 ); // experiment with changing and see what happens
73
88
74
89
// add some twist based on the wind speed
75
- float twist = map (angle , 0 ,45 , 0 ,30 );
90
+ float twist = map (windSpeed , 0 ,45 , 0 ,30 );
76
91
77
92
// keep going until the branches are too small
78
93
if (s > minSize) {
0 commit comments