Utility: create a physics object that follows the exact outline of your image

This utility creates a set of vectors that map the exact outline of any image, so you can create a physics object that reacts accurately to collisions with your image.
http://pastebin.com/embed_js.php?i=J9U4XE5y

I have included demo code, but all you really need in your own project is the VectorOutline class, and it contains instructions and comments. You can change the demo image in the Main tab to see how different images bounce.

I have tried it on many of the built in images, and with one exception, it seems to work well. Comments, corrections, improvements and suggestions are welcome.

Wow, that works really well. Great utility, thank you for sharing it!

Amazing, This is amazing @Ignatz, keep up the AWESOME code.

@ignatz,
R u sure ur a beginner?
Ur too good at this to be!

@CodeaNoob ikr

there’s a stack overflow error caused by certain images that are bigger than the given image’s dimensions namely it seems to be any thing 200 x 200 or more. I hereby submit my revision solution.

w,h = spriteSize(img)
    if w > 100 or h > 100 then
    imgname = string.sub(debug.getinfo(1).source,tonumber(string.find(debug.getinfo(1).source,"readImage")+ string.len("readImage%(")),-1)
imgname = string.sub(imgname,1,string.find(imgname,'%"%)')-1)
 
    --revZD coding is due to stack overflow with otherwise bigger images
    
    revZD = image(100,100)
    setContext(revZD)
    sprite(img,50,50,100,100)
    setContext()
    print(imgname)
    saveImage(imgname.."revZD",revZD)
    img = readImage(imgname.."revZD")
    end

put the code above right underneath the section of code where you have your image read.

So in your current code it would read

img = readImage("Tyrian Remastered:Blimp Boss")
    w,h = spriteSize(img)
    if w > 100 or h > 100 then
    imgname = string.sub(debug.getinfo(1).source,tonumber(string.find(debug.getinfo(1).source,"readImage")+ string.len("readImage%(")),-1)
imgname = string.sub(imgname,1,string.find(imgname,'%"%)')-1)
 
    --revZD coding is due to stack overflow with otherwise bigger images
    
    revZD = image(100,100)
    setContext(revZD)
    sprite(img,50,50,100,100)
    setContext()
    print(imgname)
    saveImage(imgname.."revZD",revZD)
    img = readImage(imgname.."revZD")
    end

On a side note I am noticing a glitch with the outline at times it cuts corners right through the image.

@GenobiJuan - thanks for the suggested change, I’ll look at that

The reason it cuts corners may be that it is set to only take every 5th pixel, in an effort to cut down rendering time. If you set it to 1, it will use every point on the outline, but this may be slow. I know there are some images that still stuff up a bit, but I haven’t used it enough to figure out the problem yet.