-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoids.pde
105 lines (97 loc) · 3.41 KB
/
Boids.pde
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
/*
Implemented from https://github.com/beneater/boids/blob/master/boids.js
*/
/*
DONE: use Wall class instead of PVectors to use Boids with Rays
TODO: optimisations
DONE: 1. combine speed and direction functions in Boid.pde
TODO: 2. use quadtree instead of grid partitioning
TODO: 3. use single points for Wall vertices to half vertices stored and check collisions in clockwise order of points
TODO: 4. use only wall vertices intersecting boid's surrounding cells to check collisions
*/
int numBoids = 1000;
Flock flock;
ArrayList<Boid> boids = new ArrayList<Boid>();
ArrayList<PVector> walls = new ArrayList<>();
ArrayList<Wall> bwalls = new ArrayList<>();
void setup() {
fullScreen();
// size(2560, 1600);
// size(1024, 512);
// frameRate(60);
bwalls = new ArrayList<>();
bwalls.add(new Wall(0, 0, width, height)); // border
bwalls.add(new Wall(width/2, 0, 20, 80)); // guard
bwalls.add(new Wall(width/2, height - 80, 20, 80)); // guard
bwalls.add(new Wall(0, height/2, 80, 20)); // guard
bwalls.add(new Wall(width - 80, height/2, 80, 20)); // guard
for (int i = 0; i < 50; i++) {
Wall wall = new Wall(int(random(1) * width), int(random(1) * height), int(random(1) * 100) + 10, int(random(1) * 100) + 10);
bwalls.add(wall);
}
flock = new Flock(numBoids, width, bwalls);
}
void draw() {
SM.updateDelta();
background(150);
for (int i = 0; i < bwalls.size(); i++) {
Wall w = bwalls.get(i);
w.drawShape(i == 0 ? color(255, 0) : #ffffff);
}
if (bwalls.size() > 1) {
bwalls.set(
bwalls.size() - 1, new Wall(mouseX, mouseY, (int)bwalls.get(bwalls.size() - 1).extents.x,
(int)bwalls.get(bwalls.size() - 1).extents.y));
}
flock.update(bwalls);
// update text every short while
if (frameCount % 10 == 0) {
SM.countBoidTypes(flock);
}
float textXPos = width - 210;
strokeWeight(2);
stroke(0);
textSize(15);
fill(0);
text("Show Debug (TAB): " + (SM.showDebug ? "ON" : "OFF"), textXPos, 30);
if (SM.showDebug) {
fill(255, 50, 50, 50);
rect(textXPos - 20, 0, 300, 240);
fill(0);
text("FPS: " + Float.valueOf(frameRate), textXPos, 60);
text("Frame: " + Integer.valueOf(frameCount), textXPos, 75);
for (int i = 0; i < BoidType.values().length; i++) {
BoidType bt = BoidType.values()[i];
text(bt.name() + " Count: " + Integer.valueOf(SM.boidTypeCounts.getOrDefault(bt, 0)), textXPos, 90 + (i * 15));
}
fill(0);
text("Show Rays (A): " + (SM.showRays ? "ON" : "OFF"), textXPos, 180);
text("Attacking (Q): " + (SM.canAttack ? "ON" : "OFF"), textXPos, 195);
text("Show Boids (X): " + (SM.showBoids ? "ON" : "OFF"), textXPos, 210);
text("Show Grid (Z): " + (SM.showGrid ? "ON" : "OFF"), textXPos, 225);
} else {
fill(255, 50, 50, 50);
rect(textXPos - 20, 0, 300, 50);
}
}
void keyPressed() {
if (key == ' ') {
SM.canAttack = false;
flock.reset();
}
if (keyCode == TAB) {
SM.showDebug = !SM.showDebug;
}
if (key == 'a' || key == 'A') {
SM.showRays = !SM.showRays;
}
if (key == 'q' || key == 'Q') {
SM.canAttack = !SM.canAttack;
}
if (key == 'x' || key == 'X') {
SM.showBoids = !SM.showBoids;
}
if (key == 'z' || key == 'Z') {
SM.showGrid = !SM.showGrid;
}
}