-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoid.java
173 lines (132 loc) · 5.5 KB
/
Boid.java
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
import java.util.Arrays;
import java.util.Random;
class Boid {
private Random random = new Random();
final int colorR = random.nextInt(100, 256);
final int colorG = random.nextInt(100, 256);
final int colorB = random.nextInt(100, 256);
private int MAX_VELOCITY = 5;
private float PERCEPTION_DISTANCE = 50; // radius where boid checks for other boids to align
private double MAX_FORCE = 0.2; // max amount of steering a boid can do at once
public Vector2 position = new Vector2(); // vector that is iinitialized to zero
public Vector2 velocity = new Vector2();
public Vector2 acceleration = new Vector2();
// acceleration gets reset to 0 every frame and calculated byu the three rules
// to then add to the velocity
Boid(int screen_width, int screen_height) {
this.position.v = new double[]{random.nextInt(screen_width), random.nextInt(screen_height)};
this.velocity.randomize(-MAX_VELOCITY, MAX_VELOCITY);
// this.velocity.normalize();
}
public void align(Boid[] boids, float multiplier, float perception_distance_multiplier) {
int total = 0;
Vector2 avg = new Vector2(); // desired velocity to steer to
for (int i = 0; i < boids.length; i++) {
Boid b = boids[i];
if (b == this) continue; // avoid to count for the same boid
// check if b is in vicinity
float d = (float)Math.sqrt( Math.pow(b.position.x() - this.position.x(), 2) + Math.pow(b.position.y() - this.position.y(), 2) );
if (d > PERCEPTION_DISTANCE * perception_distance_multiplier)
continue;
avg.add(b.velocity);
total++;
}
if (total > 0) {
avg.v[0] /= total;
avg.v[1] /= total;
avg.setMag(MAX_VELOCITY); // to not slow down all boids
// steer towards average
avg.sub(this.velocity); // remove current velocity
avg.limit(this.MAX_FORCE); // avoid to steer to it immidiately // NOTE: test multiplying the multiplier with the max force
}
avg.v[0] *= multiplier;
avg.v[1] *= multiplier;
this.acceleration.add(avg);
}
public void cohesion(Boid[] boids, float multiplier, float perception_distance_multiplier) {
int total = 0;
Vector2 avg = new Vector2(); // desired velocity to steer to
for (int i = 0; i < boids.length; i++) {
Boid b = boids[i];
if (b == this) continue; // avoid to count for the same boid
// check if b is in vicinity
float d = (float)Math.sqrt( Math.pow(b.position.x() - this.position.x(), 2) + Math.pow(b.position.y() - this.position.y(), 2) );
if (d > PERCEPTION_DISTANCE * perception_distance_multiplier)
continue;
avg.add(b.position);
total++;
}
if (total > 0) {
avg.v[0] /= total;
avg.v[1] /= total;
avg.sub(this.position);
avg.setMag(MAX_VELOCITY);
// steer towards average
avg.sub(this.velocity);
avg.limit(this.MAX_FORCE);
}
avg.v[0] *= multiplier;
avg.v[1] *= multiplier;
this.acceleration.add(avg);
}
public void separation(Boid[] boids, float multiplier, float perception_distance_multiplier) {
int total = 0;
Vector2 avg = new Vector2(); // desired velocity to steer to
for (int i = 0; i < boids.length; i++) {
Boid b = boids[i];
if (b == this) continue; // avoid to count for the same boid
// check if b is in vicinity
float d = (float)Math.sqrt( Math.pow(b.position.x() - this.position.x(), 2) + Math.pow(b.position.y() - this.position.y(), 2) );
if (d > PERCEPTION_DISTANCE * perception_distance_multiplier)
continue;
Vector2 diff = new Vector2();
diff.v = this.position.v.clone();
diff.sub(b.position); // vector points from b to this
if (d != 0) {
diff.v[0] /= d; // make it inversely proportional to the distance
diff.v[1] /= d;
}
avg.add(diff);
total++;
}
if (total > 0) {
avg.v[0] /= total;
avg.v[1] /= total;
avg.setMag(MAX_VELOCITY);
// steer towards average
avg.sub(this.velocity);
avg.limit(this.MAX_FORCE);
}
avg.v[0] *= multiplier;
avg.v[1] *= multiplier;
this.acceleration.add(avg);
}
public void update(int screen_width, int screen_height) {
this.position.add(velocity);
this.velocity.add(this.acceleration);
// wrap around the screen
if (this.position.x() > screen_width) {
this.position.v[0] = 0;
}
else if (this.position.x() < 0) {
this.position.v[0] = screen_width;
}
if (this.position.y() > screen_height) {
this.position.v[1] = 0;
}
else if (this.position.y() < 0) {
this.position.v[1] = screen_height;
}
// reset acceleration (wil be set by three rules)
this.acceleration.v = new double[2];
}
public static void main(String[] args) {
Boid b = new Boid(800, 600);
// print position vector
System.out.println("before");
b.position.print();
b.update(0, 0);
System.out.println("after");
b.position.print();
}
}