Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Error using VertexHelper — Gideros Forum

Error using VertexHelper

DikkesnoekDikkesnoek Member
edited October 2012 in General questions
Hello my friends,

I am trying to create a PolygonShape like this:
local landingGround = levelSelf.world:createBody({})
landingGround.name = "landingGround"
local polygonShape = b2.PolygonShape.new()
polygonShape:set(40.0, -163.0, 186.5, -95.0, 478.5, -82.0, 478.5, -202.0, 38.5, -163.5)
 
landingGround:createFixture({shape = polygonShape, density = 0})
I will get an error: "Polygon should be convex and should have a CCW winding order". I've copied
the values directly from VertexHelper (see the attached screenshot). As you see, the Polygon is
convex. Any ideas?

Regards,

Marc

Comments

  • It seems that coordinates are clock wise, you need to use counter clockwise order.
    Just pass them in reverse order. ;)
  • DikkesnoekDikkesnoek Member
    edited October 2012
    Thanks. Weird that VertexHelper will generate the data in CW order.
    When I reverse the vertices:

    polygonShape:set(38,-163, 478,-202, 478,-82, 186,-95, 40,-163)

    I will get the same error.

    Marc
  • atilimatilim Maintainer
    edited October 2012
    Remove one of the (40.0, -163.0) or (38.5, -163.5). They are very near to each other and make the polygon concave.

    Edit: If removing one of these vertices doesn't solve the problem, then pass them in reverse order. :)
  • Thanks Atilim. I am using the game template. I think that I first
    need to modify some code to let it work.

    Regards,

    Marc
  • DikkesnoekDikkesnoek Member
    edited October 2012
    Is there any additional information how to use the Polygonshape with
    more vertices? I've got it work but not with the coordinates generated
    by VertexHelper. I have to invert the Y values and rearrange the sequence
    to get it work. Also which position values do you give a Polygonshape's
    body?

    Marc
  • @Dikkesnoek

    all the coordinates for PolygonShape are relative, it means you can, for example, select one of the coordinate as 0, and provide other accordingly to the distance from this 0 coordinate. Then By setting position of the body that wears this PolygonShape, you can move it and position where you need it. It's similar using Shape object in Gideros.

    Can you post some code or/and screenshot how it looks for you now? :)
  • DikkesnoekDikkesnoek Member
    edited October 2012
    Here it is. It's a test. As you can see in the first screenshot above I want to
    create a shape on top of a part of the rock. I test on a 960x640px resolution.
    When I open the picture in VertexHelper (I don't know if there is an other
    to to do this) and draw the shape I will get the following coordinates:

    38,-163,
    478,-202,
    477,-82,
    186,-95

    As I remember from Cocos2D and Box2D, that vertices will be specified from
    the center of the picture. (0, 0) will be at (480, 320). To get it work in Gideros
    you will need to invert the Y-coordinates and reorder it to meet the CCW rule:

    478,202,
    38,163,
    186,95,
    477,82

    This is not really clear to me. Also how to position the shape. This is what I
    did:
    local body = levelSelf.world:createBody{type = b2.STATIC_BODY}
    body:setPosition(480, 320)	
    shape = b2.PolygonShape.new()
    shape:set(478,202, 38,163, 186,95, 477,82)	
    body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 1}
    See the result in the attached screenshot.

    I think that the use of Physics is still quiet complicated to a lot of people. The
    manuals are still not ready on this point. Is it an idea (I wan't to help if possible)
    to create a graphical example of a screen and show what a world, body, fixture,
    shape, etc. is. This could make it very clear.


    Regards,

    Marc



    Screen Shot 2012-10-04 at 09.20.32.png
    970 x 649 - 443K
Sign In or Register to comment.