forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuff.pde
More file actions
92 lines (79 loc) · 2.16 KB
/
Copy pathPuff.pde
File metadata and controls
92 lines (79 loc) · 2.16 KB
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
/**
* Puff
* by Ira Greenberg.
*
* Series of ellipses simulating a multi-segmented
* organism, utilizing a follow the leader algorithm.
* Collision detection occurs on the organism's head,
* controlling overall direction, and on the individual
* body segments, controlling body shape and jitter.
*/
// For puff head
float headX;
float headY;
float speedX = .7;
float speedY = .9;
// For puff body
int cells = 1000;
float[] px= new float[cells];
float[] py= new float[cells];
float[] radiiX = new float[cells];
float[] radiiY = new float[cells];
float[] angle = new float[cells];
float[] frequency = new float[cells];
float[] cellRadius = new float[cells];
void setup(){
size(640, 360);
// Begin in the center
headX = width/2;
headY = height/2;
// Fill body arrays
for (int i = 0; i < cells; i++){
radiiX[i] = random(-7, 7);
radiiY[i] = random(-4, 4);
frequency[i]= random(-9, 9);
cellRadius[i] = random(16, 30);
}
frameRate(30);
}
void draw(){
background(0);
noStroke();
fill(255, 255, 255, 5);
// Follow the leader
for (int i = 0; i < cells; i++){
if (i == 0){
px[i] = headX + sin(radians(angle[i]))*radiiX[i];
py[i] = headY + cos(radians(angle[i]))*radiiY[i];
}
else{
px[i] = px[i-1] + cos(radians(angle[i]))*radiiX[i];
py[i] = py[i-1] + sin(radians(angle[i]))*radiiY[i];
// Check collision of body
if ((px[i] >= width-cellRadius[i]/2) || (px[i] <= cellRadius[i]/2)){
radiiX[i]*=-1;
cellRadius[i] = random(1, 40);
frequency[i]= random(-13, 13);
}
if ((py[i] >= height-cellRadius[i]/2) || (py[i] <= cellRadius[i]/2)){
radiiY[i]*=-1;
cellRadius[i] = random(1, 40);
frequency[i]= random(-9, 9);
}
}
// Draw puff
ellipse(px[i], py[i], cellRadius[i], cellRadius[i]);
// Set speed of body
angle[i] += frequency[i];
}
// Set velocity of head
headX += speedX;
headY += speedY;
// Check boundary collision of head
if ((headX >= width-cellRadius[0]/2) || (headX <=cellRadius[0]/2)){
speedX*=-1;
}
if ((headY >= height-cellRadius[0]/2) || (headY <= cellRadius[0]/2)){
speedY*=-1;
}
}