[lua] local function collisionEvent(event)
if event.phase == “began” then
local obj1 = event.object1;
local obj2 = event.object2;
if ((obj1.type == “player”) and (obj2.type == “wall”)) or ((obj1.type == “wall”) and (obj2.type == “player”)) and (instance.isAligned) then
local other;
if obj1.type == “player” then
other = obj2;
else
other = obj1;
end
if (other.x == instance.x) and (other.y < instance.y) then
instance.collides[DIR_UP] = true;
print(“collides above”);
end
if (other.y == instance.y) and (other.x > instance.x) then
instance.collides[DIR_RIGHT] = true;
print(“collides right”);
end
if (other.x == instance.x) and (other.y > instance.y) then
instance.collides[DIR_DOWN] = true;
print(“collides below”);
end
if (other.y == instance.y) and (other.x < instance.x) then
instance.collides[DIR_LEFT] = true;
print(“collides left”);
end
end
end
end[/lua]
The above code is what I am using to detect collisions of a dynamic physics body and a bunch of static sensor walls in a maze game. However, each frame when a collision occurs, it seems to be only detecting the first collision that occurs. For example, in a corner only one of the two collisions is actually picked up. What’s the best way to allow detection of multiple collisions at once in this case?
since the player was shaking back and forth between the walls earlier and I couldn’t get that to stop. I have a feeling there’s a better way but I couldn’t find one. And yes, the walls are static and the player is dynamic
Basically, the concept is that you’re using that extra sensor element, which always “leads” in front of the direction the player is going, to detect when it hits a wall. When it does, you know that the player can’t move any further in that direction, so you stop its linear velocity and prevent any further movement in that direction. Other directions are, of course, still allowed, until the sensor contacts another wall, and then you prevent further movement in that direction.