Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ofxBox2dBaseShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ bool ofxBox2dBaseShape::isBody() {
}

bool ofxBox2dBaseShape::isFixed() {
return density == 0.f ? true : false;
return ofIsFloatEqual(density, 0.0f);
// return density == 0.f ? true : false;
}

bool ofxBox2dBaseShape::isSleeping() {
Expand Down Expand Up @@ -105,6 +106,7 @@ void ofxBox2dBaseShape::setDensity(float val) {
for (auto * f = body->GetFixtureList(); f; f = f->GetNext()) {
f->SetDensity(density);
}
body->ResetMassData();
}
}

Expand Down Expand Up @@ -233,11 +235,13 @@ float ofxBox2dBaseShape::toB2d(const float f) {
//------------------------------------------------
ofVec2f ofxBox2dBaseShape::getPosition() {
ofVec2f p;
if(body != NULL) {
if(body != nullptr) {
const b2Transform& xf = body->GetTransform();
b2Vec2 pos = body->GetLocalCenter();
b2Vec2 b2Center = b2Mul(xf, pos);
p = toOf(b2Center);
p = toOf(b2Center);
}else{
cout << "ofxBox2dBaseShape::getPosition() body is null\n";
}
return p;
}
Expand Down
6 changes: 5 additions & 1 deletion src/ofxBox2dCircle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ void ofxBox2dCircle::draw() {




ofRectangle ofxBox2dCircle::getRect() {
ofRectangle r;
r.setFromCenter(getPosition(), radius, radius);
return r;
}



Expand Down
2 changes: 2 additions & 0 deletions src/ofxBox2dCircle.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ofxBox2dCircle : public ofxBox2dBaseShape {
void addRepulsionForce(float x, float y, float amt);
void addRepulsionForce(ofVec2f pt, float amt);

ofRectangle getRect() ;

};


Expand Down
110 changes: 80 additions & 30 deletions src/ofxBox2dRect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,84 @@ ofxBox2dRect::ofxBox2dRect() {

//------------------------------------------------
void ofxBox2dRect::setup(b2World * b2dworld, ofRectangle rec, float angle) {
setup(b2dworld, rec.x, rec.y, rec.width, rec.height, angle);

if(b2dworld == NULL) {
ofLog(OF_LOG_NOTICE, "- must have a valid world -");
return;
}

width = rec.width/2;
height = rec.height/2;



auto p = rec.getCenter();
b2BodyDef bodyDef;
if(density == 0.f) bodyDef.type = b2_staticBody;
else bodyDef.type = b2_dynamicBody;
auto x = toB2d(p.x);
auto y = toB2d(p.y);

bodyDef.position.Set(x, y);
bodyDef.angle = ofDegToRad(angle);

body = b2dworld->CreateBody(&bodyDef);

setRectangle(rec);

//// b2PolygonShape shape;
// shape = make_unique<b2PolygonShape>();
// shape->SetAsBox(toB2d(width), toB2d(height));
// fixture.shape = shape.get();
// fixture.density = density;
// fixture.friction = friction;
// fixture.restitution = bounce;
//
// fixturePtr = body->CreateFixture(&fixture);
// setPosition(p.x, p.y);
//
// updateMesh();
alive = true;

}

//------------------------------------------------
void ofxBox2dRect::setup(b2World * b2dworld, float x, float y, float w, float h, float angle) {


if(b2dworld == NULL) {
ofLog(OF_LOG_NOTICE, "- must have a valid world -");
return;
}

w /= 2;
h /= 2;
width = w; height = h;

b2PolygonShape shape;
shape.SetAsBox(toB2d(width), toB2d(height));
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(toB2d(x), toB2d(y));
bodyDef.angle = ofDegToRad(angle);

body = b2dworld->CreateBody(&bodyDef);
body->CreateFixture(&fixture);

updateMesh();
alive = true;
ofRectangle r;
r.setFromCenter(x, y, w, h);
setup(b2dworld, r, angle);
}
void ofxBox2dRect::setRectangle(const ofRectangle& rect){
if(body){
if(body->GetFixtureList()){
body->DestroyFixture(body->GetFixtureList());
}
width = rect.width/2;
height = rect.height/2;


if(shape == nullptr){
shape = make_unique<b2PolygonShape>();
}


shape->SetAsBox(toB2d(width), toB2d(height));
fixture.shape = shape.get();
fixture.density = density;
fixture.friction = friction;
fixture.restitution = bounce;

fixturePtr = body->CreateFixture(&fixture);

auto p = rect.getCenter();
setPosition(p.x, p.y);

updateMesh();

}
}


// Temporary fix until OF 0.8.0
static void rectangle(ofPath & path, const ofRectangle & r){
path.moveTo(r.getTopLeft());
Expand Down Expand Up @@ -207,6 +249,14 @@ void ofxBox2dRect::draw() {
*/
}

//------------------------------------------------
ofRectangle ofxBox2dRect::getRect(){
ofRectangle r;
auto p = getPosition();
r.setFromCenter(p.x, p.y, getWidth(), getHeight());
return r;
}




Expand Down
8 changes: 8 additions & 0 deletions src/ofxBox2dRect.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class ofxBox2dRect : public ofxBox2dBaseShape {
private:
float width, height;

b2Fixture* fixturePtr = nullptr;

unique_ptr<b2PolygonShape> shape = nullptr;
// b2PolygonShape shape;
public:
ofVboMesh mesh;
//------------------------------------------------
Expand All @@ -25,6 +29,10 @@ class ofxBox2dRect : public ofxBox2dBaseShape {
float getWidth() { return width * 2; }
float getHeight() { return height * 2; }


void setRectangle(const ofRectangle& rect);
ofRectangle getRect() ;

void updateMesh();
void draw();

Expand Down