-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle.pde
73 lines (65 loc) · 1.64 KB
/
Particle.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
class Particle
{
PImage image;
PGraphics layer;
PVector position;
PVector velocity; // pixels per second (or should it be size-independant?)
PVector initialVelocity;
float velocityBlend = 0; // [0..1], 0 = only velocity, 1 = only initialVelocity, inbetween = blend
PVector acceleration; // pixels per second per second
float life; // seconds
boolean useLife = false;
boolean dead = false;
boolean frozen = false;
Particle(PGraphics layer, PImage image)
{
this.layer = layer;
this.image = image;
this.position = new PVector(0, 0);
this.velocity = new PVector(0, 0);
this.acceleration = new PVector(0, 0);
this.life = 20;
}
void setVelocity(PVector velocity)
{
this.velocity = velocity.get();
this.initialVelocity = velocity.get();
}
void setForce(PVector force)
{
this.acceleration = force.get();
}
void applyForce(PVector force)
{
this.acceleration.add(force);
}
void update(float dt)
{
if (frozen)
return;
// dp = v * dt
PVector v = PVector.add( PVector.mult(initialVelocity, velocityBlend), PVector.mult(velocity, 1.0 - velocityBlend) );
PVector dp = PVector.mult(v, dt);
this.position.add(dp);
// dv = a * dt
PVector dv = PVector.mult(this.acceleration, dt);
this.velocity.add(dv);
//println("dt = " + dt + ", dp = " + dp + ", dv = " + dv);
life = life - dt;
}
void draw()
{
// tint(life/100.0*255);
layer.image(this.image, position.x, position.y);
}
boolean isDead()
{
if (useLife && (life <= 0))
dead = true;
return dead;
}
boolean isFrozen()
{
return frozen;
}
}