[ Video ]
[ About ]
ofxBox2dという物理エンジンのaddonを使ってみました。
キーボードのキー入力がある間は、Circleが毎フレーム追加されていきます。
この動画中では重力が存在するので生成されたCircleは下へ落下します。
キー入力中は画面四方の壁が存在していますが、キー入力がなくなると消えるので、画面外に落下して見えなくなります。(放っておくと計算量が増え続けるので、見えなくなったCircleを削除しています)
重力や衝突、摩擦などの計算はすべてBox2d側で処理をしてくれるので、利用する側は諸々の設定をしたあとに、update関数やdara関数を呼んであればOKです
[ 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<shared_ptr<ofxBox2dCircle>> circles; 		vector<ofColor> circles_color; }; | 
| 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 | #include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ 	ofSetFrameRate(60); 	ofBackground(0); 	ofSetWindowTitle("Insta"); 	this->box2d.init(); 	this->box2d.setGravity(0, 10); 	this->box2d.createBounds(); 	this->box2d.setFPS(60); 	this->box2d.registerGrabbing(); } //-------------------------------------------------------------- void ofApp::update(){ 	if (ofGetKeyPressed()) { 		if (this->box2d.bCheckBounds) { 			float r = ofRandom(1.0, 20.0); 			this->circles.push_back(shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle)); 			this->circles.back().get()->setPhysics(3.0, 0.53, 0.1); 			this->circles.back().get()->setup(this->box2d.getWorld(), ofRandom(ofGetWidth()), 0, r); 		} else { 			this->box2d.createBounds(); 			this->box2d.checkBounds(true); 		} 	} else { 		if (this->box2d.bCheckBounds) { 			this->box2d.createBounds(0, 0, 0, 0); 			this->box2d.checkBounds(false); 		} 	} 	ofColor circle_color; 	circle_color.setHsb(ofRandom(255), 255, 255); 	this->circles_color.push_back(circle_color); 	this->box2d.update(); 	for (int i = this->circles.size() - 1; i > -1; i--) { 		if (this->circles[i].get()->getPosition().y > ofGetHeight() + 20) { 			this->circles[i].get()->destroy(); 			this->circles.erase(this->circles.begin() + i); 			this->circles_color.erase(this->circles_color.begin() + i); 		} 	} } //-------------------------------------------------------------- void ofApp::draw(){ 	for (int i = 0; i < this->circles.size(); i++) { 		ofNoFill(); 		ofSetColor(this->circles_color[i]); 		this->circles[i].get()->draw(); 	} 	this->box2d.drawGround(); } //======================================================================== int main() { 	ofSetupOpenGL(720, 720, OF_WINDOW); 	ofRunApp(new ofApp()); } |