[ Video ]
[ About ]
ランダムウォークに寿命を付加。寿命と透明度がリンクしており、寿命を迎えるとウォーカーは消えます
Random walk add Lifespan.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#pragma once #include "ofMain.h" #include "Particle.h" class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; vector<Particle> particles; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(39); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); ofSetLineWidth(1.5); for (int i = 0; i < 10; i++) { this->particles.push_back(Particle()); } } //-------------------------------------------------------------- void ofApp::update() { if (ofRandom(100) < 25) { this->particles.push_back(Particle()); } for (int i = this->particles.size() - 1; i >= 0; i--) { if (this->particles[i].IsDead()) { this->particles.erase(this->particles.begin() + i); } } for (Particle& particle : particles) { particle.Upate(); } } //-------------------------------------------------------------- void ofApp::draw() { for (Particle& particle : particles) { particle.Draw(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include "Particle.h" Particle::Particle() { int x = (int)(ofRandom(ofGetWidth()) / 40) * 40; int y = (int)(ofRandom(ofGetHeight()) / 40) * 40; this->location = ofPoint(x, y); this->directions[0] = ofPoint(1, 0); this->directions[1] = ofPoint(-1, 0); this->directions[2] = ofPoint(0, 1); this->directions[3] = ofPoint(0, -1); this->color.setHsb(ofRandom(255), 230, 255); this->life = 255; } Particle::~Particle() { } void Particle::Upate() { if (ofGetFrameNum() % 40 == 0) { int r = ofRandom(4); this->direction = this->directions[r]; if (this->location.x < 0) { this->direction = ofPoint(1, 0); } else if (this->location.x > ofGetWidth()) { this->direction = ofPoint(-1, 0); } if (this->location.y < 0) { this->direction = ofPoint(0, 1); } else if (this->location.y > ofGetHeight()) { this->direction = ofPoint(0, -1); } } this->location += this->direction * 2; this->logs.push_front(location); while (this->logs.size() > 60) { this->logs.pop_back(); } this->life -= 1; } void Particle::Draw() { ofSetColor(this->color, this->life); ofFill(); ofDrawCircle(this->location, 8); ofNoFill(); ofBeginShape(); for (ofPoint& log : this->logs) { ofVertex(log); } ofEndShape(); } bool Particle::IsDead() { return this->life < 0 ? true : false; } |