ofBox2d shape - can I make it solid?

Hi everybody,
I am playing with ofBox2d and openCV. I managed to implement a classic app everybody seems to be implementing when first experimenting. The interaction with objects (as seen in the picture).
I am using openCV to One thing I notice however is that once in a while some objects manage to pass through. When I walk from side to side for example I can’t effectively kick the letters out of the screen. Most of them pass right through me even though I manage to stop the ones falling. Is there a way of making my polyline shape be solid and not hollow so that these things don’t happen? Any idea?

openCV + box2D = happiness :smile:

Thanks
marinero

you basically have to fill/triangulate the contour - I used this addon

There is also this thread

you can also use an ofPath to tesselate a shape, just call getTesselation() and it’ll return a mesh composed by triangles

thanks @jvcleave + @arturo,
I tried what you recommended. From my understanding, ofxBox2D comes with a triangulation function nowadays, so ofxTriangle or getTesselation might not be needed. In fact including ofxTriangle+ofxBox2d gave an error because of double definitions.

I managed to write a very simple example where the same concave shape is re-created within every update and then triangulated and inserted into the box2d world. The reason it’s created every time in update() is because if I just create it and call the updates of box2d, then the triangles behave as separate objects, subject to gravity and therefore break up and fall apart.
Also as balls fall from the top I notice that balls move the triangles a bit and if the shape is moved fast enough balls push apart the triangles and they enter the mass.

Is there a way to prevent all of these? I just want the triangles to be attached to each other as if they were one object without any chance of breaking up (yet maintain interaction with the falling balls). I am scratching my head. Is there something I am not getting with box2d? In Processing there is a way to define objects as static.

Thanks.
marinero

ps. here is an image of the mouse controlled shape being broken up by falling balls
 

source.zip (1.4 KB)

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup()
{
    // Box2d
    box2d.init();
    box2d.setGravity(0, 20);
    box2d.createGround();
    box2d.setFPS(30.0);
}

//--------------------------------------------------------------
void ofApp::update()
{
    insertBody();

    if((int)ofRandom(0, 10) == 0)
    {
        ofPtr<ofxBox2dCircle> circle = ofPtr<ofxBox2dCircle>(new ofxBox2dCircle);
        circle.get()->setPhysics(3, 0.5, 0.1);
        circle.get()->setup(box2d.getWorld(), (ofGetWidth()/2)+ofRandom(-20, 20), -20, ofRandom(10, 20));
        circles.push_back(circle);
    }

    box2d.update();
}

//--------------------------------------------------------------
void ofApp::draw()
{
    ofBackground(125);

    ofFill();

    for (int i=0; i<circles.size(); i++)
    {
        ofFill();
        ofSetHexColor(0xc0dd3b);
        circles[i].get()->draw();
    }


    ofSetColor(255);
    ofNoFill();

    for (int i=0; i<polyShapes.size(); i++)
    {
        polyShapes[i].get()->draw();
        ofCircle(polyShapes[i].get()->getPosition(), 3);
    }

}

//--------------------------------------------------------------
void ofApp::insertBody()
{
    shape.clear();
    //shape.addVertex(x, y);

    //A rectangle with a chunk taken out, making a concave shape
    shape.addVertex(ofPoint(mouseX+0, mouseY));
    shape.addVertex(ofPoint(mouseX+200, mouseY));
    shape.addVertex(ofPoint(mouseX+200, mouseY+300));
    shape.addVertex(ofPoint(mouseX+0, mouseY+300));
    shape.addVertex(ofPoint(mouseX+0, mouseY+220));
    shape.addVertex(ofPoint(mouseX+75, mouseY+150));
    shape.addVertex(ofPoint(mouseX+0, mouseY+80));

    shape.simplify();
    ofPolyline outline = shape;
    ofPolyline resampled = shape.getResampledBySpacing(25);

    vector <TriangleShape> tris;
    tris = triangulatePolygonWithOutline(resampled, outline);

    addRandomPointsInside(shape, 255);
    polyShapes.clear();

    // now loop through all the triangles and make a box2d triangle
    for (int i=0; i<tris.size(); i++)
    {
          ofPtr<ofxBox2dPolygon> triangle = ofPtr<ofxBox2dPolygon>(new ofxBox2dPolygon);
          triangle.get()->addTriangle(tris[i].a, tris[i].b, tris[i].c);
          triangle.get()->setPhysics(1, 0.3, 1); //density, bounce, friction
          triangle.get()->create(box2d.getWorld());

         polyShapes.push_back(triangle);
    }
}

adding to my own response:
it seems that I need to make the object static:

for (int i=0; i<tris.size(); i++)
{
      ofPtr<ofxBox2dPolygon> triangle = ofPtr<ofxBox2dPolygon>(new ofxBox2dPolygon);
      triangle.get()->addTriangle(tris[i].a, tris[i].b, tris[i].c);
      triangle.get()->setPhysics(1, 0.3, 1); //density, bounce, friction
      triangle.get()->create(box2d.getWorld());
      triangle.get()->body->SetType(b2_staticBody);  <---ADDED THIS LINE
      polyShapes.push_back(triangle);
}

this however makes my object get drawn in the top left corner mangled up while the balls keep interacting with a “ghost” object in the middle of the screen. Any ideas how to make the object static? It seems that this would solve the above problem with the physics engine “interfering” and doing all the calculations as if it were a dynamic object. Even though now mine is supposed to be static, if I move fast enough some balls pass through its walls and remain inside. Is that normal? Shouldn’t static object be impregnable?

thank you so much @marinero! I struggled with ‘duplicate error’.

Did you ever solve this Marinero? I am having the exact same issues, so would be interested in any solution you ended up with…

You have to make the object static as I suggest above. I never manage to fix the “impregnable” error, but the solution I propose right above your post was good enough for my purpose.
I later thought it might be possible to use “joints” to join shapes together which I believe box2d supports. If you’re not happy with the results you get look into that.
tp

Ah okay, I was looking for a solution for the “impregnable” error… Thanks for your quick reply though!