239 lines
6.3 KiB
C++
Raw Normal View History

2019-04-10 21:08:52 +00:00
// particle.cpp
// particleSystem
//
#include "particle.h"
#include "ofApp.h"
Particle::Particle()
{
2019-05-23 15:52:48 +00:00
uniqueVal = ofRandom(-10000, 10000);
2019-04-10 21:08:52 +00:00
}
//--------------------------------------------------------------------------------------
2019-05-23 15:52:48 +00:00
void Particle::setup(particleMode newMode) {
mode = newMode;
2019-04-10 21:08:52 +00:00
age = 0.0;
2019-05-23 15:52:48 +00:00
maxLife = 10.0;
frc = ofVec2f(0, 0);
size = ofRandom(1, 1);
size = .5;
2019-04-10 21:08:52 +00:00
mass = ofRandom(100, 250);
2019-05-23 15:52:48 +00:00
//setVel(-40, 40);
if (mode == PARTICLE_MODE_DEFAULT) {
effect.loadFile("xml/default.xml");
int velMin = effect.getValue("default:velMin", 0);
int velMax = effect.getValue("default:velMax", 0);
setVel(velMin, velMax);
int lifeMax = effect.getValue("default:maxLife", 0);
maxLife = lifeMax;
int sizeMin = effect.getValue("default:sizeMin", 0);
int sizeMax = effect.getValue("default:sizeMax", 0);
size = ofRandom(sizeMin, sizeMax);
pos.x = ofRandomWidth();
pos.y = ofRandomHeight();
}
else if (mode == PARTICLE_MODE_RADIAL) {
effect.loadFile("xml/radial.xml");
int velMin = effect.getValue("default:velMin", 0);
int velMax = effect.getValue("default:velMax", 0);
setVel(velMin, velMax);
int lifeMax = effect.getValue("default:maxLife", 0);
maxLife = lifeMax;
int sizeMin = effect.getValue("default:sizeMin", 0);
int sizeMax = effect.getValue("default:sizeMax", 0);
size = ofRandom(sizeMin, sizeMax);
pos.x = ofGetWidth()/2;
pos.y = ofGetHeight()/2;
}
else if (mode == PARTICLE_MODE_RAIN) {
effect.loadFile("xml/rain.xml");
int velMin = effect.getValue("default:velMin", 0);
int velMax = effect.getValue("default:velMax", 0);
setVel(velMin, velMax);
int lifeMax = effect.getValue("default:maxLife", 0);
maxLife = lifeMax;
int sizeMin = effect.getValue("default:sizeMin", 0);
int sizeMax = effect.getValue("default:sizeMax", 0);
size = ofRandom(sizeMin, sizeMax);
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/attractor.xml");
int velMin = effect.getValue("default:velMin", 0);
int velMax = effect.getValue("default:velMax", 0);
setVel(velMin, velMax);
int lifeMax = effect.getValue("default:maxLife", 0);
maxLife = lifeMax;
int sizeMin = effect.getValue("default:sizeMin", 0);
int sizeMax = effect.getValue("default:sizeMax", 0);
size = ofRandom(sizeMin, sizeMax);
pos.x = ofRandomWidth();
pos.y = ofRandomHeight();
}
else if (mode == PARTICLE_MODE_DETRACTOR) {
effect.loadFile("xml/detractor.xml");
int velMin = effect.getValue("default:velMin", 0);
int velMax = effect.getValue("default:velMax", 0);
setVel(velMin, velMax);
int lifeMax = effect.getValue("default:maxLife", 0);
maxLife = lifeMax;
int sizeMin = effect.getValue("default:sizeMin", 0);
int sizeMax = effect.getValue("default:sizeMax", 0);
size = ofRandom(sizeMin, sizeMax);
pos.x = ofRandomWidth();
pos.y = ofRandomHeight();
}
else {
2019-04-10 21:08:52 +00:00
2019-05-23 15:52:48 +00:00
drag = ofRandom(0.95, 0.998);
}
2019-04-10 21:08:52 +00:00
2019-05-23 15:52:48 +00:00
tex.load("img/br.png");
2019-04-10 21:08:52 +00:00
}
//--------------------------------------------------------------------------------------
2019-05-23 15:52:48 +00:00
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.006, ofGetElapsedTimef() * 0.6);
2019-04-10 21:08:52 +00:00
2019-05-23 15:52:48 +00:00
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;
2019-04-10 21:08:52 +00:00
2019-05-23 15:52:48 +00:00
vel *= drag;
vel += frc * 0.2;
2019-04-10 21:08:52 +00:00
2019-05-23 15:52:48 +00:00
if (pos.y + vel.y > ofGetHeight()) {
pos.y -= ofGetHeight();
}
2019-04-10 21:08:52 +00:00
}
2019-05-23 15:52:48 +00:00
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();
}
2019-04-10 21:08:52 +00:00
}
}
2019-05-23 15:52:48 +00:00
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
2019-04-10 21:08:52 +00:00
age += deltaT * .5;
2019-05-23 15:52:48 +00:00
pos += (vel * deltaT);
2019-04-10 21:08:52 +00:00
// *** 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);
2019-05-23 15:52:48 +00:00
if (mode == PARTICLE_MODE_BRUNIG) {
tex.setColor(color);
tex.draw(pos.x, pos.y, size);
}
else {
ofDrawCircle(pos.x, pos.y, size);
}
}
2019-04-10 21:08:52 +00:00
2019-05-23 15:52:48 +00:00
//------------------------------------------------------------------
void Particle::setMode(particleMode newMode) {
mode = newMode;
2019-04-10 21:08:52 +00:00
}
//--------------------------------------------------------------------------------------
float Particle::getAgeNorm() {
return age / maxLife;
}
//--------------------------------------------------------------------------------------
2019-05-23 15:52:48 +00:00
void Particle::setVel(float min, float max) {
vel.x = ofRandom(min,max);
vel.y = ofRandom(min,max);
}
2019-04-10 21:08:52 +00:00
float Particle::getMaxLife() {
return 1;
}
2019-05-23 15:52:48 +00:00
float Particle::getY() {
return pos.y;
}
float Particle::getX() {
return pos.x;
}
2019-04-10 21:08:52 +00:00