-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollective.pde
134 lines (107 loc) · 2.88 KB
/
collective.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
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
import peasy.*;
int NO_ELEMENTS = 15;
float NEIGHBOR_DIST = 50;
ArrayList<Vehicle> collective;
TriangularMatrix<Relation> relations;
PeerStrategy peerStrat;
ExchangeStrategy exStrat;
PeasyCam camera;
boolean debug = false;
void setup() {
size(1024, 768, OPENGL);
if (frame != null) {
frame.setResizable(true);
}
camera = new PeasyCam(this, width/2, height/2, 0, 1000);
camera.setMinimumDistance(1);
camera.setMaximumDistance(10000);
camera.setResetOnDoubleClick(false);
relations = new TriangularMatrix<Relation>(NO_ELEMENTS);
for (int i = 0; i < NO_ELEMENTS; ++i) {
for (int j = i + 1; j < NO_ELEMENTS; ++j) {
relations.set(i, j, new Relation());
}
}
PShape shape = loadShape("vehicle.svg");
collective = new ArrayList<Vehicle>();
for (int i = 0; i < NO_ELEMENTS; ++i) {
Vehicle3D v = new Vehicle3D(shape, random(width), random(height));
v.randomizeState();
collective.add(v);
}
peerStrat = new DistancePeerStrategy(NEIGHBOR_DIST);
exStrat = new RandomExchangeStrategy();
}
void draw() {
directionalLight(255, 255, 255, // color
0, 0, -1); // direction
directionalLight(128, 128, 128, // color
1, 0, 0); // direction
background(51);
drawGrid();
// randomlize an entity every once in a while
if (random(50) < 1) {
Vehicle v = collective.get((int) random(NO_ELEMENTS));
v.randomizeState();
v.drawCircleAround(NEIGHBOR_DIST / 2);
}
updateRelations();
exchangeInfo();
for (Vehicle wanderer : collective) {
wanderer.wander();
wanderer.run();
if (debug) wanderer.drawCircleAround(NEIGHBOR_DIST);
}
}
void drawGrid() {
// draw a grid
int wStep = width / 10;
int hStep = height / 10;
pushMatrix();
noFill();
stroke(128, 128);
strokeWeight(1);
beginShape(LINES);
for (int i = 0; i < width; i += wStep) {
vertex(i, 0, 0);
vertex(i, height, 0);
}
for (int i = 0; i < height; i += hStep) {
vertex(0, i, 0);
vertex(width, i, 0);
}
endShape();
popMatrix();
}
void keyPressed() {
switch (keyCode) {
case ' ':
camera.reset();
break;
default:
}
}
void updateRelations() {
for (int i = 0; i < NO_ELEMENTS; ++i) {
Vehicle v1 = collective.get(i);
for (int j = i + 1; j < NO_ELEMENTS; ++j) {
Vehicle v2 = collective.get(j);
float d = v1.location.dist(v2.location);
relations.get(i, j).distance = d;
}
}
}
void exchangeInfo() {
for (int i = 0; i < NO_ELEMENTS; ++i) {
for (int j = i + 1; j < NO_ELEMENTS; ++j) {
Vehicle v1 = collective.get(i);
Vehicle v2 = collective.get(j);
Relation r = relations.get(i, j);
exStrat.exchangeInfo(peerStrat, v1, v2, r);
if (r.inExchange) {
v1.drawCircleAround(NEIGHBOR_DIST);
v2.drawCircleAround(NEIGHBOR_DIST);
}
}
}
}