# Generate a physics body from a sprite [Best solution]

[EDIT]
Hello !

After 3-4 days of search, I have found a very good solution for make physics body from a sprite.
The solution of @Ignatz is interesting but for make bigger simulation, that is not enough.

I share my code when my class was clean.

Are you trying to find the angle of a vector? If so use this function:

``````function angleOfPoint( pt )
local x, y = pt.x, pt.y
if angle < 0 then angle = 360 + angle end
return angle
end
``````

Excuse me, my word is bad. I have rename angle by corner.
I have a array of points that describes contours of a polygon, and I try to find corner in this array.

Simple example:

``````
XXXXXXXXXXX
X         X
X         X
XXXXXXXXXXX

I want found O :

OXXXXXXXXXO
X         X
X         X
OXXXXXXXXXO
``````

For the moment, I have not found a algorithm to resolve my problem.
So, I use algorithm of Radius Simplify to reduce number of points in my array, but it’s not enough.
I have try algorithm of DouglasPeucker (for the same goal) but my version doesn’t work.

— Comment deleted

@HyroVitalyProtago Have you looked at the code by @Ignatz to generate a physics body from a sprite image? http://twolivesleft.com/Codea/Talk/discussion/2297/utility%3A-create-a-physics-object-that-follows-the-exact-outline-of-your-image

Thanks a lot @Simeon.

Hmmh, Algorithm of @Ignatz is interesting but not enough for me…
My algorithm is slower than his, but my solution is more efficient and that could be better if I find how found corner in array of points.

``````Exemples :
His solution for "Tyrian Remastered:Blimp Boss" : 57 points
My solution : 33 points
Future solution not interesting for this case.

His solution for "Space Art:Asteroid Large" : 62 points
My solution : 39 points
My "future" solution : ~8 points
``````

@Simeon, @Ignatz, if you have any idea

With DouglasPeucker :

``````"Tyrian Remastered:Blimp Boss" : 28 (~31 (if you want more precision) )

"Space Art:Asteroid Large" : 14
My "future" solution : ~8 points
``````

Combinaison of RadiusSimplify & DouglasPeucker =>The Best Solution !!!

``````"Tyrian Remastered:Blimp Boss" : 20
"Space Art:Asteroid Large" : 9
``````