Sprite To Polygon

by DevonX

Posted on February 12, 2007 at 5:13 PM

This is a simple engine that takes a sprite and it draws a polygon on a surface that outlines the sprite, this engine is not very sound though. I will be updating it sometime with a better version. Right now, complex sprites can cause glitches, i have done all i can to prevent infinite loops, so you won't get those often, prolly just once in a while. What it does is for every 4 degrees, it moves out from the center of the sprite until it hits the transparency color, then it adds a point to the polygon there.

Juju commented February 12, 2007 at 5:49 PM
Simple yet clever. As you said, a bit unstable on occasion but in all other aspects rather nice. 9/10
Crazy Star commented February 12, 2007 at 8:08 PM
A different approach could be to:
1.1 start a for loop running through all pixels in the sprite
1.2 run a flood fill like script that adds all transparent pixels with adjacent not tranparent pixel(s) to a list
1.3 if the transparent pixel is already in a list continue, otherwise create a new list, start a new flood fill and add all the transparent pixels to the new list.

That was part 1.
Part 2:
2.1 Start a for loop running though the lists one at a time
2.2 for each tranparent pixel find the two nearest pixels in the same list (let's call them prev and next), and store those. (I guess a two dimensional array would do) You could even be advanced and add an argument to determine how many pixels should be skipped (see part 2b)

Part 2b:
2b.1 You're doing this instead of the loop in part 2
2b.2 for one tranparent pixel find the argument num nearest pixels in the same list, and store the ones furthest away in a whole new list. Now do the same for those 2
2b.3 repeat this process until all pixels has either been checked or added to the new list
2b.4 delete all pixels from the old list that didn't make it to the new list

Part 3:
3.1 draw lines from all the remaining pixels to their nearest ones.
*note I realize that this would draw double as many lines as needed but what the hey...

(I haven't tested this)
OL commented February 12, 2007 at 8:10 PM
Pretty cool, I may find this useful.
Crane-ium commented February 12, 2007 at 11:23 PM
Pretty interesting. Although I found it rather pointless to use in a game unless you prove me wrong. Works rather well though, few unstabilities though as you said. And the outline isn't perfect either, but that's ok because it's pixels...8/10
gtvg commented February 13, 2007 at 9:55 PM
Good, but there are a few glitche and it is not entirely accurate.
DevonX commented February 14, 2007 at 12:28 AM
alright spectre, ill experimant with some of ur suggestions
Melancor commented February 21, 2007 at 8:52 AM
This seems to do the same as "create polygon auto" from GMPhysics. Only your code is MUCH more compact: I think create polygon auto is 3 pages long, yours only 26 lines. And it sure is useful (if, like me, you are using GMPhysics where such Polygons are required for the generation of collision bodies)
ussyless commented February 27, 2007 at 2:27 PM
I didnt check out the coding, but neat
DevonX commented April 08, 2007 at 11:28 PM
Melancor, I do remember that function from GMPhysics, I did not use that, because that wouldn't be right. I never even thouroughly went though that script. This is entirly my approach. I thought of it durring math class . [:)]
Blood Ninji commented July 26, 2008 at 4:41 PM
Hey i got a use for it, to easily generate polygon shapes in polygon shooters like asteroids, were the shape would look crumby through rotation the poly would keep a nice figure.