Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Issues detecting direction of collision — Gideros Forum

Issues detecting direction of collision

ClaudioBAClaudioBA Member
edited February 2013 in Game & application design
Hello, first time using Gideros! (and building apps for mobile devices)
im trying to get the collision and the direction of it but it doesnt work...
this is part of my code:

function GCchar:onBeginCollision(event)
local bodyA, bodyB = event.fixtureA:getBody(), event.fixtureB:getBody();

if self:collision(bodyA.xSprite) == "B" then
print("Down")
end

end

function Sprite:collision(sprite2)

-- self bottom < other sprite top
if self:getY() + self:getHeight() < sprite2:getY() then
return "T";
end
-- self top > other sprite bottom
if self:getY() > sprite2:getY() + sprite2:getHeight() then
return "B";
end
-- self left > other sprite right
if self:getX() > sprite2:getX() + sprite2:getWidth() then
return "R";
end
-- self right < other sprite left
if self:getX() + self:getWidth() < sprite2:getX() then
return "L";
end

return "N";
end

is this approach correct???
anyone has done this??

Thanks!

Comments

  • atilimatilim Maintainer
    Hi @ClaudioBA, welcome here :)

    You can use event.contact information like
    local worldManifold = event.contact:getWorldManifold()
    print(worldManifold.normal.x, worldManifold.normal.y)
  • ClaudioBAClaudioBA Member
    edited February 2013
    Thanks for the fast response!

    There is a way to detect the bodies making contact?
    for example, my character is walking against a wall (made by a pile of squares), and i get 2 lectures.... i supose one of the lectures is the square of the wall and the other is the square under the wall (on the floor)???


  • atilimatilim Maintainer
    edited February 2013 Accepted Answer
    After creating your bodies, set a field like "type" (or any other name as you want):
    local body1 = world:createBody(....)
    body1.type = "wall"
     
    local body2 = world:createBody(...)
    body2.type = "character"
    And then you can check
    local bodyA = event.fixtureA:getBody()
     
    if bodyA.type == "wall" then
        -- this is wall
    else if bodyA.type == "character"
        -- oh no, it's character
    end
  • Thanks, i'll try that!!!

  • Another question related to contact....
    if i have my character against a wall at his right, how can i get the body at his right if they dont collide at the touch event?
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013
    @ClaudioBA did not really understand the problem, but if the bodies are not colliding, but are near, you could use b2.World:queryAABB(lowerx, lowery, upperx, uppery) to get the fixture and then the body, where lowerx, lowery, upperx, uppery is bounding box where to check for box2d bodies
  • ClaudioBAClaudioBA Member
    edited February 2013
    sorry my English is not very good...
    i was trying to get the body colliding with the character, but after the onBeginCollision event take place....

    EDIT: i'm doing it right now, but doing something like this:

    charH is char Height

    if ( bodyBY < bodyAY+charH/2 and bodyBY+32> bodyAY+charH/2) then

    is pretty ugly and im sure there is a better way to acomplish this

    Thanks!
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013
    if onBeginCollision event is raised, then it means that bodies are already colliding, so I can't really understand where the problem is :)

    You can check this tutorial: http://appcodingeasy.com/Gideros-Mobile/Animating-Box2d-objects

    where collision detection happens here:
    --define begin collision event handler function
    function scene:onBeginContact(e)
        --getting contact bodies
        local fixtureA = e.fixtureA
        local fixtureB = e.fixtureB
        local bodyA = fixtureA:getBody()
        local bodyB = fixtureB:getBody()
     
        --check if first colliding body is a ball
        if bodyB.type and bodyB.type == "ball" then
            --switch animation using our created function
            bodyB.object:switchAnimation()
        end
    end
  • i mean i need to check contact after another action happens, not collition, so i need to identify the bodies in actual contact with it
  • oh ok I think I get it now.
    Well there are two ways, you can implement two methods for Event.BEGIN_CONTACT and Event.END_CONTACT

    inside first one you can set some value self.isColliding = true and on second event set it to false. Then any time you can check if self.isColliding then.

    Second not tested but should work, is to retrieve contact object from contact event and store it for later use
    --define begin collision event handler function
    function scene:onBeginContact(e)
       self.contact = e.contact
    end
     
    function scene:isColliding()
        return self.contact:isTouching()
    end
Sign In or Register to comment.