Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Last active April 24, 2018 19:51
Show Gist options
  • Select an option

  • Save vanderlin/7593342 to your computer and use it in GitHub Desktop.

Select an option

Save vanderlin/7593342 to your computer and use it in GitHub Desktop.
Rotate a box2d Rect
#include "testApp.h"
#include "ofxBox2d.h"
ofxBox2dRect centerRect;
ofxBox2d box2d;
vector <ofPtr<ofxBox2dCircle> > circles;
//--------------------------------------------------------------
void testApp::setup() {
ofDisableAntiAliasing();
ofSetVerticalSync(true);
ofBackgroundHex(0xfdefc2);
ofSetLogLevel(OF_LOG_NOTICE);
box2d.init();
box2d.setGravity(0, 10);
box2d.createGround();
box2d.setFPS(30.0);
centerRect.setPhysics(0, 0, 0);
centerRect.setup(box2d.getWorld(), ofGetWidth()/2, ofGetHeight()/2, 500, 50);
}
//--------------------------------------------------------------
void testApp::update() {
box2d.update();
if((int)ofRandom(10)==0) {
circles.push_back(ofPtr<ofxBox2dCircle>(new ofxBox2dCircle));
circles.back().get()->setPhysics(3.0, 0.53, 0.1);
circles.back().get()->setup(box2d.getWorld(), ofRandom(-100, 100)+(ofGetWidth()/2), 10, ofRandom(2, 10));
}
ofRemove(circles, ofxBox2dBaseShape::shouldRemoveOffScreen);
}
//--------------------------------------------------------------
void testApp::draw() {
for(int i=0; i<circles.size(); i++) {
ofFill();
ofSetHexColor(0x90d4e3);
circles[i].get()->draw();
}
ofFill();
ofSetColor(255, 0, 255);
centerRect.draw();
ofSetColor(255);
ofCircle(centerRect.getPosition(), 3);
string info = "";
info += "Press [c] for circles\n";
info += "Total Bodies: "+ofToString(box2d.getBodyCount())+"\n";
info += "Total Joints: "+ofToString(box2d.getJointCount())+"\n\n";
info += "FPS: "+ofToString(ofGetFrameRate())+"\n";
ofSetHexColor(0x444342);
ofDrawBitmapString(info, 30, 30);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key) {
if(key == 'c') {
float r = ofRandom(4, 20);
circles.push_back(ofPtr<ofxBox2dCircle>(new ofxBox2dCircle));
circles.back().get()->setPhysics(3.0, 0.53, 0.1);
circles.back().get()->setup(box2d.getWorld(), mouseX, mouseY, r);
}
if(key == 't') ofToggleFullscreen();
}
//--------------------------------------------------------------
void testApp::keyReleased(int key) {
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ) {
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button) {
ofVec2f vec = ofPoint(x, y) - centerRect.getPosition();
float angle = ofRadToDeg( atan2(-vec.y, vec.x) * -1 );
centerRect.setRotation(angle);
centerRect.updateMesh();
printf("Angle %f\n", angle);
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button) {
ofVec2f vec = ofPoint(x, y) - centerRect.getPosition();
float angle = ofRadToDeg( atan2(-vec.y, vec.x) * -1 );
centerRect.setRotation(angle);
centerRect.updateMesh();
printf("Angle %f\n", angle);
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button) {
}
//--------------------------------------------------------------
void testApp::resized(int w, int h){
}
@hassang2

Copy link
Copy Markdown

Thank you! This helped me with rotating objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment