Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
box2d (LiquidFun) - get shape (polygon) from fixture — Gideros Forum

box2d (LiquidFun) - get shape (polygon) from fixture

Hello everybody.
In certain situations during game, I'd like to get the shape (polygon vertices) of fixture. There's no such method in LiquidFun library.
Any suggestions?

Likes: antix

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • MoKaLuxMoKaLux Member
    edited December 2020
    Assuming you have a class (a player class, ...) you can access its shape or anything using self.
    -- sprite & bitmap
    self.bitmap = Bitmap.new(self.spritesheetimgs[1]) -- starting bmp texture
    -- body
    self.body = xworld:createBody { type = b2.DYNAMIC_BODY }
    self.body.lives = 3
    -- body fixture
    self.shape = b2.CircleShape.new(0, 0, 16) -- (centerx, centery, radius)
    self.fixture = self.body:createFixture {
    	shape = self.shape,
    	density = xdensity or 1,
    	restitution = xrestitution or 0,
    	friction = xfriction or 1
    }
    Hope this helps.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • In my case self.shape is a polygon. The problem is that I'm not sure if self.shape follows body rotation in game loop. Even if it does, I would need polygon in form of {x1,y1,x2,y2....xn,yn}, but type(self.shape) returns 'userdata' - probably kind of C pointer. Useless for me.
    thanxx anyway
  • MoKaLuxMoKaLux Member
    edited December 2020
    I believe the shape follows the rotation of the body.
    A couple of things you can try:
    - try to store your {x1,y1,x2,y2....xn,yn} is a self.table so you can access it
    - or you could try unpack()
    stage:addEventListener(Event.ENTER_FRAME, function()
    	-- edit the step values if required. These are good defaults!
    	world:step(1/60, 8, 3)
    	ball:setPosition(ball.body:getPosition())
    	ball:setRotation(math.rad(ball.body:getAngle())) -- ROTATION HERE
    end)
    https://wiki.gideros.rocks/index.php/B2.Body
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • sphinxxsphinxx Member
    edited December 2020
    ok, let's assume the shape is updated according to body rotation/position.
    But still I don't know the data structure of shape ball.poly for conversion to {x1,y1,x2,y2...} later in onEnterFrame loop.

    local poly = {-20,-20, 40,-20, 50,20, -25,30}
    ball.body = world:createBody{type = b2.DYNAMIC_BODY}
    ball.poly = b2.PolygonShape.new()
    ball.poly:set(unpack(poly))
    ball.fixture = ball.body:createFixture{shape = ball.poly, density = 1.0, friction = 0.1, restitution = 0.2}

    ...anyway, I already did it in other way.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.