[ Video ]
[ About ]
Leap Motionと連携しました。Leap::HandのgrabStrengthメソッドで取得した値によって吸引・反発を切り替えています。じゃんけんでいう、グーが吸引、パーが反発になっています。思ったより、グーで持ち上げてる感じや、パーで弾く感じが出て良かった。
両手で触るのが楽しかったので、いつもLeap Motionの撮影はスマートフォンでしていましたが、絵の中に手を描写してキャプチャしました。Unityなんかを使うともっと手っぽいポリゴン?な表現を使えるようなので、いつかマネしてみたいですね。
本当はSphereを小さくして数をもう少し増やしたかったのですが、Surface Pro 4だとこの辺が限界ですね。グラフィック関係を中心に開発環境もグレードアップしてみたいです。お小遣いが許さないですが…(涙
[ 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 40 41 |
#pragma once #include "ofMain.h" #include "ofxBullet.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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofEasyCam cam; ofLight light; ofxBulletWorldRigid world; vector < ofxBulletSphere*> spheres; vector<ofColor> spheres_color; vector<ofVec3f> spheres_location; // Leap Motion Leap::Controller leap; ofVec3f hand_position; void drawHand(Leap::Hand hand); void drawFinger(Leap::Finger finger); }; |
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 129 130 131 132 133 134 135 136 137 138 139 140 |
#include "ofApp.h" //-------------------------------------------------------------- ofApp::~ofApp() { for (ofxBulletSphere* tmp : this->spheres) { delete tmp; } this->spheres.clear(); } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofEnableDepthTest(); ofBackground(255); ofSetWindowTitle("Insta"); this->world.setup(); this->world.setGravity(ofVec3f(0.0, 0.0, 0.0)); float size = 30; for (float x = -150; x < 150; x += size) { for (float y = -150; y < 150; y += size) { for (float z = -150; z < 150; z += size) { ofxBulletSphere* sphere = new ofxBulletSphere(); sphere->create(this->world.world, ofVec3f(x, y, z), 1.0, size / 2); sphere->setRestitution(1.0); sphere->add(); this->spheres.push_back(sphere); ofColor color; color.setHsb(ofRandom(255), 255, 255); this->spheres_color.push_back(color); this->spheres_location.push_back(ofVec3f(x, y, z)); } } } this->light.setPosition(ofVec3f(0, 0, 512)); ofEnableLighting(); this->light.enable(); } //-------------------------------------------------------------- void ofApp::update() { ofVec3f diff; for (int i = 0; i < this->spheres.size(); i++) { diff = this->spheres_location[i] - this->spheres[i]->getPosition(); diff *= 10; this->spheres[i]->applyCentralForce(diff); } Leap::Frame frame = this->leap.frame(); for (Leap::Hand hand : frame.hands()) { this->hand_position = ofVec3f(hand.palmPosition().x, hand.palmPosition().y, hand.palmPosition().z); ofVec3f diff; for (int i = 0; i < this->spheres.size(); i++) { diff = this->hand_position - this->spheres[i]->getPosition(); if (diff.length() < 150) { if (hand.grabStrength() > 0.5) { diff *= ofMap(diff.length(), 0, 300, 300, 0); } else { diff *= ofMap(diff.length(), 0, 150, -1024, 0); } this->spheres[i]->applyCentralForce(diff); } } } this->world.update(); } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); Leap::Frame frame = this->leap.frame(); for (Leap::Hand hand : frame.hands()) { this->drawHand(hand); } float len = 50; for (int i = 0; i < this->spheres.size(); i++) { ofSetColor(this->spheres_color[i]); this->spheres[i]->draw(); } this->cam.end(); } //-------------------------------------------------------------- void ofApp::drawHand(Leap::Hand hand) { if (hand.grabStrength() < 0.5) { ofSetColor(255, 0, 0); } else { ofSetColor(0, 0, 255); } Leap::FingerList fingers = hand.fingers(); for (int j = 0; j < fingers.count(); j++) { this->drawFinger(fingers[j]); } ofPushMatrix(); ofVec3f palm_point = ofVec3f(hand.palmPosition().x, hand.palmPosition().y, hand.palmPosition().z); ofTranslate(palm_point); ofSphere(10); ofPopMatrix(); } //-------------------------------------------------------------- void ofApp::drawFinger(Leap::Finger finger) { ofVec3f tip_point = ofVec3f(finger.tipPosition().x, finger.tipPosition().y, finger.tipPosition().z); ofPushMatrix(); ofTranslate(tip_point); ofSphere(5); ofPopMatrix(); ofVec3f base_point = ofVec3f(tip_point.x + finger.direction().x * finger.length(),// * -1, tip_point.y + finger.direction().y * finger.length() - 1, tip_point.z + finger.direction().z * finger.length() - 1); ofPushMatrix(); ofTranslate(base_point); ofSphere(5); ofPopMatrix(); ofLine(tip_point, base_point); } //======================================================================== int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |