vanderlin / ofxBox2d

Openframework wrapper for box2d

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possibility to specify the world bounds

acarreras opened this issue · comments

Sometimes is interesting to let the box2D world to have some open bound.

ofxBox2d.h

void createBounds(float x=0, float y=0, float w=ofGetWidth(), float h=ofGetHeight(), bool btop=true, bool bbottom=true, bool bleft=true, bool bright=true);

ofxBox2d.cpp

void ofxBox2d::createBounds(float x, float y, float w, float h, bool btop, bool bbottom, bool bleft, bool bright) {

    if(world == NULL) {
        ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -");
        return;
    }
    if(ground!=NULL) world->DestroyBody(ground);

    b2BodyDef bd;
    bd.position.Set(0, 0);
    ground = world->CreateBody(&bd);

    b2EdgeShape shape;

    ofRectangle rec(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE, w/OFX_BOX2D_SCALE, h/OFX_BOX2D_SCALE);

    if(bright){
        //right wall
        shape.Set(b2Vec2(rec.x+rec.width, rec.y), b2Vec2(rec.x+rec.width, rec.y+rec.height));
        ground->CreateFixture(&shape, 0.0f);
    }
    if(bleft){
        //left wall
        shape.Set(b2Vec2(rec.x, rec.y), b2Vec2(rec.x, rec.y+rec.height));
        ground->CreateFixture(&shape, 0.0f);
    }
    if(btop){
        // top wall
        shape.Set(b2Vec2(rec.x, rec.y), b2Vec2(rec.x+rec.width, rec.y));
        ground->CreateFixture(&shape, 0.0f);
    }
    if(bbottom){
        // bottom wall
        shape.Set(b2Vec2(rec.x, rec.y+rec.height), b2Vec2(rec.x+rec.width, rec.y+rec.height));
        ground->CreateFixture(&shape, 0.0f);
    }
}

yes - this creates a rectangle based on the params. It defaults to the screen width and height.