//------------------------------------------------ void ofxBox2dRect::draw() { if(body == NULL) { return; } // update the polyline getRectangleShape(); ofPath path; for (int i=0; i<shape.size(); i++) { if(i==0)path.moveTo(shape[i]); else path.lineTo(shape[i]); } // draw the path path.setColor(ofGetStyle().color); path.setFilled(ofGetStyle().bFill); path.draw(); // are we sleeping if(isSleeping()) { ofPushStyle(); ofEnableAlphaBlending(); path.setColor(ofColor(255, 100)); path.setFilled(true); path.draw(); ofPopStyle(); } }
//------------------------------------------------ void ofxBox2dRect::setup(b2World * b2dworld, float x, float y, float w, float h,b2BodyType type ) { if(b2dworld == NULL) { ofLog(OF_LOG_NOTICE, "- must have a valid world -"); return; } if (OF_RECTMODE_CORNER) { w/=2; h/=2; x += w; y += h; } width = w; height = h; b2PolygonShape shape; shape.SetAsBox(w/OFX_BOX2D_SCALE, h/OFX_BOX2D_SCALE); fixture.shape = &shape; fixture.density = density; fixture.friction = friction; fixture.restitution = bounce; b2BodyDef bodyDef; if(density == 0.f) bodyDef.type = b2_staticBody; else bodyDef.type = b2_dynamicBody; bodyDef.position.Set(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE); body = b2dworld->CreateBody(&bodyDef); body->CreateFixture(&fixture); body->SetType(type); // update the rectShape getRectangleShape(); }