Problem with polygon based physic body position

Brent , after the user “finalizes” the drawing of the polygon.

Brent correct me if I’m wrong, but physics polygon shapes have to be convex.  So you would need to make sure all your points are convex or you would have to have multiple physics bodies.  If you have a list of your vertices, you should be able to create a physics shape that works out for you instead of depending on physics to auto-shape it.

Rob

Well, how to make that a polygon created correctly?

I haven’t run this actual code, but @horacebury (Corona utility guru :slight_smile: ) has created some modules which may do exactly what you need:

http://developer.coronalabs.com/code/shape-creation-use-addbody

http://developer.coronalabs.com/code/simple-polygon-triangulation

And yes, custom physics shapes must be convex at all point bends, no exceptions.

Brent

Brent , it will not work with polygon because I did not know where his center. This will only work with a shape property. Shape of the object relative to the center does not shift automatically as opposed to the type of object poligon.

The only thing you can do is to disable the automatic alignment of form object type poligon, or how to calculate the coordinates of the center of that object.

Thanks

Hi @GamingStudio17,

Can you gather the points of the drawn polygon, calculate the center using center-finding math on an irregular polygon, and then shift those vertices relative to the new center point?

Brent, no

Hi @GamingStudio17,

Can you explain in more detail why not? If you look at the attached diagram (theoretical use case), the blue triangle would be the one that the user “draws”. The center point would be approximately where the blue “X” is. But, of course, when you draw the bounding rectangle around the object (pink rectangle), the center point of that bounding rectangle is at a different point compared to the triangle. However, if you calculate the actual center point of the blue triangle using the proper math, you know the relation between that point (in coordinates) versus the center point of the bounding rectangle. So, you can use that offset X/Y on each of the corner points of the physics shape, which should put the polygon body correctly oriented around the object.

Brent

triangle.png

I did it, thanks. Find the minimum and maximum x and y. Then, add them up and divide, and this will be the coordinates of the center of the polygon.

local new_x = (max_x+min_x)*0.5

local new_y = (max_y+min_y)*0.5

Hi @GamingStudio17,

You may want to investigate the math in the link below… I don’t think that your formula will find the actual center of irregular polygons. Inspecting the irregular triangle that I pasted, your math would find the center of the “bounding rectangle” but not the actual center of the triangle. Anyway, check this post out:

http://stackoverflow.com/questions/5271583/center-of-gravity-of-a-polygon

Best regards,

Brent

I Already Did, thank you. Everything works fine.

Another question, how to make sure that when a message is displayed

WARNING: Polygon could not be generated. The polygon outline is invalid, possibly due to holes or self-intersection.

Then Polygon is removed.

Hi @GamingStudio17,

I don’t have any specific advice on this, unfortunately. Corona will not create an invalid polygon (hence the warning message when you try to), but there’s no way to “pre-detect” that using the warning message. You may refer back to the module I linked you to, which (I think) figures out if a shape is “valid” or not, correct? If it doesn’t, you’ll need to locate some algorithm to step through your vertices and ensure that the shape is valid in two ways: 1) that it’s just one valid polygon (doesn’t result in self-intersection), and 2) the vertices yield only convex bends for proper physical behavior in Box2D.

Best regards,

Brent

This looks like a bug.  Can you file a bug report using the “Report a bug” link above.  The code above will suffice as the demo project needed.  This way the engineer’s can take a look at it.

Thanks

Rob

Thank you Rob. I reported.

Can you post the bug ID back here for later reference.

Thanks

Rob

where can i find my bug ID?

It should have been in the email you got after filing it.  It would have also been on the last screen after submitting the bug I think.

Its 28033 for reference. 

Thanks

Rob

Confirm that it is a serious mistake. Even if in the example with a star, turn on physics, it will be seen that it does not correspond to objects. Please correct.

And another bug: WARNING: Polygon could not be generated. The polygon outline is invalid, possibly due to holes or self-intersection.

Should be created because the physical form is created.