So I did some more work on this and discovered that when colliding with a corner, it’s actually detecting the following tiles:
19:50:29.287 x=304, y=144
19:50:29.287 x=304, y=112
19:50:29.287 x=304, y=80
So basically if the following is a corner with the “O” being the player and each “[]” a tile with the arrow indicating the player’s direction:
[]
–>O []
[] []
Only collisions with the three tiles on the right are detected, not the one underneath. So now I’m completely lost as to how I can even figure out that that tile is there before the player literally goes right through it.
If it helps here’s my new code with the queue system:
[lua]local function collisionEvent(event)
local obj1 = event.object1;
local obj2 = event.object2;
local other;
if obj1.type == “player” then
other = obj2;
else
other = obj1;
end
if (event.phase == “began”) and (instance.isAligned) and (other.type == “wall”) then
table.insert(collidingTiles, other);
end
end
local function frameEvent(event)
if instance.direction > DIR_NONE then
for _, v in pairs(collidingTiles) do
if (v.x == instance.x) and (v.y <= instance.y) then
instance.collides[DIR_UP] = true;
end
if (v.y == instance.y) and (v.x >= instance.x) then
instance.collides[DIR_RIGHT] = true;
end
if (v.x == instance.x) and (v.y >= instance.y) then
instance.collides[DIR_DOWN] = true;
end
if (v.y == instance.y) and (v.x <= instance.x) then
instance.collides[DIR_LEFT] = true;
end
end
if (instance.isAligned) and (not instance.collides[instance.desiredDirection]) then
instance.direction = instance.desiredDirection;
end
if not instance.collides[instance.direction] then
if (instance.direction == DIR_UP) then
instance.y = instance.y - 2;
elseif (instance.direction == DIR_RIGHT) then
instance.x = instance.x + 2;
elseif (instance.direction == DIR_DOWN) then
instance.y = instance.y + 2;
else
instance.x = instance.x - 2;
end
collidingTiles = {};
end
end
if ((instance.x - 16)%32 == 0) and ((instance.y - 16)%32 == 0) then
instance.isAligned = true;
else
instance.isAligned = false;
end
end[/lua]
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.