problem creating closed polygon with box2d


so i’m trying to just create a triangle in box2d (using the latest version on github) and ofw 008, but im having issues

my code looks like this

void testApp::setup(){  
	box2d.setGravity(0, 20);  
void testApp::update(){  
void testApp::draw(){  
    for(int i=0; i<triangles.size(); i++){  
void testApp::mouseReleased(int x, int y, int button){  
    // make a triangle  
    ofxBox2dPolygon t;  
    ofVec2f a = ofVec2f(100,0);  
    ofVec2f b = ofVec2f(0,200);  
    ofVec2f c = ofVec2f(200,200);  
    t.addTriangle(a, b, c);  
    t.setPhysics(0.3, 1.0, 0.1);  
    // print some debug      
    vector <ofPoint>&pts = t.getVertices();  
    printf("size=%i\n", (int)pts.size());  
    for(int i=0; i<pts.size(); i++){  
        printf("%i %f %f\n", i, pts[i].x, pts[i].y);  
    printf("closed=%i\n", (int)t.isClosed());  
    // move to mouse pos  

as you can see, in terms of box2d shape, there are only two sides. so instead of doing addTriangle() i also tried

t.addVertex(a.x, a.y);
t.addVertex(b.x, b.y);
t.addVertex(c.x, c.y);
t.addVertex(a.x, a.y);

nothing, same problem. I also tried t.close() (supposed to close the polyline) but nothing.

when i draw the triangle i see two sides, but when i print the vertices (as shown in screenshot in console), if i do t.setAsEdge(false); i see 3 points listed, when i do t.setAsEdge(true) i see only two points listed. either way its not a closed triangle shape in box2d, so something is going wrong.

any thoughts greatly appreciated,


I used box2d to draw different shapes and I never had this issue…

from the src file:

for (b2Fixture * f = body->GetFixtureList(); f; f = f->GetNext()) {  
		b2PolygonShape * poly = (b2PolygonShape*)f->GetShape();  
		if(poly) {  
			for(int i=0; i<poly->GetVertexCount(); i++) {  
				b2Vec2 pt = b2Mul(xf, poly->GetVertex(i));  
				drawShape.addVertex(pt.x*OFX_BOX2D_SCALE, pt.y*OFX_BOX2D_SCALE);  
			if(isClosed()) drawShape.close();  

Could you try to set the “close” to true? (I guess is just setClose(true) inherited from ofpolyline)


Hi Chris,

I have the same problem with OF 0.8 and latest ofxBox2d from Github. The issue is related to the t.create(box2d.getWorld()) line cause when calling that function one vertex is lost from the triangle. An ugly hack I found that works for triangles is using t.setAsEdge(true); before calling create an then force the triangle to close by modifying ofxBox2dPolygon.cpp class like the code below so I guess the issue is related to ofxBox2d itself and it should be reported in ofxBox2d’s github.



if(bSetAsEdge) {  
     for (int i=1; i<size(); i++) {  
	b2PolygonShape	shape;  
	b2Vec2 a = screenPtToWorldPt(getVertices()[i-1]);  
	b2Vec2 b = screenPtToWorldPt(getVertices()[i]);  
	shape.SetAsEdge(a, b);  
	fixture.shape		= &shape;  
	fixture.density		= density;  
	fixture.restitution = bounce;  
	fixture.friction	= friction;	  
            // Add this:  
            b2PolygonShape	shape;  
            b2Vec2 a = screenPtToWorldPt(getVertices()[0]);  
            b2Vec2 b = screenPtToWorldPt(getVertices()[size()]);  
            shape.SetAsEdge(a, b);  
            fixture.shape		= &shape;  
            fixture.density		= density;  
            fixture.restitution = bounce;  
            fixture.friction	= friction;  

I posted this and it works,
I think there is a bug in the rendering part that I will fix, but the gist works for now.

i fixed this in my version of ofxBox2d also… let me know if you’re still having an issue and i’ll post. @wbvdw on twitter.