Quick Links: Download Gideros Studio | Gideros Documentation | Ultimate Guide | Gideros Development Center
How to get collision in box2d? - Gideros Forum

How to get collision in box2d?

HolonistHolonist Member
edited March 9 in General questions
Hi,

In box2d the Debug Draw is highlighting objects that are colliding.
How can I get that information in the code? (test if object A collides with object B)

I want to apply a modification on an object, based on the other object it collides with.

Edit: Also, I need to access the sprite which the body is attached to.

Greetings

Comments

  • HolonistHolonist Member
    edited March 9
    Found out how to detect collision by looking at one of the tutorials.

    To access the sprite, you can actually set a back-reference to it in the body.
    ball.body = self.physics:createBody{type = b2.DYNAMIC_BODY}
    ball.body:setPosition(ball:getPosition())
    ball.body:setAngle( ball:getRotation() * math.pi/180 )
    ball.body.parent = ball
    To get the sprite, you can:
    - get fixture
    - get body from fixture
    - get parent from body, which we did set above
    self.physics:addEventListener(Event.BEGIN_CONTACT, function(e)
    local fixtureA = e.fixtureA
    local fixtureB = e.fixtureB
    local bodyA = fixtureA:getBody()
    local bodyB = fixtureB:getBody()

    bodyB.parent:setRotation(bodyA.parent:getRotation())
    end)
    Note that this is not usually needed if you do something with physics!

    But it my case it was. Now if someone wants to know why, here's an awful video :'D
    The guy's body is a ball, and the sprite is not synced with the ball's rotation (obviously). But the guy's sprite orientation should be synced with the surface's rotation.



    Just for fun, what happens IF the guy's rotation is synced with his body :D
  • totebototebo Member
    Welcome to the world of physics! I use collision data all the time, and, in fact, built an entire game dependent on collisions; Scrappy Cat.

    It takes a little getting used to, because you get collisions for all fixtures at the same time. It takes a little filtering to use them.

    Aye: Holonist, antix

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