#include “testApp.h”
//--------------------------------------------------------------
void testApp::setup(){
ofSetVerticalSync(true);
num = 10;
p.assign(num, demoParticle());
currentMode = PARTICLE_MODE_ATTRACT;
currentModeStr = "1 - PARTICLE_MODE_ATTRACT: attracts to mouse";
resetParticles();
}
//--------------------------------------------------------------
void testApp::resetParticles(){
//these are the attraction points used in the forth demo
attractPoints.clear();
for(int i = 0; i < 4; i++){ // Push Back adds elements to the arry
attractPoints.push_back( ofPoint( ofMap(i, 0, 4, 100, ofGetWidth()-100) , ofRandom(100, ofGetHeight()-100) ) );
}
attractPointsWithMovement = attractPoints;
for(unsigned int i = 0; i < num; i++){
p[i].setMode(currentMode);
p[i].setAttractPoints(&attractPointsWithMovement);;
p[i].reset(ofPoint(1,0,0));
}
}
//--------------------------------------------------------------
void testApp::update(){
for(unsigned int i = 0; i < p.size(); i++){
p[i].setMode(currentMode);
p[i].update();
}
//lets add a bit of movement to the attract points
for(unsigned int i = 0; i < attractPointsWithMovement.size(); i++){
attractPointsWithMovement[i].x = attractPoints[i].x + ofSignedNoise(i * 10, ofGetElapsedTimef() * 0.7) * 12.0;
attractPointsWithMovement[i].y = attractPoints[i].y + ofSignedNoise(i * -10, ofGetElapsedTimef() * 0.7) * 12.0;
}
}
//--------------------------------------------------------------
void testApp::draw(){
ofBackgroundGradient(ofColor(60,60,60), ofColor(10,10,10));
for(unsigned int i = 0; i < p.size(); i++){
p[i].draw();
}
ofSetColor(190);
if( currentMode == PARTICLE_MODE_NEAREST_POINTS ){
for(unsigned int i = 0; i < attractPoints.size(); i++){
ofNoFill();
ofCircle(attractPointsWithMovement[i], 10);
ofFill();
ofCircle(attractPointsWithMovement[i], 4);
}
}
ofSetColor(230);
ofDrawBitmapString(currentModeStr + "\n\nSpacebar to reset. \nKeys 1-4 to change mode.", 10, 20);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if( key == ‘1’){
currentMode = PARTICLE_MODE_ATTRACT;
currentModeStr = “1 - PARTICLE_MODE_ATTRACT: attracts to mouse”;
updateParticles(1);
}
if( key == '2'){
currentMode = PARTICLE_MODE_REPEL;
currentModeStr = "2 - PARTICLE_MODE_REPEL: repels from mouse";
}
if( key == '3'){
currentMode = PARTICLE_MODE_NEAREST_POINTS;
currentModeStr = "3 - PARTICLE_MODE_NEAREST_POINTS: hold 'f' to disable force";
}
if( key == '4'){
currentMode = PARTICLE_MODE_NOISE;
currentModeStr = "4 - PARTICLE_MODE_NOISE: snow particle simulation";
//resetParticles();
}
if( key == ' ' ){
updateParticles(2);
//resetParticles();
}
}
void testApp::updateParticles(float _n)
{
num += _n;
}