[ Video ]
[ About ]
Box2DのオブジェクトをMouse操作で遊んでいます。
マウスの左ボタンでオブジェクトを掴んでいます。
マウスの中ボタンでRectを生成
マウスの右ボタンで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 28 29 30 31 32 |
#pragma once #include "ofMain.h" #include "ofxBox2d.h" class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void keyPressed(int key) {} void keyReleased(int key) {} void mouseMoved(int x, int y) {} void mouseDragged(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) {} ofVec2f press_position; ofxBox2d box2d; vector<shared_ptr<ofxBox2dCircle>> circles; vector<ofColor> circles_color; vector<shared_ptr<ofxBox2dRect>> rects; vector<ofColor> rects_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 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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofSetFrameRate(60); ofBackground(0); ofSetWindowTitle("Insta"); ofSetCircleResolution(60); ofSetLineWidth(1.5); this->box2d.init(); this->box2d.setGravity(0, 10); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); } //-------------------------------------------------------------- void ofApp::update(){ this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw(){ if (this->press_position != ofVec2f(0, 0)){ // Center button if (ofGetMousePressed(1)) { ofVec2f mouse_position = ofVec2f(ofGetMouseX(), ofGetMouseY()); ofVec2f difference = mouse_position - this->press_position; ofColor line_color; line_color.setHsb(ofGetFrameNum() % 255, 255, 255); ofSetColor(line_color); ofFill(); ofDrawRectangle(this->press_position, difference.x, difference.y); } // Right button if (ofGetMousePressed(2)) { ofVec2f mouse_position = ofVec2f(ofGetMouseX(), ofGetMouseY()); float r = mouse_position.distance(this->press_position); ofColor line_color; line_color.setHsb(ofGetFrameNum() % 255, 255, 255); ofSetColor(line_color); ofNoFill(); ofDrawEllipse(this->press_position, r * 2, r * 2); } } ofNoFill(); for (int i = 0; i < this->circles.size(); i++) { ofSetColor(this->circles_color[i]); this->circles[i].get()->draw(); } for (int i = 0; i < this->rects.size(); i++) { ofSetColor(this->rects_color[i]); this->rects[i].get()->draw(); } this->box2d.drawGround(); } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button) { // Center or Right button if (button == 1 || button == 2) { this->press_position = ofVec2f(x, y); } } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button) { // Center button if (button == 1) { ofVec2f mouse_position = ofVec2f(x, y); ofVec2f difference = mouse_position - this->press_position; this->rects.push_back(shared_ptr<ofxBox2dRect>(new ofxBox2dRect)); this->rects.back().get()->setPhysics(3.0, 0.53, 0.1); this->rects.back().get()->setup(this->box2d.world, this->press_position.x + difference.x / 2, this->press_position.y + difference.y / 2, difference.x, difference.y); ofColor rect_color; rect_color.setHsb(ofGetFrameNum() % 255, 255, 255); this->rects_color.push_back(rect_color); this->press_position = ofVec2f(0, 0); } // Right button if (button == 2) { ofVec2f mouse_position = ofVec2f(x, y); float r = mouse_position.distance(this->press_position); 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(), this->press_position.x, this->press_position.y, r); ofColor circle_color; circle_color.setHsb(ofGetFrameNum() % 255, 255, 255); this->circles_color.push_back(circle_color); this->press_position = ofVec2f(0, 0); } } //======================================================================== int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |