212 lines
5.1 KiB
C++
212 lines
5.1 KiB
C++
|
|
||
|
// particle.cpp
|
||
|
// particleSystem
|
||
|
//
|
||
|
|
||
|
#include "particle.h"
|
||
|
#include "ofApp.h"
|
||
|
|
||
|
Particle::Particle()
|
||
|
{
|
||
|
uniqueVal = ofRandom(-10000, 10000);
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
|
||
|
|
||
|
void Particle::setup(particleMode newMode) {
|
||
|
mode = newMode;
|
||
|
age = 0.0;
|
||
|
//maxLife = 30.0;
|
||
|
frc = ofVec2f(0, 0);
|
||
|
//size = ofRandom(4.0, 0.01);
|
||
|
size = .5;
|
||
|
maxLife = 0.99;
|
||
|
mass = ofRandom(100, 250);
|
||
|
if (mode == PARTICLE_MODE_DEFAULT) {
|
||
|
effect.loadFile("xml/default.xml");
|
||
|
int velMin = effect.getValue("default:velMin", 0);
|
||
|
int velMax = effect.getValue("default:velMax", 0);
|
||
|
int lifeMax = effect.getValue("default:maxLife", 0);
|
||
|
setVel(velMin, velMax);
|
||
|
pos.x = ofRandomWidth();
|
||
|
pos.y = ofRandomHeight();
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_RADIAL) {
|
||
|
effect.loadFile("xml/radial.xml");
|
||
|
int velMin = effect.getValue("radial:velMin", 0);
|
||
|
int velMax = effect.getValue("radial:velMax", 0);
|
||
|
setVel(velMin, velMax);
|
||
|
pos.x = ofGetWidth()/2;
|
||
|
pos.y = ofGetHeight()/2;
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_RAIN) {
|
||
|
effect.loadFile("xml/rain.xml");
|
||
|
vel.x = ofRandom(-40, 40);
|
||
|
vel.y = ofRandom(-40, 40);
|
||
|
pos.x = ofRandomWidth();
|
||
|
pos.y = ofRandomHeight();
|
||
|
drag = ofRandom(0.97, 0.99);
|
||
|
vel.y = fabs(vel.y) * 6.0;
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_ATTRACTOR) {
|
||
|
effect.loadFile("xml/rain.xml");
|
||
|
vel.x = ofRandom(-40, 40);
|
||
|
vel.y = ofRandom(-40, 40);
|
||
|
pos.x = ofRandomWidth();
|
||
|
pos.y = ofRandomHeight();
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_DETRACTOR) {
|
||
|
effect.loadFile("xml/rain.xml");
|
||
|
vel.x = ofRandom(-40, 40);
|
||
|
vel.y = ofRandom(-40, 40);
|
||
|
pos.x = ofRandomWidth();
|
||
|
pos.y = ofRandomHeight();
|
||
|
}
|
||
|
else {
|
||
|
|
||
|
drag = ofRandom(0.95, 0.998);
|
||
|
}
|
||
|
|
||
|
tex.load("img/br.png");
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
|
||
|
void Particle::update(float deltaT, vector<Attractor*>* attractors,vector<Particle*> system) {
|
||
|
if (mode == PARTICLE_MODE_DEFAULT) {
|
||
|
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_RAIN) {
|
||
|
float wind = ofSignedNoise(pos.x * 0.003, pos.y * 0.0006, ofGetElapsedTimef() * 0.6);
|
||
|
|
||
|
frc.x = wind * 0.25 + ofSignedNoise(uniqueVal, pos.y * 0.08) * 0.6;
|
||
|
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.06, ofGetElapsedTimef()*0.2) * 0.09 + 0.18;
|
||
|
|
||
|
vel *= drag;
|
||
|
vel += frc * 0.2;
|
||
|
|
||
|
if (pos.y + vel.y > ofGetHeight()) {
|
||
|
pos.y -= ofGetHeight();
|
||
|
}
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_ATTRACTOR) {
|
||
|
|
||
|
ofVec2f force;
|
||
|
counterOfActiveAttractors = 0;
|
||
|
for (int i = 0; i < attractors->size(); i++) {
|
||
|
if (attractors->at(i)->getX() == 0 && attractors->at(i)->getY() == 0) {
|
||
|
vel += force;
|
||
|
vel = mass * vel.getNormalized();
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
counterOfActiveAttractors++;
|
||
|
force.set(attractors->at(i)->getX() - pos.x, attractors->at(i)->getY() - pos.y);
|
||
|
|
||
|
if (force.length() < 250) {
|
||
|
force = 40 * force.getNormalized();
|
||
|
size = size + ofRandom(.03, 0.01);
|
||
|
vel += force;
|
||
|
vel = mass * vel.getNormalized();
|
||
|
}
|
||
|
else {
|
||
|
force = 0 * force.getNormalized();
|
||
|
vel += force;
|
||
|
vel = mass * vel.getNormalized();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if (mode == PARTICLE_MODE_DETRACTOR) {
|
||
|
ofVec2f force;
|
||
|
counterOfActiveAttractors = 0;
|
||
|
for (int i = 0; i < attractors->size(); i++) {
|
||
|
if (attractors->at(i)->getX() == 0 && attractors->at(i)->getY() == 0) {
|
||
|
//force = 0 * force.getNormalized();
|
||
|
vel += force;
|
||
|
vel = mass * vel.getNormalized();
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
counterOfActiveAttractors++;
|
||
|
force.set(attractors->at(i)->getX() - pos.x, attractors->at(i)->getY() - pos.y);
|
||
|
|
||
|
if (force.length() < 600) {
|
||
|
force.normalize();
|
||
|
size = size + ofRandom(.01, 0.01);
|
||
|
|
||
|
vel += -force*2000;
|
||
|
vel = mass * vel.getNormalized();
|
||
|
}
|
||
|
else {
|
||
|
force = 0 * force.getNormalized();
|
||
|
vel += force;
|
||
|
vel = mass * vel.getNormalized();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//Update particle pos
|
||
|
age += deltaT* .5;
|
||
|
pos += (vel * deltaT);
|
||
|
|
||
|
// *** COLLISION *** COLLISION *** COLLISION *** COLLISION *** COLLISION *** COLLISION
|
||
|
|
||
|
//if (position.x <= 0 || position.x >= ofGetWidth()) {
|
||
|
//vel.x = -vel.x;
|
||
|
//position += (vel * deltaT);
|
||
|
//}
|
||
|
//else if (position.y <= 0 || position.y >= ofGetHeight()) {
|
||
|
//vel.y = -vel.y;
|
||
|
//position += (vel * deltaT);
|
||
|
//}
|
||
|
//else {
|
||
|
//position += (vel * deltaT);
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
void Particle::draw() {
|
||
|
|
||
|
color.set(getAgeNorm() * 241,241/ getAgeNorm() ,219);
|
||
|
|
||
|
ofSetColor(color, (1 - getAgeNorm()) * 255);
|
||
|
if (mode == PARTICLE_MODE_BRUNIG) {
|
||
|
tex.setColor(color);
|
||
|
tex.draw(pos.x, pos.y, size);
|
||
|
}
|
||
|
else {
|
||
|
ofDrawCircle(pos.x, pos.y, size);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------
|
||
|
void Particle::setMode(particleMode newMode) {
|
||
|
mode = newMode;
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
float Particle::getAgeNorm() {
|
||
|
return age / maxLife;
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
void Particle::setVel(float min, float max) {
|
||
|
vel.x = ofRandom(min,max);
|
||
|
vel.y = ofRandom(min,max);
|
||
|
}
|
||
|
|
||
|
float Particle::getMaxLife() {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
float Particle::getY() {
|
||
|
return pos.y;
|
||
|
}
|
||
|
|
||
|
|
||
|
float Particle::getX() {
|
||
|
return pos.x;
|
||
|
}
|
||
|
|
||
|
|