Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Box2D Collisions — Gideros Forum

Box2D Collisions

click2installclick2install Member
edited December 2013 in General questions
Ok so my platformer is coming along nicely and a lot quicker than I thought :) When my hero hits a Box2D object I am using Event.PRE_SOLVE to detect and stop the world from moving, change hero animation etc. If he then turns around to move off the object can I detect this with built in functions or do I need to maintain some variables myself to achieve this functionality? I am currently using variables but it is getting messy, hence the question.

NOTE: the world only moves when the hero is set to move left/right/up/down, it is not a continuous runner.

Thanks,

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @click2install if I understood you correctly, for that you should use Event.BEGIN_CONTACT and Event.END_CONTACT events, to determine when collision begun and when it ended.

    Additionally you can retrieve and save specific contact informations, like:
    function onCollision(e)
        --gives you b2.Contact object
        local contact = e.contact
        --which you can then use to check
        if contact:isTouching() then
            --the collision is still active
        end
    end
    http://docs.giderosmobile.com/reference/physics/b2Contact#b2.Contact
  • Thank you, that is what I am after. Don't know how I missed that when looking through API.
  • click2installclick2install Member
    edited December 2013
    @ar2rsawseen, turns out it didn't solve my issue. I have attached a stripped down project to illustrate the problem I am having.

    Use the left/right arrows to move the map.

    The moving object stops when it collides with another body, but (once box turns grey) you can then pass through that body unlimited times until you hit a different body.

    Also note that if you change the mover objects start position x to 150 it drops onto the body but then stays at that height until it hits another body before it falls to the ground. I realise I am not moving the map in the y-axis but I never am (in this project) and it still falls after hitting another body.

    If I hook PRE_SOLVE, POST_SOLVE, BEGIN_CONTACT, END_CONTACT, once the initial contact is made, none of these events are fired until contact is made with a different body.

    Any ideas?
    Thanks
    zip
    zip
    PhysicsTest.zip
    4K
  • Update: I am about to head out for a while but it looks like collision filtering is what I am after. I noticed that if I maintain the initial collision (holding down left or right on first collision) then PRE_SOLVE and POST_SOLVE both fire.

    Will take a closer look when I get back in.
  • ar2rsawseenar2rsawseen Maintainer
    edited December 2013 Accepted Answer
    @click2install unfortunately you are doing it wrong

    you can't move character like this, nor you can move map like this, it can break box2d world calculations, and thus break collisions, etc.

    you would need to move your character with applyForce, jump with applyLinearImpulse and let scene follow the character, by getting its coordinates on each enter frame and move the scene

    Here is how I would go about it:

    zip
    zip
    PhysicsTest2.zip
    4K

    Likes: chipster123

    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you I understand now.
Sign In or Register to comment.