Cool, I'll have a look. Thank you!
EDIT: I fixed the problem explained below by making Spiral enter the wall 1 pixel. I don't know why it's neccessary but now it works, so I'll assume it's a Hardon "feature" (lol).
By the way, vldr, I'd appreciate a lot if you could bring some light to my current problem. I'm detecting collisions with floor, ceiling and walls with this function (the most optimized I've been able to do so far), but you can skip it for now (I copied it just in case you want to check if it's wrong):
Code: Select all
--********************************************************
-- spiral:collidingWith
--********************************************************
function spiral:collidingWith(what) -- what can be "floor", "ceiling" and "walls"
local neighbors = spiral.bbox:_getNeighbors()
for _,shape in pairs(neighbors) do
for group, _ in pairs(shape._groups) do
if (group == what) then
if (what == "floor" and spiral:collidingWithFloor(shape)) then
return true
end
if (what == "ceiling" and spiral:collidingWithCeiling(shape)) then
return true
end
if (what == "walls") then
local wall = spiral:collidingWithWalls(shape)
if (wall) then return wall end
end
end
end
end
return false
end
Thus, when none of the negihbours belong to the group I'm checking, I return false. This works like a charm except for the walls collision, that gets walls at left side of the player perfectly but not always at its right side. The function I'm using for detecting walls is (you can skip this one as well and just have a look to the next chunk):
Code: Select all
--********************************************************
-- spiral:collidingWithWalls
--********************************************************
function spiral:collidingWithWalls(shape)
local spCX,spCY = spiral.bbox:center()
local spX1,spY1, spX2,spY2 = spiral.bbox:bbox()
local shCX,shCY = shape:center()
local shX1,shY1, shX2,shY2 = shape:bbox()
if (spiral.y + 20 < shY2 -- past ceiling
and spiral.y + spiral.height - 20 > shY1 -- past floor
) then
--print ("in vertical range")
if (spX1 <= shX2 and spX2 > shX2) then
spiral.x = shX2 - 10
if (spiral.velX < 0) then spiral.velX = 0 end
return "left"
end
if (spX2 >= shX1 and spX1 < shX1) then
spiral.x = shX1 - spiral.width + 10
if (spiral.velX > 0) then spiral.velX = 0 end
return "right"
else -- if right wasn't reached, I want to know how the player's and the shape's coordinates
print ("SPX1 ".. spX1.."\tSPX2 ".. spX2)
print ("SHX1 ".. shX1.."\tSHX2 ".. shX2)
end
end
end
The insteresting part is just this (don't skip this one now, hehe):
Code: Select all
if (spX2 >= shX1 and spX1 < shX1) then
spiral.x = shX1 - spiral.width + 10
if (spiral.velX > 0) then spiral.velX = 0 end
return "right"
else -- if right wasn't reached, I want to know how the player's and the shape's coordinates
print ("SPX1 ".. spX1.."\tSPX2 ".. spX2)
print ("SHX1 ".. shX1.."\tSHX2 ".. shX2)
end
Sometimes it detects the wall at right correctly, but sometimes not, so I put those two prints to get the values to try to debug the problem. Surprisingly, the values I get should get the colision and not print anything, but they don't. You can see it in the attachment. Explanation:
SPX1, SPX2 = Spiral (player) bounding box x1 and x2.
SHX1, SHX2 = the shape's bounding box x1 and x2. It's a wall shape (otherwise the message wouldn' be printed).
In the game window you can see "WALLS: false". This prints "left" or "right" if a wall is detected.
As you can see in the picture, the player's bounding box is next to the right wall, spX2 == shX1 (=1024) and spX1 < shX1. With these values, it should enter the "if", not the "else", so I wonder what's wrong.
I've tried with many alternatives (getting collisions with ALL shapes from the "walls" group, for instance), but the result is the same :S
I've attached the project if you need to have a look at the code.