Box2d shape confusion

Hello there,
I have set up a box2d body with the following shape:

	b2Vec2 verticies[7];
	verticies[0] = b2Vec2(0, 0);
	verticies[1] = b2Vec2(1, 1);
	verticies[2] = b2Vec2(2, 1);
	verticies[3] = b2Vec2(3, 3);
	verticies[4] = b2Vec2(4, 1);
	verticies[5] = b2Vec2(5, 1);
	verticies[6] = b2Vec2(6, 0);

	b2PolygonShape ps;
	ps.Set(verticies, 7);

I expected a shape like this(#paint :slight_smile: ):


But I get this:

There probably is a pretty simple and logical explanation for this and I would love to know that one. Could anybody please assist me. Thanks! Much appreciated! Btw. I have tried to do some research before asking

Where did you get that result image from? A cocos2d-x project or a box2d editor? Maybe it’s just not renedered properly, you code seems fine, except the fact that you are creating a non-convex shape which is a problem; box2d doesn’t work well with concave shapes, but it should have drawn it right (though that may depend on your box2d version).

Hi! First of all, thank you so much for your reply!! I use cocos2d-x 3.7 and the result image is from a cocos2d-x project using the box2d version that follows along. I get what your saying but still cannot figure out what exactly to do?

Here is a little article about convex and concave shapes. Also, you might want to use the b2Vec2::Set function to set your vertices like this: verticies[0].Set(0, 0); etc.

And one more thing: the vertices order should be provided counter-clockwise, your vertices are in a clockwise order right now.

1 Like

Points (2, 1) and (4, 1) are removed as they are not in the convex hull.

1 Like

But I do need those points in order to achieve the shape i want correct? So it is not possible to create such a shape or? and @Boby thanks, that did help me a little but still not enough so that i can create the shape i want…

You have to split your concave polygon into multiple convex polygons.

Well true, thanks buddy! But doesn’t that take up more memory than when only have one polygon or? (1 < 2+ so probably yes but more like how)

It is linear, i.e. two polygons will consume twice the amount of memory of one polygon.

1 Like

guys thanks for it all. it helped me sorting it out but i still do not get why box2d just doesn’t support concave shapes… wouldn’t that make it all easier?

Well I believe math problems :stuck_out_tongue: