How to detect from an image the shape for an physic object?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ellipticaldoor
Prole
Posts: 6
Joined: Sun May 05, 2013 2:56 pm

How to detect from an image the shape for an physic object?

Post by ellipticaldoor »

I want use complex shapes with the 2D physics engina that love2d has, right now I onlu can use simple shapes like a circle or a square. I want to draw something like a hand in an image and when it loads the engine has to detect the shape of the object.

Is this easely achiavable?
Relazy
Citizen
Posts: 51
Joined: Thu Jul 10, 2014 11:10 pm

Re: How to detect from an image the shape for an physic obje

Post by Relazy »

ellipticaldoor wrote:I want use complex shapes with the 2D physics engina that love2d has, right now I onlu can use simple shapes like a circle or a square. I want to draw something like a hand in an image and when it loads the engine has to detect the shape of the object.

Is this easely achiavable?
1. the 2d physics engine that love2d has is called Box2D, it supports polygons so complicated shapes are possible if you find that it isn't enough use love.math.triangulate
2. Translating image to a physics shape is quite complicated and the results aren't perfect either, so unless you have 50+ images it is not recommended.
How to go about it:
- get image data from the image.
- make a bit mask of data(iterate through each pixel), matrix with 1 as transparent and 0 as colored:
[1,1,1]
[1,0,1]
[1,1,1]
- produce shapes out of this(most likely polygons and hence use triangulation) and attach it to a body.
- circles are a whole different story.

Note:
If you are planing to use this for the main character or NPC's please consider making shapes manually as it is way easier and produces better results.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: How to detect from an image the shape for an physic obje

Post by vrld »

- produce shapes out of this(most likely polygons and hence use triangulation) and attach it to a body.
This is the complicated step, by the way. You can use the Marching Squares Algorithm for that. Note that you should simplify (remove points that area (nearly) on a line between two other points) your shape before triangulating it with love.math.triangulate.

In any case, it is probably faster to draw the shapes manually (for example with Inkscape) than to write the code that extracts them from the image.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: How to detect from an image the shape for an physic obje

Post by bobbyjones »

This is what I have used in the past if you are on windows than it will have an executable for you if you are on linux i think it runs in wine. All you will have to do is make a export template for love. https://www.codeandweb.com/physicseditor
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests