AutoPolygon problem

I’m having some problems tracing an image into a PhysicsBody, does anyone have any suggestions?

Upper left is the kind of source image I’m working with (white = transparent, btw).
Upper right is the kind of polygon I end up with.
Lower right is what I want.

My code:

std::unique_ptr<Image> img = std::make_unique<Image>();
img->initWithImageFile("images/jar_shape.png");

auto ap = AutoPolygon(img->getFilePath());
auto rect = Rect(0, 0, img->getWidth(), img->getHeight());
std::vector<Vec2> verts = ap.trace(rect);
verts = ap.reduce(verts, rect, 1.0);
verts = ap.expand(verts, rect, 7.0);

auto jarBody = PhysicsBody::createPolygon(verts.data(), verts.size());

If, instead of createPolygon, I use createEdgePolygon it kind of works; as the name implies, I only get the outline of the shape - I’d like to have it filled…

Anyone know how to do this?

1 Like

Sorry, perhaps I don’t understand. Isnt

what you have in the top right pic?

To clarify.

Top right: The polygon covers the whole outer limits of the jar. The empty space inside the jar is closed up (not what I want).
Bottom right: The inside of the jar is left empty. Only the non-transparent pixels are covered with physics shapes/polygons.

Using createEdgePolygon, I only get the outline of the outline, which has empty space inside it (not exactly what I want, but I might have to stick with it, see below image).

Sorry, I’m still not seeing what you want compared to the edge image you posted. Can you show me that better?

Ok, maybe a zoomed in image could show it better.

You can use Physics Editor for this, it will be easy & fast.
Check one of my game here : Kitchen Chef

1 Like

@smitpatel88 actually, can you explain more about what the OP wants to achieve. I’m not clear from the pictures posted what the goal is.

Basically he wants custom physics body, which is possible with Physics Editor only.