Particles with circular motion


testApp.h

#pragma once
#include "ofMain.h"
#include "Particle.h"

class testApp : public ofBaseApp {
public:
    void setup();
    void update();
    void draw();
    void createParticle();
    void keyPressed(int key);
       
    ofImage img;
    
    vector<Particle> particles;    
};

testApp.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
    img.loadImage("bambi2.jpg");
    ofSetBackgroundAuto(false);
    ofSetFrameRate(30);
    ofEnableAlphaBlending();
    ofBackground(0);
}

//--------------------------------------------------------------
void testApp::update(){
    ofSetWindowTitle(ofToString(ofGetFrameRate(),0));
    for (int i=0; i<particles.size(); i++) {
        particles[i].update();
    }
}

//--------------------------------------------------------------
void testApp::draw(){    
    ofSetColor(255);
            
    if (ofGetMousePressed()) {
        img.draw(0,0);
    }
    
    for (int i=0; i<particles.size(); i++) {
        Particle &p = particles[i];
        //p.color = img.getColor(p.pos.x,p.pos.y);
        p.draw();        
    }
        
    for (int i=0; i<50; i++) {
        createParticle();
    }
    
    while (particles.size()>1000) {
        particles.erase(particles.begin());
    }

}

void testApp::createParticle() {
    Particle p;
    p.setup();
    p.pos.set(ofRandomWidth(), ofRandomHeight());
    p.color = img.getColor(p.pos.x,p.pos.y);
    particles.push_back(p);
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    if (key=='s') ofSaveFrame();
    if (key=='q') ofEnableSmoothing();
    if (key=='Q') ofDisableSmoothing();
}

Particle.h

#include "ofMain.h"

class Particle {
public:

    void setup();
    void draw();
    void update();
    
    ofVec2f pos,vel,acc,oldpos;
    float angle;
    ofColor color;
    int age;
};

Particle.cpp

#include "Particle.h"

void Particle::setup() {
    vel.x = 2*ofRandomf();
    vel.y = 2*ofRandomf();
    angle = ofRandomf()*TWO_PI;
    age = 0;
}

void Particle::draw() {
    ofSetColor(color,ofMap(age,0,20,255,0,true));
    ofLine(oldpos,pos);
}

void Particle::update() {
    angle += ofSignedNoise(pos.x, pos.y)*TWO_PI;
    vel.rotate(angle);
    oldpos = pos;
    pos += vel;
    age++;
}

Comments