[ Video ]
[ About ]
文字のParticleを線で結びました。Leap Motionから取得した指の座標からParticleが逃げます。
Escape from Index Finger.
[ 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 37 38 39 |
#pragma once #include "ofMain.h" #include "ofFbo.h" #include "Particle.h" // Leap Motion SDK #include "Leap.h" #pragma comment(lib, "Leap.lib") 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) {}; ofFbo fbo; ofTrueTypeFont font; vector<vector<Particle*>> particles; vector<ofColor> words_color; // Leap Motion Leap::Controller leap; }; |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include "ofApp.h" //-------------------------------------------------------------- ofApp::~ofApp() { for (int i = this->particles.size() - 1; i > -1; i--) { for (int j = this->particles[i].size() - 1; j > -1; j--) { delete this->particles[i][j]; this->particles[i].erase(this->particles[i].begin() + j); } } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(0); ofSetWindowTitle("Insta"); ofNoFill(); ofSetLineWidth(1.5); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); this->font.loadFont("fonts/Kazesawa-Bold.ttf", 130, true, false, true); vector<string> words; words.push_back("FINGER"); words.push_back("Escape"); ofColor body_color; for (int word_index = 0; word_index < words.size(); word_index++) { ofVec2f word_size = ofVec2f(this->font.stringWidth(words[word_index]), -this->font.stringHeight(words[word_index])); vector<ofTTFCharacter> char_paths = this->font.getStringAsPoints(words[word_index]); vector<Particle*> tmp_particles; for (int path_index = 0; path_index < char_paths.size(); path_index++) { vector<ofPolyline> outline = char_paths[path_index].getOutline(); for (int outline_index = 0; outline_index < outline.size(); outline_index++) { outline[outline_index] = outline[outline_index].getResampledBySpacing(5); tmp_particles.clear(); tmp_particles.shrink_to_fit(); vector<ofPoint> vertices = outline[outline_index].getVertices(); for (int vertices_index = 0; vertices_index < vertices.size(); vertices_index++) { ofVec2f location = vertices[vertices_index] - word_size / 2; location.y = location.y + 75 * (word_index - 1); tmp_particles.push_back(new Particle(location, ofColor(255))); } this->particles.push_back(tmp_particles); ofColor moji_color; moji_color.setHsb(ofRandom(255), 255, 255); this->words_color.push_back(moji_color); } } } this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { ofVec2f finger_point; float power; Leap::Frame frame = leap.frame(); for (Leap::Hand hand : frame.hands()) { for (Leap::Finger finger : hand.fingers()) { if (finger.type() == Leap::Finger::TYPE_INDEX) { int x = ofMap(finger.tipPosition().x, -300, 300, -ofGetWidth() / 2, ofGetWidth() / 2); int y = ofMap(finger.tipPosition().y, 0, 300, ofGetHeight() / 2, -ofGetHeight() / 2); power = ofMap(finger.tipPosition().z, 100, -100, 0, 100); finger_point = ofVec3f(x, y); } } } this->fbo.begin(); ofClear(0); ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2); ofDrawCircle(finger_point, 3); for (int i = 0; i < this->particles.size(); i++) { for (int j = 0; j < this->particles[i].size(); j++) { float distance = finger_point.distance(this->particles[i][j]->get_location()); if (distance < power) { this->particles[i][j]->seek_r(finger_point); this->particles[i][j]->seek(this->particles[i][j]->get_start_location()); } else { this->particles[i][j]->seek(this->particles[i][j]->get_start_location()); } this->particles[i][j]->update(); } } for (int i = 0; i < this->particles.size(); i++) { ofSetColor(this->words_color[i]); ofBeginShape(); for (int j = 0; j < this->particles[i].size(); j++) { //this->particles[i][j]->draw(); ofVertex(this->particles[i][j]->get_location().x, this->particles[i][j]->get_location().y); } ofEndShape(true); } this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); Particle(ofVec3f location, ofColor body_color); ~Particle(); void apply_force(ofVec3f force); void seek(ofVec3f target); void seek_r(ofVec3f target); void update(); void draw(); ofVec3f get_location(); ofVec3f get_start_location(); private: float size; ofVec3f location; ofVec3f start_location; ofVec3f velocity; ofVec3f acceleration; float max_force; float max_speed; float radius; 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 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include "Particle.h" Particle::Particle() : Particle(ofVec3f(0, 0, 0), ofColor(255)) { } Particle::Particle(ofVec3f location, ofColor body_color) { this->location = location; this->start_location = location; this->velocity = ofVec3f(); this->acceleration = ofVec3f(); this->size = 5; this->body_color = body_color; this->radius = radius; this->max_force = 100; this->max_speed = 6000; this->radius = 1; } Particle::~Particle() { } void Particle::apply_force(ofVec3f force) { this->acceleration += force; } void Particle::seek(ofVec3f target) { ofVec3f desired = target - this->location; float distance = desired.length(); desired.normalize(); if (distance < this->radius) { if (distance < 1) { this->location = this->start_location; return; } else { distance *= ofMap(distance, 0, this->radius, 0, this->max_speed); } } else { distance *= max_speed; } ofVec3f steer = desired - this->velocity; steer.limit(this->max_force); apply_force(steer); } void Particle::seek_r(ofVec3f target) { ofVec3f desired = target - this->location; desired.normalize(); desired *= this->max_speed * 0.5; ofVec3f steer = desired - this->velocity; steer.limit(this->max_force * 0.5); steer *= -1; apply_force(steer); } void Particle::update() { this->velocity += this->acceleration; //this->velocity.limit(this->max_speed); this->location += this->velocity; this->acceleration *= 0; this->velocity *= 0.98; } void Particle::draw() { ofSetColor(this->body_color); ofDrawCircle(this->location, this->size); } ofVec3f Particle::get_location() { return this->location; } ofVec3f Particle::get_start_location() { return this->start_location; } |