[ Video ]
[ About ]
Leap Motion を使って指の軌跡でParticle生成。
Particle created from finger velocity.
[ 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 |
#pragma once #include "ofMain.h" #include "Particle.h" #include "Cleator.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) {}; ofEasyCam cam; vector<Particle*> particles; // 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 |
#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); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(0); ofSetWindowTitle("Insta"); ofNoFill(); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); } //-------------------------------------------------------------- void ofApp::update() { Leap::Frame frame = leap.frame(); for (Leap::Hand hand : frame.hands()) { for (Leap::Finger finger : hand.fingers()) { ofVec2f velocity = ofVec2f(finger.tipVelocity().x, -finger.tipVelocity().y); if (velocity.length() > 250) { ofVec3f finger_position = ofVec3f(ofMap(finger.tipPosition().x, -300, 300, -ofGetWidth() / 2, ofGetWidth() / 2), ofMap(finger.tipPosition().y, 0, 300, -ofGetHeight() / 2, ofGetHeight() / 2), finger.tipPosition().z); this->particles.push_back(new Particle(finger_position, velocity * 0.008)); } } } 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); } } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofSetColor(255, 128); ofDrawBox(720); 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 < 80) { ofDrawLine(this->particles[i]->get_location(), this->particles[j]->get_location()); } } } this->cam.end(); } //-------------------------------------------------------------- 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); Particle(ofVec3f location, ofVec3f velocity); Particle(ofVec3f location, ofVec3f velocity, ofColor body_color); ~Particle(); void update(); void draw(); ofVec3f get_location(); bool isDead(); private: ofVec3f location; ofVec3f 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 37 38 39 40 |
#include "Particle.h" Particle::Particle() : Particle(ofVec3f(0, 0), ofVec3f(0, 0)) { } Particle::Particle(ofVec3f location, ofVec3f velocity){ this->location = location; this->velocity = velocity; this->alpha = 255; this->body_size = velocity.length() / 2; this->body_color.setHsb(ofRandom(255), 255, 255); } Particle::Particle(ofVec3f location, ofVec3f velocity, ofColor body_color) { this->location = location; this->velocity = velocity; this->alpha = 255; this->body_size = velocity.length() / 2; this->body_color = body_color; } Particle::~Particle() { } void Particle::update(){ this->location += this->velocity; this->alpha -= 3; } void Particle::draw(){ ofSetColor(this->body_color, this->alpha); ofDrawSphere(this->location, this->body_size); } ofVec3f Particle::get_location(){ return this->location; } bool Particle::isDead(){ return this->alpha < 0; } |