[ Video ]
[ About ]
Box2dを使って物理演算をしています。
内部では文字列の大きさに合わせて四角を生成しています。
[ 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 |
#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) {}; ofTrueTypeFont font; std::string moji; ofxBox2d box2d; vector<shared_ptr<ofxBox2dRect>> rects; ofxBox2dEdge eage; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(255); ofSetWindowTitle("Insta"); ofSetColor(0); ofSetRectMode(ofRectMode::OF_RECTMODE_CENTER); this->font.loadFont("fonts/Kazesawa-Bold.ttf", 50); this->moji = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?"; this->box2d.init(); this->box2d.setGravity(0, 10); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); for (int i = 0; i < this->moji.size(); i++) { this->rects.push_back(shared_ptr<ofxBox2dRect>(new ofxBox2dRect)); this->rects.back().get()->setPhysics(3.0, 0.2, 0.1); this->rects.back().get()->setup(this->box2d.getWorld(), ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), this->font.stringWidth(this->moji.substr(i, 1)), this->font.stringHeight(this->moji.substr(i, 1))); } this->eage = ofxBox2dEdge(); this->eage.addVertex(ofVec2f(200, ofGetHeight() / 2)); this->eage.addVertex(ofVec2f(ofGetWidth() - 200, ofGetHeight() / 2)); this->eage.create(this->box2d.getWorld()); } //-------------------------------------------------------------- void ofApp::update() { this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { for (int i = 0; i < this->moji.size(); i++) { ofPushMatrix(); ofTranslate(this->rects[i].get()->getPosition().x, this->rects[i].get()->getPosition().y); ofRotate(this->rects[i].get()->getRotation()); this->font.drawString((this->moji.substr(i, 1)), -this->font.stringWidth(this->moji.substr(i, 1)) / 2, this->font.stringHeight(this->moji.substr(i, 1)) / 2); ofPopMatrix(); } this->eage.draw(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |