Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit/AppGameKit Studio Showcase / Image to Polyline to Triangles (usefull for SetSpriteShapeChain)

Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 9th Feb 2023 17:20 Edited at: 9th Feb 2023 17:29
So i made some small librarys two years ago for the contour tracing, douglas peucker and triangulation (by ear clipping !?) algorithms.
The contour tragcing algorithm finds the contour of the image and creates points along it's contour.
This leaves you with a whole lot of points but you can simpify it with the douglas peucker algorithm which reduces the amount points by a given deviation distance between two points.
I created a simple example project that doubles as a sort of editor.

So you can create the points along the contour of an image for SetSpriteShapeChain() to use in 2D Physics for example.
This is interesting because normally you woud only have maximal 12 points with the autogenerated polygon shapes or you would set them manually with an editor and load them into AGK.
Now the triangulation would also be usefull if might have not messed something up, so this is an invitation to fix the bug.
I would have made an more advanced editor from this if we had build in GUI commands but for now it uses the code properties in debug mode for you to change the parameters.
You can see it worked at some point:




Login to view attachments
DBPro Master
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 9th Feb 2023 20:27
This is neat, was just thinking about making something like this actually. So by changing the deviation I can effectively make a more or less accurate trace?
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 10th Feb 2023 00:04 Edited at: 10th Feb 2023 00:09
Yes by changing the epsilon you make it so to say more or less accurate.
Points on the original polyline that are farther away than the epsilon distance from the simplified polyline are discarded, resulting in a simplified version of the polyline with fewer points.
And you can also change the alpha threshold which is used to control the minimum level of transparency for which a pixel is considered to be part of the contour.
While you already can move the Points manually, i also thought about erasing or lowering the alpha value of pixels by manually drawing on the image so the algorithm changes the generated polyline.
You might want to do this to create an inner wall for a vase for example while the vase is actually fully opaque.
So instead of moving and adding points to the polyline you could in a way sculpt the physics mesh.

If you continue this though it then ultimately leads to destructive terrain and objects in realtime

Login to post a reply

Server time is: 2023-09-29 08:48:53
Your offset time is: 2023-09-29 08:48:53