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
You can use event.contact information like
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)???
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?
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!
You can check this tutorial: http://appcodingeasy.com/Gideros-Mobile/Animating-Box2d-objects
where collision detection happens here:
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