[ Video ]
[ About ]
時間の経過で色を変化。
The color changes with the lapse of time.
[ 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 26 27 28 29 30 |
#pragma once #include "ofMain.h" #include "Particle.h" #include "Cleator.h" class ofApp : public ofBaseApp { public: ~ofApp(); 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; vector<Cleator*> cleators; }; |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#include "ofApp.h" //-------------------------------------------------------------- ofApp::~ofApp() { for (int i = this->particles.size() - 1; i > -1; i--) { delete this->particles[i]; this->particles.erase(this->particles.begin() + i); } for (int i = this->cleators.size() - 1; i > -1; i--) { delete this->cleators[i]; this->cleators.erase(this->cleators.begin() + i); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(0); ofSetWindowTitle("Insta"); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); for (int i = 0; i < 3; i++) { this->cleators.push_back(new Cleator()); } } //-------------------------------------------------------------- void ofApp::update() { ofColor body_color; for (int i = 0; i < this->cleators.size(); i++) { ofVec2f location = this->cleators[i]->get_location(); ofVec2f velocity = this->cleators[i]->get_velocity(); if (velocity.length() > 5) { body_color.setHsb((ofGetFrameNum() + i * 90) % 255, 255, 255); this->particles.push_back(new Particle(location, velocity * 0.3, body_color)); } } if (ofGetMousePressed()) { ofVec2f location = ofVec2f(ofGetMouseX() - ofGetWidth() / 2, ofGetMouseY() - ofGetHeight() / 2); ofVec2f prev_location = ofVec2f(ofGetPreviousMouseX() - ofGetWidth() / 2, ofGetPreviousMouseY() - ofGetHeight() / 2); ofVec2f velocity = prev_location - location; if (velocity.length() > 5) { body_color.setHsb(ofRandom(255), 255, 255); this->particles.push_back(new Particle(location, velocity * 0.3, body_color)); } } for (int i = this->particles.size() - 1; i > -1; i--) { this->particles[i]->update(); if (this->particles[i]->isDead()) { delete this->particles[i]; this->particles.erase(this->particles.begin() + i); } } for (int i = 0; i < this->cleators.size(); i++) { this->cleators[i]->update(); } } //-------------------------------------------------------------- void ofApp::draw() { ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2); float distance; for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->draw(); for (int j = i; j < this->particles.size(); j++) { distance = this->particles[i]->get_location().distance(this->particles[j]->get_location()); if (distance < 100) { ofDrawLine(this->particles[i]->get_location(), this->particles[j]->get_location()); } } } for (int i = 0; i < this->cleators.size(); i++) { this->cleators[i]->draw(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1080, 1080, 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); Particle(ofVec2f location, ofVec2f velocity, ofColor body_color); ~Particle(); void update(); void draw(); ofVec2f get_location(); bool isDead(); private: ofVec2f location; ofVec2f velocity; float body_size; ofColor body_color; int alpha; }; |
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 |
#include "Particle.h" Particle::Particle() : Particle(ofVec2f(0, 0), ofVec2f(0, 0), ofColor(128)) { } Particle::Particle(ofVec2f location, ofVec2f velocity, ofColor body_color) { this->location = location; this->velocity = velocity; this->alpha = 255; this->body_size = velocity.length() / 2; //this->body_color.setHsb(ofRandom(255), 255, 255); this->body_color = body_color; } Particle::~Particle() { } void Particle::update() { this->location += this->velocity; this->alpha -= 1; } void Particle::draw() { ofSetColor(this->body_color, this->alpha); ofDrawEllipse(this->location, this->body_size, this->body_size); } ofVec2f Particle::get_location() { return this->location; } bool Particle::isDead() { return this->alpha < 0; } |
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 |
#pragma once #include "ofMain.h" class Cleator { public: Cleator(); ~Cleator(); void applyForce(ofVec2f force); void seek(ofVec2f target); void update(); void draw(); ofVec2f get_location(); ofVec2f get_velocity(); private: ofVec2f location; ofVec2f velocity; ofVec2f acceleration; ofVec2f future; ofVec2f target; float max_speed; float max_force; float radius; float angle; }; |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include "Cleator.h" Cleator::Cleator() { this->location = ofVec2f(ofRandom(-1, 1), ofRandom(-1, 1)); this->velocity = ofVec2f(0, 0); this->acceleration = ofVec2f(0, 0); this->max_speed = 10; this->max_force = 3; this->radius = 30; this->angle = ofRandom(360); } Cleator::~Cleator() { } void Cleator::applyForce(ofVec2f force) { this->acceleration += force; } void Cleator::seek(ofVec2f target) { ofVec2f desired = this->location - target; float distance = desired.length(); desired.normalize(); if (distance < this->radius) { float m = ofMap(distance, 0, this->radius, 0, this->max_speed); desired *= m; } else { desired *= this->max_speed; } ofVec2f steer = this->velocity - desired; steer.limit(this->max_force); applyForce(steer); } void Cleator::update() { this->future.set(this->velocity); this->future.normalize(); this->future *= 60; this->future += this->location; this->angle = ofRandom(0, 360); float x = 60 * cos(this->angle * DEG_TO_RAD) + future.x; float y = 60 * sin(this->angle * DEG_TO_RAD) + future.y; this->target = ofVec2f(x, y); this->seek(this->target); this->velocity += this->acceleration; this->velocity.limit(max_speed); this->location += this->velocity; if (this->location.x < -ofGetWidth() / 2) { this->location.x = -ofGetWidth() / 2; this->velocity.x *= -1; } else if (this->location.x > ofGetWidth() / 2) { this->location.x = ofGetWidth() / 2; this->velocity.x *= -1; } if (this->location.y < -ofGetHeight() / 2) { this->location.y = -ofGetHeight() / 2; this->velocity.y *= -1; } else if (this->location.y > ofGetHeight() / 2) { this->location.y = ofGetHeight() / 2; this->velocity.y *= -1; } this->acceleration *= 0; this->velocity *= 0.98; } void Cleator::draw() { } ofVec2f Cleator::get_location() { return this->location; } ofVec2f Cleator::get_velocity() { return this->velocity; } |