-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.pde
More file actions
143 lines (128 loc) · 3.26 KB
/
Example.pde
File metadata and controls
143 lines (128 loc) · 3.26 KB
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
/*
* Define a universe
*/
Universe u;
int population = 1000;
class ExampleParticle extends Particle {
ExampleParticle(PVector pos) { super(pos); }
void paint() {
if ( hidden ) return;
int particleSize = 10;
pushMatrix();
pushStyle();
translate( position.x, position.y );
ellipseMode(CENTER);
noStroke();
fill(lerpColor(#fc2020, #2045fc, (charge+20.0)/40.0));
ellipse(0, 0, particleSize, particleSize);
popStyle();
popMatrix();
}
}
void settings() {
size(800,600);
smooth();
}
void setup() {
settings();
background(50);
float minX = 50;
float minY = 50;
float maxX = width - minX;
float maxY = height - minY;
/*
* In the standard Processing setup function,
* initialise the universe and populate it with
* particles
*/
u = new Universe();
/*
* Add laws to the universe
* Available laws are:
* Gravity, Coulomb, StokesDrag
* Special laws are also defined to deal with particles
* that reach the edge of the universe
* BounceEdge, WrapEdge, KillEdge
* The edge of the universe defaults to the edge of the
* screen at the time the universe was initialised.
*/
// Makes particles with charge act accordingly
u.addLaw(new Coulomb());
// Applies drag proportional to the velocity of the particle
u.addLaw(new StokesDrag(0.1));
// Makes particles wrap at the edges
u.addLaw(new WrapEdge(minX, minY, maxX - minX, maxY - minY));
// Add Newtonian Mechanics
u.addLaw(new NewtonsLaws());
for (int i = 0; i < population; i++) {
// Initialise a series of particles at random positions
PVector position = new PVector(
random(minX, maxX),
random(minY, maxY)
);
Particle p = new ExampleParticle(position);
// Give each particle a mass...
p.setMass(random(10,20));
// ...and a charge of +10 or -10
p.setCharge(random(100) < 50 ? -10 : +10);
// Add the particle to the universe
u.addThing(p);
}
}
void draw() {
/*
* Blank the screen with transparency set to fade out older
*/
noStroke();
fill(20,10);
rect(0,0,width,height);
/*
* Update the universe (which applies all laws
* and updates the particles
*/
u.update();
/*
* Paint the universe, which calls the paint method
* of the particles in the universe.
* NB - because the default paint behaviour of the
* Particle class is a bit boring, we'd typically want
* to subclass and override the paint method to do something
* more interesting. Here we're just using the default, which
* means we also need to set the stroke colour.
*/
stroke(180);
u.paint();
/*
* Add a label
*/
label("ScrambledPhysics Example sketch", 5, height-5);
/*
* Print frame rate
*/
label(
"Pop: " + population
+ " | Rate: " + round(frameRate*10)/10.0 + " ["
+ (u.barnesHut ? "B" : "")
+ (u.DEBUG ? "D" : "")
+ "]",
width-205, height-5);
}
void label(String message, float x, float y) {
pushMatrix(); pushStyle();
translate(x, y);
noStroke();
fill(180);
rect(0, 0, 205, -18, 5);
fill(20);
text(message, 5, -5);
popStyle(); popMatrix();
}
void keyReleased() {
if ( key == 'b' || key == 'B' ) {
u.barnesHut = ! u.barnesHut;
return;
}
if ( key == 'd' || key == 'D' ) {
u.DEBUG = ! u.DEBUG;
}
}