noc04_particleSystems

Particle System

Source Code

ofApp.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "ofMain.h"
#include "particleSystem.hpp"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
vector<particleSystem> ps;
ofEasyCam cam;
ofLight light;
};

ofApp.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
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofEnableDepthTest();
ofBackground(0);
ofSetColor(255);
//----light--------//
light.setAreaLight(100, 100);
light.setDiffuseColor(ofFloatColor(1.0,1.0,1.0)); // light has several types,try every type
light.setSpecularColor(ofFloatColor(0.7,0.7,0.7));
light.setPosition(0,0,0); // light position
light.enable();
for(int i = 0; i < 2000; i++){
particleSystem myParticle;
myParticle.setup();
ps.push_back(myParticle);
}
}
//--------------------------------------------------------------
void ofApp::update(){
for ( int i = 0; i < ps.size(); i ++){
ps[i].update();
ps[i].addRepulsionForce(ofPoint(ofNoise(ofGetElapsedTimef()+100)*100,ofNoise(ofGetElapsedTimef()+500)*100,ofNoise(ofGetElapsedTimef()+1000)*100), 200, 50);
ps[i].addAttractionForce(ofPoint(ofNoise(ofGetElapsedTimef()+100)*100,ofNoise(ofGetElapsedTimef()+500)*100,ofNoise(ofGetElapsedTimef()+1000)*100), 700, 200);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
cam.begin();
for(int i = 0; i< ps.size(); i ++){
ps[i].draw();
}
cam.end();
}

particleSystem.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#include "ofMain.h"
class particleSystem{
public:
void setup();
void update();
void draw();
void addForce(ofPoint _force);
void addRepulsionForce(ofPoint _posOfForce, float radius, float strength);
void addAttractionForce(ofPoint _posOfForce, float radius, float strength);
ofPoint pos, vel, acc;
float damp;
};

particleSystem.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "particleSystem.hpp"
//--------------------------------------------------------------
void particleSystem::setup(){
pos.set(ofRandom(-300,300),ofRandom(-300,300),ofRandom(-300,300));
vel.set(0,0,0);
acc.set(0,0,0);
damp = 0.995;
}
//--------------------------------------------------------------
void particleSystem::update(){
pos += vel;
vel += acc;
vel *= damp;
acc *= 0.0;
}
//--------------------------------------------------------------
void particleSystem::draw(){
ofDrawSphere(pos, 1);
}
//--------------------------------------------------------------
void particleSystem::addForce(ofPoint _force){
vel += _force;
}
//--------------------------------------------------------------
void particleSystem::addRepulsionForce(ofPoint _posOfForce, float radius, float strength){
ofPoint posOfForce = _posOfForce;
ofPoint direction;
float distance;
ofPoint force;
direction = posOfForce - pos;
distance = direction.length();
direction.normalize();
if((distance > 10) && (distance < radius)){
force = -1/ (distance * distance * distance ) * direction * strength;
acc += force;
}
}
//--------------------------------------------------------------
void particleSystem::addAttractionForce(ofPoint _posOfForce, float radius, float strength){
ofPoint posOfForce = _posOfForce;
ofPoint direction;
float distance;
ofPoint force;
direction = posOfForce - pos;
distance = direction.length();
direction.normalize();
if((distance > 50) && (distance < radius)){
force = 1/ (distance * distance ) * direction * strength;
acc += force;
}
}
};