-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchFlocking3.java
executable file
·174 lines (140 loc) · 5.42 KB
/
SketchFlocking3.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
package de.hfkbremen.algorithmiccliches.examples;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PMatrix3D;
import processing.core.PVector;
import teilchen.BasicBehaviorParticle;
import teilchen.Physics;
import teilchen.behavior.Alignment;
import teilchen.behavior.Cohesion;
import teilchen.behavior.Motor;
import teilchen.behavior.Separation;
import teilchen.behavior.Wander;
import teilchen.constraint.Teleporter;
import teilchen.force.ViscousDrag;
import java.util.ArrayList;
public class SketchFlocking3 extends PApplet {
private Physics mPhysics;
private ArrayList<SwarmEntity> mSwarmEntities;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
frameRate(60);
rectMode(CENTER);
hint(DISABLE_DEPTH_TEST);
textFont(createFont("Courier", 11));
/* physics */
mPhysics = new Physics();
Teleporter mTeleporter = new Teleporter();
mTeleporter.min().set(0, 0, height / -2.0f);
mTeleporter.max().set(width, height, height / 2.0f);
mPhysics.add(mTeleporter);
ViscousDrag myViscousDrag = new ViscousDrag();
mPhysics.add(myViscousDrag);
/* setup entities */
mSwarmEntities = new ArrayList<>();
for (int i = 0; i < 80; i++) {
SwarmEntity mSwarmEntity = new SwarmEntity();
mSwarmEntity.position().set(random(mTeleporter.min().x, mTeleporter.max().x),
random(mTeleporter.min().y, mTeleporter.max().y),
random(mTeleporter.min().z, mTeleporter.max().z));
mSwarmEntities.add(mSwarmEntity);
mPhysics.add(mSwarmEntity);
}
}
public void draw() {
final float mDeltaTime = 1.0f / frameRate;
/* physics */
mPhysics.step(mDeltaTime);
/* entities */
for (SwarmEntity s : mSwarmEntities) {
s.update(mDeltaTime);
}
/* draw */
background(255);
for (SwarmEntity s : mSwarmEntities) {
s.draw(g);
}
/* draw extra info */
fill(0);
noStroke();
text("ENTITIES : " + mSwarmEntities.size(), 10, 12);
text("FPS : " + (int) frameRate, 10, 24);
}
private class SwarmEntity
extends BasicBehaviorParticle {
private final Separation<SwarmEntity> separation;
private final Alignment<SwarmEntity> alignment;
private final Cohesion<SwarmEntity> cohesion;
private final Wander wander;
private final Motor motor;
public SwarmEntity() {
maximumInnerForce(random(100.0f, 1000.0f));
radius(10f);
separation = new Separation<>();
separation.proximity(20);
separation.weight(50.0f);
behaviors().add(separation);
alignment = new Alignment<>();
alignment.proximity(60);
alignment.weight(30.0f);
behaviors().add(alignment);
cohesion = new Cohesion<>();
cohesion.proximity(200);
cohesion.weight(5.0f);
behaviors().add(cohesion);
wander = new Wander();
behaviors().add(wander);
motor = new Motor();
motor.auto_update_direction(true);
motor.strength(20.0f);
behaviors().add(motor);
}
public void update(float theDeltaTime) {
separation.neighbors(mSwarmEntities);
alignment.neighbors(mSwarmEntities);
cohesion.neighbors(mSwarmEntities);
}
private void draw(PGraphics g) {
pushMatrix();
{
translate(position().x, position().y, position().z);
pushMatrix();
{
PMatrix3D p = new PMatrix3D();
teilchen.util.Util.pointAt(p,
position(),
new PVector(0, 1, 0),
PVector.add(position(), velocity()));
applyMatrix(p);
pushMatrix();
{
stroke(255, 127, 0, 31);
noFill();
ellipse(0, 0, separation.proximity() * 2, separation.proximity() * 2);
ellipse(0, 0, alignment.proximity() * 2, alignment.proximity() * 2);
ellipse(0, 0, cohesion.proximity() * 2, cohesion.proximity() * 2);
rotateX(HALF_PI);
ellipse(0, 0, separation.proximity() * 2, separation.proximity() * 2);
ellipse(0, 0, alignment.proximity() * 2, alignment.proximity() * 2);
ellipse(0, 0, cohesion.proximity() * 2, cohesion.proximity() * 2);
}
popMatrix();
noStroke();
fill(0, 127, 255);
scale(1, 0.25f, 3);
box(6);
}
popMatrix();
/* velocity */
stroke(0, 127, 255, 127);
line(0, 0, 0, velocity().x, velocity().y, velocity().z);
}
popMatrix();
}
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchFlocking3.class.getName()});
}
}