forked from YohannesTz/BoidsFX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
327 lines (270 loc) · 10.8 KB
/
Main.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//DEPS org.openjfx:javafx-controls:RELEASE:${os.detected.jfxname}
//SOURCES Boid.java
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
private int width = 1100;
private int height = 650;
private final int numBoids = 100;
private final int visualRange = 80;
private Boid[] boids = new Boid[numBoids];
private GraphicsContext gc;
private boolean drawTraces = false;
//Controlling factors
double centeringFactor = 0.005;
int minDistance = 15;
double avoidFactor = 0.05;
double matchingFactor = 0.05;
int speedLimit = 15;
@Override
public void start(Stage primaryStage) {
HBox vbox = new HBox();
Canvas canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D();
initBoids();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long l) {
animationLoop();
}
};
timer.start();
vbox.getChildren().addAll(canvas, buildSlidersBox());
vbox.setBackground(new Background(new BackgroundFill(Color.web("#282B34"), CornerRadii.EMPTY, Insets.EMPTY)));
primaryStage.setTitle("Boids");
primaryStage.setScene(new Scene(vbox));
//Currently Resizing stage with canvas doesn't work
primaryStage.setResizable(false);
primaryStage.show();
}
public VBox buildSlidersBox(){
VBox vBox = new VBox();
//Lables for slider
Label centeringFactorLabel = new Label("Centering Factor");
centeringFactorLabel.setTextFill(Color.web("#fff"));
Label separationLabel = new Label("Minimum Distance");
separationLabel.setTextFill(Color.web("#fff"));
Label matchingFactorLabel = new Label("Matching Factor");
matchingFactorLabel.setTextFill(Color.web("#fff"));
Label speedLimitLabel = new Label("Speed Limit");
speedLimitLabel.setTextFill(Color.web("#fff"));
Label avoidFactorLabel = new Label("Avoid Factor");
avoidFactorLabel.setTextFill(Color.web("#fff"));
//Slider
Slider centeringFactorSlider = new Slider(0.003, 0.007, 0.005);
Slider minDistanceSlider = new Slider(10, 20, 15);
Slider matchingFactorSlider = new Slider(0.03, 0.07, 0.05);
Slider speedLimitSlider = new Slider(10, 25, 15);
Slider avoidFactorSlider = new Slider(0.03, 0.07, 0.05);
//Reset button
Button resetButton = new Button("Reset Settings");
//DrawTraces Checkbox
CheckBox drawTracesCheckBox = new CheckBox();
Label drawTracesLabel = new Label("Draw Traces");
drawTracesLabel.setTextFill(Color.web("#fff"));
drawTracesCheckBox.setSelected(drawTraces);
drawTracesCheckBox.selectedProperty().addListener((observable) -> {
drawTraces = !drawTraces;
});
resetButton.setOnAction(e -> {
centeringFactorSlider.setValue(0.005);
minDistanceSlider.setValue(15);
matchingFactorSlider.setValue(0.05);
speedLimitSlider.setValue(15);
avoidFactorSlider.setValue(0.05);
});
centeringFactorSlider.valueProperty().addListener((observableValue, oldValue, newValue) -> {
System.out.println("Centering Factor - observableValue: " + observableValue.getValue() + ", " + "oldValue: "+ oldValue +", " + "newValue: " + newValue);
centeringFactor = newValue.doubleValue();
});
minDistanceSlider.valueProperty().addListener((observableValue, oldValue, newValue) -> {
System.out.println("MinDistance - observableValue: " + observableValue.getValue() + ", " + "oldValue: "+ oldValue +", " + "newValue: " + newValue);
minDistance = newValue.intValue();
});
matchingFactorSlider.valueProperty().addListener((observableValue, oldValue, newValue) -> {
System.out.println("Matching Factor - observableValue: " + observableValue.getValue() + ", " + "oldValue: "+ oldValue +", " + "newValue: " + newValue);
matchingFactor = newValue.doubleValue();
});
speedLimitSlider.valueProperty().addListener((observableValue, oldValue, newValue) -> {
System.out.println("SpeedLimit - observableValue: " + observableValue.getValue() + ", " + "oldValue: "+ oldValue +", " + "newValue: " + newValue);
speedLimit = newValue.intValue();
});
avoidFactorSlider.valueProperty().addListener((observableValue, oldValue, newValue) -> {
System.out.println("Avoid Factor - observableValue: " + observableValue.getValue() + ", " + "oldValue: "+ oldValue +", " + "newValue: " + newValue);
avoidFactor = newValue.doubleValue();
});
vBox.getChildren().addAll(centeringFactorLabel, centeringFactorSlider, separationLabel,
minDistanceSlider, matchingFactorLabel, matchingFactorSlider,speedLimitLabel, speedLimitSlider, avoidFactorLabel, avoidFactorSlider, resetButton, drawTracesLabel,drawTracesCheckBox);
return vBox;
}
public void initBoids() {
for (int i = 0; i < numBoids; i += 1) {
boids[i] = new Boid(
Math.random() * width,
Math.random() * height,
Math.random() * 10 - 5,
Math.random() * 10 - 5,
new Point2D[0]
);
}
}
public double distance(Boid boid1, Boid boid2) {
return Math.sqrt((boid1.x - boid2.x) * (boid1.x - boid2.x) + (boid1.y - boid2.y) * (boid1.y - boid2.y));
}
//Currently not used so why spent so much time?
/*public Boid[] nClosestBoids(Boid boid, int n) {
Boid[] sorted = new Boid[boids.length];
System.arraycopy(boids, 0, sorted, 0, boids.length);
.
.
//todo - finish this
return sorted;
}*/
public void keepWithinBounds(Boid boid) {
int margin = 100;
int turnFactor = 1;
if (boid.x < margin) {
boid.dx += turnFactor;
}
if (boid.x > width - margin) {
boid.dx -= turnFactor;
}
if (boid.y < margin) {
boid.dy += turnFactor;
}
if (boid.y > height - margin) {
boid.dy -= turnFactor;
}
}
public void flyTowardsCenter(Boid boid) {
double centerX = 0;
double centerY = 0;
int numNeighbors = 0;
for (Boid otherBoid : boids) {
if (distance(boid, otherBoid) < visualRange) {
centerX += otherBoid.x;
centerY += otherBoid.y;
numNeighbors += 1;
}
}
if (numNeighbors != 0) {
centerX = centerX / numNeighbors;
centerY = centerY / numNeighbors;
boid.dx += (centerX - boid.x) * centeringFactor;
boid.dy += (centerY - boid.y) * centeringFactor;
}
}
public void avoidOthers(Boid boid) {
double moveX = 0;
double moveY = 0;
for (Boid otherBoid : boids) {
if (otherBoid != boid) {
if (distance(boid, otherBoid) < minDistance) {
moveX += boid.x - otherBoid.x;
moveY += boid.y - otherBoid.y;
}
}
}
boid.dx += moveX * avoidFactor;
boid.dy += moveY * avoidFactor;
}
public void mathVelocity(Boid boid) {
double avgDX = 0;
double avgDY = 0;
int numNeighbors = 0;
for (Boid otherBoid : boids) {
if (distance(boid, otherBoid) < visualRange) {
avgDX += otherBoid.dx;
avgDY += otherBoid.dy;
numNeighbors += 1;
}
}
if (numNeighbors != 0) {
avgDX = avgDX / numNeighbors;
avgDY = avgDY / numNeighbors;
boid.dx += (avgDX - boid.dx) * matchingFactor;
boid.dy += (avgDY - boid.dy) * matchingFactor;
}
}
public void limitSpeed(Boid boid) {
double speed = Math.sqrt(boid.dx * boid.dx + boid.dy * boid.dy);
if (speed > speedLimit) {
boid.dx = (boid.dx / speed) * speedLimit;
boid.dy = (boid.dy / speed) * speedLimit;
}
}
public void drawGraphics(GraphicsContext ctx, Boid boid) {
double angle = Math.atan2(boid.dy, boid.dx);
ctx.translate(boid.x, boid.y);
ctx.rotate(angle);
ctx.translate(-boid.x, -boid.y);
Color fillColor = Color.web("#558cf4");
Color storkeColor = Color.web("#558cf466");
ctx.setFill(fillColor);
ctx.beginPath();
ctx.moveTo(boid.x, boid.y);
ctx.lineTo(boid.x - 15, boid.y + 5);
ctx.lineTo(boid.x - 15, boid.y - 5);
ctx.lineTo(boid.x, boid.y);
ctx.fill();
ctx.setTransform(1, 0, 0, 1, 0, 0);
if (drawTraces) {
ctx.setStroke(storkeColor);
ctx.beginPath();
ctx.moveTo(boid.history[0].getX(), boid.history[0].getY());
for (Point2D point : boid.history) {
ctx.lineTo(point.getX(), point.getY());
}
ctx.stroke();
}
}
public void animationLoop() {
for (Boid boid : boids) {
flyTowardsCenter(boid);
avoidOthers(boid);
mathVelocity(boid);
limitSpeed(boid);
keepWithinBounds(boid);
boid.x += boid.dx;
boid.y += boid.dy;
boid.history = push(boid.history, boid.x, boid.y);
boid.history = slice(boid.history, boid.history.length - 50, 0);
}
gc.clearRect(0, 0, width, height);
for (Boid boid : boids) {
drawGraphics(gc, boid);
}
}
public Point2D[] push(Point2D[] arr, double x, double y) {
Point2D[] longer = new Point2D[arr.length + 1];
System.arraycopy(arr, 0, longer, 0, arr.length);
longer[arr.length] = new Point2D(x, y);
return longer;
}
public Point2D[] slice(Point2D[] arr, int start, int end) {
if (arr.length > 50) {
Point2D[] slice = new Point2D[start - end];
for (int i = 0; i < slice.length; i++) {
slice[i] = arr[start + i];
}
return slice;
}
return arr;
}
public static void main(String[] args) {
launch(args);
}
}