-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle.cpp
62 lines (47 loc) · 1.26 KB
/
Particle.cpp
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
#include "Particle.h"
Particle::Particle() {
// initialize particle with some reasonable values first;
//
velocity.set(0, 0, 0);
acceleration.set(0, 0, 0);
position.set(0, 0, 0);
forces.set(0, 0, 0);
lifespan = 5;
birthtime = 0;
radius = .1;
damping = .99;
mass = 1;
color = ofColor::white;
}
void Particle::draw() {
ofSetColor(color);
//ofSetColor(ofMap(age(), 0, lifespan, 255, 10), 0, 0);
ofDrawSphere(position, radius);
}
// write your own integrator here.. (hint: it's only 3 lines of code)
//
void Particle::integrate() {
// interval for this step
//
float dt = 1.0 / ofGetFrameRate();
// update position based on velocity
//
position += (velocity * dt);
// update acceleration with accumulated paritcles forces
// remember : (f = ma) OR (a = 1/m * f)
//
ofVec3f accel = acceleration; // start with any acceleration already on the particle
accel += (forces * (1.0 / mass));
velocity += accel * dt;
// add a little damping for good measure
//
velocity *= damping;
// clear forces on particle (they get re-added each step)
//
forces.set(0, 0, 0);
}
// return age in seconds
//
float Particle::age() {
return (ofGetElapsedTimeMillis() - birthtime)/1000.0;
}