Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with polygon based physic body position
Started by rustam Nov 16 2013 06:26 AM

35 replies to this topic
[TOPIC CONTROLS]
Page 2 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#26

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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?



[TOPIC: post.html]
#27

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Brent, no



[TOPIC: post.html]
#28

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#29

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

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.

 

Attached File  pppp.jpg   7.83KB   8 downloads

 

local new_x = (max_x+min_x)*0.5
local new_y = (max_y+min_y)*0.5


[TOPIC: post.html]
#30

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#31

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

I Already Did, thank you. Everything works fine.



[TOPIC: post.html]
#32

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

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.


[TOPIC: post.html]
#33

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#34

jacques1

[GLOBAL: userInfoPane.html]
jacques1
  • Contributor

  • 318 posts
  • Corona SDK

Would be great if we can manually switch off the polygon auto-centering, it's a huge pain and kind of defeats the point of setting vertices when they all get auto-shifted by the auto-centering. Having to calculate the center of the polygon then shift it back to where you originally want it is ridiculous. For example I'm calculating the vertices of a wall then using those to draw a shadow using a polygon. However, since the polygon auto-centers itself the shadow never ends up where it's supposed to as it ends up auto-centering itself to the wall and not remaining where it's supposed to be. Perhaps i missed something but it's really strange and not practical to have polygons auto-center themselves.



[TOPIC: post.html]
#35

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Would be great if we can manually switch off the polygon auto-centering, it's a huge pain and kind of defeats the point of setting vertices when they all get auto-shifted by the auto-centering. Having to calculate the center of the polygon then shift it back to where you originally want it is ridiculous. For example I'm calculating the vertices of a wall then using those to draw a shadow using a polygon. However, since the polygon auto-centers itself the shadow never ends up where it's supposed to as it ends up auto-centering itself to the wall and not remaining where it's supposed to be. Perhaps i missed something but it's really strange and not practical to have polygons auto-center themselves.

I already wrote a polygon can be easily aligned. But thanks to the auto centering, cent polygon always correct.



[TOPIC: post.html]
#36

jacques1

[GLOBAL: userInfoPane.html]
jacques1
  • Contributor

  • 318 posts
  • Corona SDK

@ gamingstudio17, hi, Not sure what you're on about, I was referring to the general issue of polygons auto-centering.

Anyways, @brent, if corona can simply give us the option to switch off auto-centering. I don't actually even see the point of polygons auto-centering in the first place.




[topic_controls]
Page 2 of 2 1 2
 
[/topic_controls]