How to create a polygon which has the same shape of a sprite

I wanna create a polygon body by the vertexes, and the polygon should be has the same shape with the sprite which is its userdata, how can I get the edge vertexes of the sprite Or how can I create it as the sprite shape
The basics of it should be quite simple, since the sprite is a box just create a box.

But I suspect you want more than just a box, I suspect you want some kind of sprite collision detection.

I have some idea's how to do that and how to implement it on a graphics card since manipulating all those pixels can be quite expensive...
If you really want to turn the sprite into a polygon then here is an idea:

Turn each pixel into a polygon box perhaps slightly larger than it's original pixel size.

And then use a polygon boolean operations library to try and merge all the polygons together in one or perhaps a few shapes.

It would be a bit blocky... but it's a start 😉
I actually remember inventing an algorithm long ago in dos which could be used to turn a bitmap into a polygon...

So I might give that a try... first I am going to google this problem... see what it turns up... and if I don't like what I find... I roll my own... which is quite amuzing and probably very simple and pretty fast...

Though I keep it secret for now... it will probably work and you might buy it for a big price... lol... otherwise forget about it ! LOL =D
Hmm... I just realized something... boolean operations won't help... and the algorithm I had in mind also won't help that much...

The problem with box2d is that polygon shapes need to be convex... so the only thing which would slightly help is "convex hull" which would probably suck...

Good thing I realized this early so I don't waste any time on dead-ends 😉

So now back to the mission at hand... I guess an easy solution would simply be to use "boxes" to build up the rigid body out of multiple shapes...

The author itself warns that treating all pixels as individual boxes would be a bad idea... I am not so sure about that... if the engine itself makes a "bounding box" around rigid bodies than it shouldn't be so bad...

Still... simply converting the bitmap into boxes might be a nice start...

An additional idea could be to convert multiple boxes which sit next to each other to large boxes as to save "space/processing power"

So something like this:

BitmapToBoxes/Rectangles:

+---+
| |
+---+-----------+
| |
+---------------+

Follow-up:

Anyway... the engine seems to work with AABB boxes or whatever that is... and these seem to be per polygon shape.. and there doesn't seem to be a big rigid body bounding box... so this idea of many little boxes is probably a bad idea with the current state of the engine 😉

Bye,
Skybuck.
Then again concave polygons might still be usefull... they could be converted to triangles which are convex shapes...

So then the algorithms do become usefull...
Ok,

I consulted my old pascal code on cd's... kinda funny... 1998... what an old junk/code that was !

Though the code I found was kinda nice... but a bit different...

There is always a patent from later in 1998 which seems samiliar to my method which is kinda odd...

None the less I looked at the problem from a little ascii picture and now I see a new/better solution/algorithm to make it work...

So later I am gonna give that a try and program it and then that will probably be the last of it and thus problem then more or less solved...

Also the delphi code comes with nice helper routines for polygons so that might save me some work with converting to triangles and such... it could even do concave to convex polygon conversions but might be a bit lossy... so will so about that...

But me now too tired to continue ! 😉 🙂
Here's how I do this:

    1. Take sprite image and make a binary (BW) mask out of it.
    2. Then use OpenCV to find the contour/hull from the BW mask.
    3. Decompose the possibly concave hull into multiple convex hulls.
    4. Add each hull as a Polygon Shape to your body.

Hey presto.

NB: I should point out that I generate the polygon shapes from the image on 'load' of the application... you probably don't want to do it between frames while running your app realtime.
Yay! So excited to try this later!
Hi,

It's very simple You can use this stuff

int32 num=6;
b2Vec2 Verts[6];

Verts[5].Set(-(w)/PTM_RATIO,0/PTM_RATIO);
Verts[4].Set(-(0.5*w)/PTM_RATIO,h/PTM_RATIO);
Verts[3].Set((0.5*w)/PTM_RATIO,h/PTM_RATIO);
Verts[2].Set((w)/PTM_RATIO,0/PTM_RATIO);
Verts[1].Set((0.5*w)/PTM_RATIO,-h/PTM_RATIO);
Verts[0].Set(-(0.5*w)/PTM_RATIO,-h/PTM_RATIO);

b2PolygonShape Shape;
Shape.Set(Verts,num);

Where this stuff creates hexagon and by increasing number of vertex's you can create body of shape which ever you want....


Regards

Anand

Original thread: https://box2d.org/forum/viewtopic.php?t=5870