#include "demoParticle.h"
demoParticle::demoParticle(){
attractPoints = NULL;
}
void demoParticle::setMode(particleMode newMode){
mode = newMode;
}
void demoParticle::setAttractPoints( vector <glm::vec3> * attract ){
attractPoints = attract;
}
void demoParticle::reset(){
uniqueVal = ofRandom(-10000, 10000);
pos.x = ofRandomWidth();
pos.y = ofRandomHeight();
pos.z = 0;
vel.x = ofRandom(-3.9, 3.9);
vel.y = ofRandom(-3.9, 3.9);
vel.z = 0;
frc = glm::vec3(0,0,0);
scale = ofRandom(0.5, 1.0);
if( mode == PARTICLE_MODE_NOISE ){
drag = ofRandom(0.97, 0.99);
vel.y = fabs(vel.y) * 3.0;
}else{
drag = ofRandom(0.95, 0.998);
}
}
void demoParticle::update(){
if( mode == PARTICLE_MODE_ATTRACT ){
glm::vec3 attractPt(ofGetMouseX(), ofGetMouseY(), 0);
frc = attractPt-pos;
frc = glm::normalize(frc);
vel *= drag;
vel += frc * 0.6;
}
else if( mode == PARTICLE_MODE_REPEL ){
glm::vec3 attractPt(ofGetMouseX(), ofGetMouseY(), 0);
frc = attractPt-pos;
float dist = glm::length(frc);
frc = glm::normalize(frc);
vel *= drag;
if( dist < 150 ){
vel += -frc * 0.6;
}else{
frc.x = ofSignedNoise(uniqueVal, pos.y * 0.01, ofGetElapsedTimef()*0.2);
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.01, ofGetElapsedTimef()*0.2);
vel += frc * 0.04;
}
}
else if( mode == PARTICLE_MODE_NOISE ){
float fakeWindX = ofSignedNoise(pos.x * 0.003, pos.y * 0.006, ofGetElapsedTimef() * 0.6);
frc.x = fakeWindX * 0.25 + ofSignedNoise(uniqueVal, pos.y * 0.04) * 0.6;
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.006, ofGetElapsedTimef()*0.2) * 0.09 + 0.18;
vel *= drag;
vel += frc * 0.4;
if( pos.y + vel.y > ofGetHeight() ){
pos.y -= ofGetHeight();
}
}
else if( mode == PARTICLE_MODE_NEAREST_POINTS ){
if( attractPoints ){
glm::vec3 closestPt;
int closest = -1;
float closestDist = 9999999;
for(unsigned int i = 0; i < attractPoints->size(); i++){
float lenSq = glm::length2( attractPoints->at(i)-pos );
if( lenSq < closestDist ){
closestDist = lenSq;
closest = i;
}
}
if( closest != -1 ){
closestPt = attractPoints->at(closest);
float dist = sqrt(closestDist);
frc = closestPt - pos;
vel *= drag;
if( dist < 300 && dist > 40 && !ofGetKeyPressed('f') ){
vel += frc * 0.003;
}else{
frc.x = ofSignedNoise(uniqueVal, pos.y * 0.01, ofGetElapsedTimef()*0.2);
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.01, ofGetElapsedTimef()*0.2);
vel += frc * 0.4;
}
}
}
}
pos += vel;
if( pos.x > ofGetWidth() ){
pos.x = ofGetWidth();
vel.x *= -1.0;
}else if( pos.x < 0 ){
pos.x = 0;
vel.x *= -1.0;
}
if( pos.y > ofGetHeight() ){
pos.y = ofGetHeight();
vel.y *= -1.0;
}
else if( pos.y < 0 ){
pos.y = 0;
vel.y *= -1.0;
}
}
void demoParticle::draw(){
if( mode == PARTICLE_MODE_ATTRACT ){
ofSetColor(255, 63, 180);
}
else if( mode == PARTICLE_MODE_REPEL ){
ofSetColor(208, 255, 63);
}
else if( mode == PARTICLE_MODE_NOISE ){
ofSetColor(99, 63, 255);
}
else if( mode == PARTICLE_MODE_NEAREST_POINTS ){
ofSetColor(103, 160, 237);
}
ofDrawCircle(pos.x, pos.y, scale * 4.0);
}
Comments