Skip to content

Commit a209db1

Browse files
committed
Better motion with Perlin noise
1 parent 1074eda commit a209db1

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

InteractionAndGames/Wind/Wind.pde

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@
33
WIND
44
Jeff Thompson | 2015 | www.jeffreythompson.org
55
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.
109
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.
1317
1418
CHALLENGES:
1519
+ How might you visualize the wind onscreen?
1620
+ Can you draw grass or other elements that are effected by the
1721
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?
1824
1925
*/
2026

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)
2329

2430
// 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
2736

2837

2938
void setup() {
@@ -39,7 +48,7 @@ void draw() {
3948
// draw a simple tree
4049
pushMatrix();
4150
translate(width/2, height);
42-
rotate(radians(angle));
51+
rotate(radians(windSpeed));
4352
stroke(10);
4453
line(0,0, 0,-trunkLen);
4554
fill(255, 100);
@@ -51,28 +60,34 @@ void draw() {
5160
// of fractal pattern using a recursive function
5261
/*
5362
translate(width/2, height);
54-
rotate(radians(angle));
63+
rotate(radians(windSpeed));
5564
stroke(255);
5665
line(0,0, 0,-trunkLen);
5766
branch(trunkLen);
5867
*/
5968

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);
6478
}
6579

6680

6781
// a "recursive" function to draw the tree
82+
// for more information on recursive functions, see Basics > Recursion
6883
void branch (float s) {
6984

7085
// store previous and reduce branch length
7186
float prevS = s;
7287
s *= 0.5 * sqrt(2); // experiment with changing and see what happens
7388

7489
// 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);
7691

7792
// keep going until the branches are too small
7893
if (s > minSize) {

0 commit comments

Comments
 (0)