-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDustParticle.java
More file actions
108 lines (101 loc) · 2.43 KB
/
DustParticle.java
File metadata and controls
108 lines (101 loc) · 2.43 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
import processing.core.PApplet;
import processing.core.PVector;
public class DustParticle
{
/** The parent PApplet that we will render ourselves onto. */
private PApplet p5;
/** Position */
private PVector pos;
/** Force accumulator acting on the particle */
private PVector force;
/** List of precedent positions for drawing a trail */
private PVector trail[];
/** Number of positions in the trail */
private int sizeTrail;
/** Current particle velocity */
private PVector vel;
/** Particle mass */
private float mass;
/** Noise increment */
private float zincrement;
DustParticle(PApplet p, PVector position, float mass) {
this.p5 = p;
this.pos = position;
this.vel = new PVector();
this.force = new PVector();
this.mass = mass;
this.zincrement = 0.01f;
this.sizeTrail = 10;
this.trail = new PVector[sizeTrail];
for(int i=0; i<sizeTrail; ++i) {
this.trail[i] = pos.get();
}
}
/**
* Animate the dust trails by one step.
* @param zoff Noise offset for fluid motion.
* @param windForce Lateral wind force affecting the motion.
*/
void step(float zoff, PVector windForce) {
float pxoff = zincrement* pos.x;
float pyoff = zincrement* pos.y;
float nval = (p5.noise(pxoff, pyoff, zoff)-0.5f)*2.0f*PApplet.TWO_PI;
force.x = PApplet.cos(nval)*mass;
force.y = PApplet.sin(nval)*mass;
force.mult(0.1f);
vel.add(force);
vel.add(PVector.mult(vel, -0.025f));
vel.add(PVector.mult(windForce, 1.8f));
pos.add(vel);
postStep();
}
/**
* Regroup the trail at the current position.
*/
private void regroupTrail()
{
for(int i=sizeTrail-1; i>0; --i) {
trail[i] = pos.get();
}
}
/**
* Update the trail by updating the chain of last positions.
*/
private void updateTrail()
{
trail[0] = pos.get();
for(int i=sizeTrail-1; i>0; --i) {
trail[i] = trail[i-1].get();
}
}
/**
* Post-step fix. Loop the particles around the edges.
*/
void postStep()
{
int border = 7;
if(pos.x < -border) {
pos.x = p5.width;
regroupTrail();
} else if (pos.x > p5.width+border) {
pos.x = 0;
regroupTrail();
}
if(pos.y < -border) {
pos.y = p5.height;
regroupTrail();
} else if (pos.y > p5.height+border) {
pos.y = 0;
regroupTrail();
}
updateTrail();
}
void draw() {
p5.stroke(255);
p5.strokeWeight(1.0f);
for(int i=0; i<sizeTrail-1; ++i) {
p5.line(trail[i].x, trail[i].y, trail[i+1].x, trail[i+1].y);
}
p5.noStroke();
}
}