Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Box2d star shape (and other shapes) - Gideros Forum

Box2d star shape (and other shapes)

Hi all,

I'm having an image of a star that i would like to behave just as the circles in the Collision Filtering example project, only it has the shape of a... well... star, not rolling circles. Maybe there is a way to make a box2d shape out of a sprite?

Can't make it a PolygonShape since max 8 vertices are allowed, nor a ChainShape since it must be a DYNAMIC_BODY.

Comments

  • hgy29hgy29 Maintainer
    Do you need to check for collision the gap between the branches of your star ? if not then (assuming your star as less than 8 branches) you can use polygon shape (pentagon, hexagon, etc) for box2D. if there are more than 8 branches, then a circle collision shape would be a good approximation anyhow
  • saeyssaeys Member
    Well it's a five-pointed star, and my thought is to have several of those lying piled together. So in that case I would like to be able to check for collision between one star's branch and an other star's gap. Otherwise it may look strange in some cases.

    So it isn't possible to check for collision at a sprite's boundaries or something like that?

    Tested also to draw the PolygonShape vertices from edge to edge like a pentagram, but alas - Polygon should be convex and should have a CCW winding order, whatever that means. :smile:
  • saeyssaeys Member
    Found this: https://stackoverflow.com/questions/8543105/is-there-any-way-to-draw-the-concave-polygon-in-box2d-and-detect-the-collision-w

    If this may be a solution, ie making five triangle shapes instead of the five pointed star and group the together in the body somehow, I'm not sure how to implement it in the code.
    local function createStar(texture, x, y, filter)
    	local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = x, y = y}}
     
    	local shape = b2.PolygonShape.new()
    	shape:set(29,0, 11,52, 56,20, 0,19, 46,52) -- polygon drawn CCW, but crosses itself creating an error
    	body:createFixture{shape = shape, density = 1, restitution = 0.7, friction = 1.1, filter = filter}
     
    	local sprite = Bitmap.new(Texture.new(texture, true))
    	sprite:setAnchorPoint(0.5, 0.5)
    	stage:addChild(sprite)
    	actors[body] = sprite
    end
     
    local blueFilter = {categoryBits = 2, maskBits = 3}
     
    createStar("star.png", 200, 0, blueFilter)
  • saeyssaeys Member
    Got it. Didn't expect it was possible to have several fixtures on the same body. So I created several fixtures in a row and changed the polygon shape before each fixture creation, into five triangles, forming a star:
    	local shape = b2.PolygonShape.new()
    	starFixtureProperties @ |shape = shape, density = 1, restitution = 0.5, friction = 1|
    	shape:set(29,0, 39,16, 19,16)
    	body:createFixture{starFixtureProperties}
    	shape:set(56,20, 43,33, 39,16)
    	body:createFixture{starFixtureProperties}
    	shape:set(46,52, 28,44, 43,33)
    	body:createFixture{starFixtureProperties}
    	shape:set(11,52, 14,33, 28,44)
    	body:createFixture{starFixtureProperties}
    	shape:set(0,19, 19,16, 14,33)
    	body:createFixture{starFixtureProperties}
    May there be any problems with this solution that I haven't seen yet?

    Also, the polygons were drawn CW (clockwise). If they were drawn CCW, it crashes with this slightly confusing error message:
    Polygon should be convex and should have a CCW winding order.
  • saeyssaeys Member
    Looks interesting, thank you!
  • antixantix Member

    Likes: saeys

    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing | Ninja Fruit Master
    +1 -1 (+1 / -0 ) Share on Facebook
  • totebototebo Member
    Interestingly I have seen that the latest Tiled supports polygons, so might be worth a look:

    https://www.mapeditor.org

    If you need something less tiley, I can second RUBE. I've used it since my first game and it's unbeatable as a Box2D editor.

    Likes: Apollo14

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.