Now about my question. I know that this has been asked so much here in the forums. I have searched and found some really good examples about COLLISIONS and stuffs. I have been coding this for about 2 days and I can't seem to get it or solve it on my own. I wanted to create a little game which the player can move in all four directions (up, down, left, right) and the game is divided into stages. Each stage has its own .png picture file as the map (and a transparent bounding boxes for collision. each box is 32x32 big and also the player). The camera view is on the top of the player, by the way. It's zelda-like view.
But here's where my problem is, I can't solve the collision detection between the player and the wall! I have seen many examples but I just don't get it. Could someone explain it to me? I have plenty of test codes that I have created and all is not working as it should:
Code: Select all
function check_touch(a, coll_type)
--CHECK COLLISION BETWEEN PLAYER AND GROUND
if ( player.getHeight() + player.getY() ) == a.getY() then
player.isTouchingFloor = true
else
player.isTouchingFloor = false
end
--CHECK COLLISION BETWEEN PLAYER AND CEILING
if player.getHeight() == ( a.getHeight() + a.getY() ) then
player.isTouchingCeiling = true
else
player.isTouchingCeiling = false
end
--CHECK COLLISION BETWEEN PLAYER AND WALL
if ( player.getWidth() + player.getX() ) == a.getX and
player.getX() == ( a.getWidth() + a.getX() ) then
player.isTouchingWall = true
else
player.isTouchingWall = false
end
end
function colliding(a, b)
if (a.height + a.y) == b.y or
a.height == (b.height + b.y) or
(a.width + a.x) == b.x or
a.width == (b.width + b.x) then return true
else return false
end
end
function colliding(a, b)
for grid_y, value in ipairs(tiletable) do
for grid_x, tile in ipairs(value) do
if tile == 1 and a + tileW > (grid_x - 1) * b.width and
b + b.height > (grid_y - 1) * b.height and
a < (grid_x - 1) * tileW + tileW and
y < (grid_y - 1) * tileH + tileH
then col = true end
end
end
return col
end
function colliding (a, b)
for grid_y, v in ipairs(stage_level) do
for grid_x, tile in ipairs(v) do
if tile == 1 then
if
(a.height + a.y) == (b.height * grid_y) or
a.y == (b.height * grid_y) or
(a.width + a.x) == 1+(b.x * grid_x) or
a.width == (b.width + b.x) * grid_x
then return true
else return false
end
end
end --END INNER LOOP
end --END OUTER LOOP
end
function check_box_coll(x, y)
local col=false
for grid_y, v in ipairs(stage_level) do
for grid_x, tile in ipairs(v) do
if tile == 1 and x + stage_level.width > (grid_x - 1) * stage_level.width and
y + stage_level.height > (grid_y - 1) * stage_level.height and
x < (grid_x - 1) * stage_level.width + stage_level.width and
y < (grid_y - 1) * stage_level.height + stage_level.height
then col = true end
end
end
end
function check_passable_tile(a, b, dir)
local coll = false
if dir == "down" then
for grid_y, index in ipairs(b) do
for grid_x, tile in ipairs(index) do
if tile == 1 then
if (a.height + a.y) == ((grid_y - 1) * (b.height * 6)) then
col = true
return col
end
end
end
end
end
if dir == "up" then
for grid_y, index in ipairs(stage_level) do
for grid_x, value in ipairs(index) do
if value == 1 and a.y == (grid_y * b.height) + (b.height * grid_y) then
return true
end
end
end
end
if dir == "left" then
for grid_y, index in ipairs(stage_level) do
for grid_x, value in ipairs(index) do
if value == 1 and a.x == (grid_x * b.width) + (b.width * grid_x) then
return true
end
end
end
end
if dir == "right" then
for grid_y, index in ipairs(stage_level) do
for grid_x, value in ipairs(index) do
if value == 1 and (a.width + a.x) == (grid_x * b.width) + (b.width * 4) then
return true
end
end
end
end
end
Code: Select all
map = {
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}
}
1 => box / impassable tile
0 => empty / player can walk on it
Thank you
EDIT
I created a love file without any collision to it. How do I add collisions between a player and wall? I tried many things, followed many tutorials but I can't seem to get it right.