-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlocking.pde
More file actions
127 lines (116 loc) · 2.82 KB
/
Flocking.pde
File metadata and controls
127 lines (116 loc) · 2.82 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
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
import java.util.Random;
Random rand = new Random();
public ArrayList<Bird> birds = new ArrayList<Bird>();
public ArrayList<Predator> predators = new ArrayList<Predator>();
public float[] parameters = new float[11];
void setup(){
size(700, 700);
noStroke();
frameRate(30);
fill(0);
for(int i = 0; i < 200; i++){
birds.add(createRandomBird());
}
updateParameters();
}
void draw(){
background(255);
for(Bird b : birds){
b.updateVelocity();
b.updatePosition();
b.drawBird();
}
for(Predator p : predators){
p.updateVelocity();
p.updatePosition();
p.drawBird();
}
}
Bird createRandomBird(){
int x = rand.nextInt(width);
int y = rand.nextInt(height);
float v_x = (float)rand.nextGaussian()*2;
float v_y = (float)rand.nextGaussian()*2;
PVector v = new PVector(v_x, v_y);
Bird b = new Bird(x, y, v);
return b;
}
<T extends Bird> PVector getClosestBird(Bird thisBird, ArrayList<T> list){
PVector closestDisplacement = new PVector(width, height);
for(Bird b : list){
PVector displacement = PVector.sub(b.position, thisBird.position);
if(displacement.x > width/2){
displacement.x -= width;
}
if(displacement.x < -width/2){
displacement.x += width;
}
if(displacement.y > height/2){
displacement.y -= height;
}
if(displacement.y < -height/2){
displacement.y += height;
}
if(displacement.mag() < closestDisplacement.mag() && displacement.mag() > 0){
closestDisplacement = displacement;
}
}
return closestDisplacement;
}
ArrayList<Bird> getNeighbors(Bird thisBird, float radius, float angle){
ArrayList<Bird> toReturn = new ArrayList<Bird>();
for(Bird b : birds){
PVector displacement = PVector.sub(b.position, thisBird.position);
if(displacement.x > width/2){
displacement.x -= width;
}
if(displacement.y > height/2){
displacement.y -= height;
}
if(displacement.mag() < radius && displacement.mag() > 0){
if(Math.abs(displacement.heading() - thisBird.velocity.heading()) < angle/2){
toReturn.add(b);
}
}
}
return toReturn;
}
void mouseClicked(){
if(mouseButton == LEFT){
predators.add(new Predator(createRandomBird()));
}else if(mouseButton == RIGHT){
if(predators.size() > 0){
predators.remove(predators.size() - 1);
}
}else if(mouseButton == CENTER){
updateParameters();
}
}
void updateParameters(){
BufferedReader input = createReader("Flocking Parameters.txt");
try{
for(int i = 0; i < parameters.length; i++){
String data = input.readLine();
int colonIndex = data.indexOf(':');
data = data.substring(colonIndex + 1, data.length()).trim();
parameters[i] = Float.parseFloat(data);
}
}
catch(IOException e){
e.printStackTrace();
}
catch(RuntimeException e){
e.printStackTrace();
}
finally{
try{
input.close();
}
catch(IOException e){
e.printStackTrace();
}
catch(RuntimeException e){
e.printStackTrace();
}
}
}