123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
-
- // 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;
- }
-
|