From 45738507d0c1313a6875dd409a1bee3435281250 Mon Sep 17 00:00:00 2001 From: Roy Macdonald Date: Tue, 5 Oct 2021 23:25:46 -0500 Subject: [PATCH] ofxBox2dRect: added get and set rectangle functions. ofxBox2dCircle: added getRect function. ofxBox2dBaseShape: using ofIsFloatEqual to check float value equality. Recalculating mass data when setting density. --- src/ofxBox2dBaseShape.cpp | 10 ++-- src/ofxBox2dCircle.cpp | 6 ++- src/ofxBox2dCircle.h | 2 + src/ofxBox2dRect.cpp | 110 +++++++++++++++++++++++++++----------- src/ofxBox2dRect.h | 8 +++ 5 files changed, 102 insertions(+), 34 deletions(-) diff --git a/src/ofxBox2dBaseShape.cpp b/src/ofxBox2dBaseShape.cpp index 13a8cfa..d57a3fc 100755 --- a/src/ofxBox2dBaseShape.cpp +++ b/src/ofxBox2dBaseShape.cpp @@ -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() { @@ -105,6 +106,7 @@ void ofxBox2dBaseShape::setDensity(float val) { for (auto * f = body->GetFixtureList(); f; f = f->GetNext()) { f->SetDensity(density); } + body->ResetMassData(); } } @@ -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; } diff --git a/src/ofxBox2dCircle.cpp b/src/ofxBox2dCircle.cpp index 671f0ff..6dbd289 100755 --- a/src/ofxBox2dCircle.cpp +++ b/src/ofxBox2dCircle.cpp @@ -164,7 +164,11 @@ void ofxBox2dCircle::draw() { - +ofRectangle ofxBox2dCircle::getRect() { + ofRectangle r; + r.setFromCenter(getPosition(), radius, radius); + return r; +} diff --git a/src/ofxBox2dCircle.h b/src/ofxBox2dCircle.h index b8093b4..92a27db 100755 --- a/src/ofxBox2dCircle.h +++ b/src/ofxBox2dCircle.h @@ -33,6 +33,8 @@ class ofxBox2dCircle : public ofxBox2dBaseShape { void addRepulsionForce(float x, float y, float amt); void addRepulsionForce(ofVec2f pt, float amt); + ofRectangle getRect() ; + }; diff --git a/src/ofxBox2dRect.cpp b/src/ofxBox2dRect.cpp index 43e9083..4b5e73c 100755 --- a/src/ofxBox2dRect.cpp +++ b/src/ofxBox2dRect.cpp @@ -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(); +// 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(); + } + + + 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()); @@ -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; +} + diff --git a/src/ofxBox2dRect.h b/src/ofxBox2dRect.h index 815dbe7..aa28451 100755 --- a/src/ofxBox2dRect.h +++ b/src/ofxBox2dRect.h @@ -12,6 +12,10 @@ class ofxBox2dRect : public ofxBox2dBaseShape { private: float width, height; + b2Fixture* fixturePtr = nullptr; + + unique_ptr shape = nullptr; +// b2PolygonShape shape; public: ofVboMesh mesh; //------------------------------------------------ @@ -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();