[ Video ]
[ About ]
文字から抜き出したPath情報を元にofxBox2dで物理演算をしています。Key入力を元に文字が生成されているので、リアルタイムに入力した文字が崩れていきます。
昔に一度、作りたいと思いながらもPath情報の取得方法が分からずに諦めていたのですが、ついに念願が叶いました。出来なった事が出来るようになるのは嬉しいですね。
[ 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 | #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; 	ofxBox2d box2d; 	vector<shared_ptr<ofxBox2dPolygon>> polygons; 	vector<float> polygons_life; 	vector<ofColor> polygons_color; 	ofxBox2dEdge eage_1, eage_2; }; | 
| 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 | #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", 130, true, false, true); 	this->box2d.init(); 	this->box2d.setGravity(0, 50); 	this->box2d.createBounds(); 	this->box2d.setFPS(60); 	this->box2d.registerGrabbing(); 	this->eage_1 = ofxBox2dEdge(); 	this->eage_1.addVertex(ofVec2f(ofGetWidth() / 2 - 150, ofGetHeight() / 2 + 100)); 	this->eage_1.addVertex(ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2)); 	this->eage_1.create(this->box2d.getWorld()); 	this->eage_2 = ofxBox2dEdge(); 	this->eage_2.addVertex(ofVec2f(ofGetWidth() / 2 + 150, ofGetHeight() / 2 + 100)); 	this->eage_2.addVertex(ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2)); 	this->eage_2.create(this->box2d.getWorld()); } //-------------------------------------------------------------- void ofApp::update() { 	for (int i = this->polygons.size() - 1; i >= 0; i--) { 		this->polygons_life[i] -= 1; 		if (this->polygons_life[i] < 20) { 			this->polygons[i].get()->destroy(); 			this->polygons.erase(this->polygons.begin() + i); 			this->polygons_life.erase(this->polygons_life.begin() + i); 			this->polygons_color.erase(this->polygons_color.begin() + i); 		} 	} 	this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { 	for (int i = 0; i < this->polygons.size(); i++) { 		ofSetColor(this->polygons_color[i], this->polygons_life[i]); 		this->polygons[i].get()->draw(); 	} 	ofSetColor(32); 	this->eage_1.draw(); 	this->eage_2.draw(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key) { 	if (key >= 32 && key < 127) { 		char key_c = (char)key; 		string key_s{ key_c }; 		ofTTFCharacter path = this->font.getCharacterAsPoints(key_c); 		ofPoint c_size = ofPoint(this->font.stringWidth(key_s), this->font.stringHeight(key_s)); 		ofPoint adjust(ofGetWidth() / 2 - c_size.x / 2, c_size.y); 		ofColor color; 		color.setHsb(ofRandom(255), 255, 225); 		vector<ofPolyline> outline = path.getOutline(); 		for (int o_index = 0; o_index < outline.size(); o_index++) { 			outline[o_index] = outline[o_index].getResampledBySpacing(ofRandom(15, 40)); 		} 		ofMesh mesh; 		ofTessellator t; 		t.tessellateToMesh(outline, ofPolyWindingMode::OF_POLY_WINDING_ODD, mesh); 		vector<ofMeshFace> triangles = mesh.getUniqueFaces(); 		for (int i = 0; i < triangles.size(); i++) { 			shared_ptr<ofxBox2dPolygon> polygon = shared_ptr<ofxBox2dPolygon>(new ofxBox2dPolygon); 			polygon.get()->addTriangle(triangles[i].getVertex(0) + adjust, triangles[i].getVertex(1) + adjust, triangles[i].getVertex(2) + adjust); 			polygon.get()->setPhysics(1.0, 0.3, 0.3); 			polygon.get()->create(box2d.getWorld()); 			this->polygons.push_back(polygon); 			this->polygons_life.push_back(255); 			this->polygons_color.push_back(color); 		} 	} } //-------------------------------------------------------------- int main() { 	ofSetupOpenGL(720, 720, OF_WINDOW); 	ofRunApp(new ofApp()); } |