[ Video ]
[ About ]
Particleの位置関係(お互いに近いParticleが3つ以上あった場合)を元にofxBox2dのPolygonを生成しています。
ポロポロと生成されては消えていくのを眺めていると癒されます。OpenCVのフレーム間のギャップをオブジェクトに置き換えても面白そうかな。
[ 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 31 32 33 34 35 36 |
#pragma once #include "ofMain.h" #include "ofFbo.h" #include "ofxBox2d.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) {}; ofFbo fbo; vector<unique_ptr<Particle>> particles; ofxBox2d box2d; vector<shared_ptr<ofxBox2dPolygon>> polygons; vector<float> polygons_life; vector<ofColor> polygons_color; }; |
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 98 99 100 101 102 103 104 105 106 107 108 109 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(255); ofSetWindowTitle("Insta"); for (int i = 0; i < 32 + 12; i++) { unique_ptr<Particle> particle(new Particle()); this->particles.push_back(std::move(particle)); } this->box2d.init(); this->box2d.setGravity(0, 50); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { for (int i = this->polygons.size() - 1; i >= 0; i--) { this->polygons_life[i] -= 1; if (this->polygons_life[i] < 20) { this->polygons[i].get()->destroy(); this->polygons.erase(this->polygons.begin() + i); this->polygons_life.erase(this->polygons_life.begin() + i); this->polygons_color.erase(this->polygons_color.begin() + i); } } this->fbo.begin(); ofClear(0); for(int i = 0; i < this->particles.size(); i++){ this->particles[i]->update(); vector<ofVec2f> near_points; for (int j = i; j < this->particles.size(); j++) { float distance = this->particles[i]->get_location().distance(this->particles[j]->get_location()); if (distance < 50) { ofSetColor(this->particles[i]->get_body_color(), 128); ofDrawLine(this->particles[i]->get_location(), this->particles[j]->get_location()); if (distance < 35) { near_points.push_back(this->particles[j]->get_location()); } } } if (near_points.size() >= 3) { shared_ptr<ofxBox2dPolygon> polygon = shared_ptr<ofxBox2dPolygon>(new ofxBox2dPolygon); for (int n_index = 0; n_index < near_points.size(); n_index++) { polygon.get()->addVertex(near_points[n_index]); } polygon.get()->setPhysics(1.0, 0.7, 0.3); polygon.get()->create(box2d.getWorld()); this->polygons.push_back(polygon); this->polygons_life.push_back(255); ofColor c; c.setHsb(ofRandom(255), 255, 255); this->polygons_color.push_back(c); } } this->box2d.update(); for (int i = 0; i < this->polygons.size(); i++) { ofSetColor(this->polygons_color[i], this->polygons_life[i]); this->polygons[i].get()->draw(); } for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->draw(); } this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1280, 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle() {}; void apply_force(ofVec2f force); void update(); void draw(); void set_color(ofColor body_color); ofVec2f get_location(); ofColor get_body_color(); private: ofVec2f location; ofVec2f velocity; ofVec2f acceleration; ofColor body_color; }; |
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 |
#include "Particle.h" Particle::Particle() { this->location = ofVec2f(ofRandom(ofGetWidth()), ofRandom(ofGetHeight())); this->velocity = ofVec2f(ofRandom(-3, 3), ofRandom(-3, 3)); this->velocity = this->velocity.normalized() * ofRandom(3, 5); this->acceleration = ofVec2f(); //this->body_color.setHsb(ofRandom(255), 230, 230); this->body_color = ofColor(0); } void Particle::apply_force(ofVec2f force) { this->acceleration += force; } void Particle::update() { this->acceleration += this->velocity; this->location += this->acceleration; this->acceleration *= 0; if (this->location.x < 0) { this->location.x = 0; this->velocity.x *= -1; } else if (this->location.x > ofGetWidth()) { this->location.x = ofGetWidth(); this->velocity.x *= -1; } if (this->location.y < 0) { this->location.y = 0; this->velocity.y *= -1; } else if (this->location.y > ofGetHeight() / 2) { this->location.y = ofGetHeight() / 2; this->velocity.y *= -1; } } void Particle::draw() { ofSetColor(this->body_color); ofDrawCircle(this->location, 3); } void Particle::set_color(ofColor body_color) { this->body_color = body_color; } ofVec2f Particle::get_location() { return this->location; } ofColor Particle::get_body_color() { return this->body_color; } |