[ Video ]
[ About ]
ofxBox2dがopenFramworks0.10.0に、既に対応しているようだったので、新しい環境で使ってみました。ofxBox2dの環境構築については、Qiita記事を書きましたので、下記からご参照下さい。
openFrameworks0.10.0でofxBox2dを動かす(Windows)
https://qiita.com/junkiyoshi/items/68e602568a0c94840bec
今回は重力(x, y)を、それぞれ0として無重力にする代わりに、中央にフレーム番号と連動して変動する力場を作ってみました。Circle自身は移動する力を持っていませんが、力場から作用する力に引っ張られて飛び出していきます。
[ 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 |
#pragma once #include "ofMain.h" #include "ofxBox2d.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) {} ofxBox2d box2d; vector<pair<shared_ptr<ofxBox2dCircle>, ofColor>> circles; int force_field_radius; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(239); ofSetWindowTitle("Insta"); // Initialize ofxBox2d this->box2d.init(); this->box2d.setGravity(0, 0); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); // Add Circle for (int i = 0; i < 256; i++) { float radius = ofRandom(8.0, 15.0); auto circle = make_shared<ofxBox2dCircle>(); circle->setPhysics(1.0, 0.63, 0.1); circle->setup(this->box2d.getWorld(), ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), radius); ofColor color; color.setHsb(ofRandom(255), 239, 239); this->circles.push_back(make_pair(circle, color)); } this->force_field_radius = 200; } //-------------------------------------------------------------- void ofApp::update() { // Field of Force on Center for (int i = 0; i < this->circles.size(); i++) { shared_ptr<ofxBox2dCircle> circle = get<0>(this->circles[i]); ofPoint point = get<0>(this->circles[i])->getPosition(); float distance = point.distance(ofPoint(ofGetWidth() / 2, ofGetHeight() / 2)); if(distance < this->force_field_radius) { ofPoint p = ofPoint(this->force_field_radius * cos(ofGetFrameNum() * DEG_TO_RAD), this->force_field_radius * sin(ofGetFrameNum() * DEG_TO_RAD)); circle->addForce(p, ofMap(distance, 0, 200, 10, 1)); } } // Compute box2d this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { // Draw Filed of Force ofPushMatrix(); ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2); ofNoFill(); ofSetColor(39); ofSetLineWidth(0.5); ofBeginShape(); for (int deg = 0; deg < 360; deg += 1) { ofVertex(this->force_field_radius * cos(deg * DEG_TO_RAD), this->force_field_radius * sin(deg * DEG_TO_RAD)); } ofEndShape(true); ofFill(); ofBeginShape(); for (int deg = ofGetFrameNum(); deg < ofGetFrameNum() + 360; deg += 120) { ofVertex(this->force_field_radius * cos(deg * DEG_TO_RAD), this->force_field_radius * sin(deg * DEG_TO_RAD)); } ofEndShape(true); ofPopMatrix(); // Draw Circles ofSetLineWidth(3); for (int i = 0; i < this->circles.size(); i++) { ofSetColor(get<1>(this->circles[i])); float radius = get<0>(this->circles[i])->getRadius(); ofPoint point = get<0>(this->circles[i])->getPosition(); ofDrawCircle(point, radius); ofDrawLine(point, point - get<0>(this->circles[i])->getVelocity()); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |