Collision not working
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Collision not working
Hi there,
i have followed some various tutorials to get tile maps + collision to work, however it does not work 100% properly yet, and i don't know why.
It's hard to explain what the problem is, so just run the .love file and you will understand! If anyone of you is kind enough to take a look at my code i would be very happy!
Thanks!
i have followed some various tutorials to get tile maps + collision to work, however it does not work 100% properly yet, and i don't know why.
It's hard to explain what the problem is, so just run the .love file and you will understand! If anyone of you is kind enough to take a look at my code i would be very happy!
Thanks!
- Attachments
-
- game.love
- (400.79 KiB) Downloaded 325 times
Re: Collision not working
You seem to already use a more oriented Tile engine for Quads. Do you already studied Kikito's nice Tile Tutorial?
https://github.com/kikito/love-tile-tutorial/wiki
https://github.com/kikito/love-tile-tutorial
EDITED: Ah, I understand your problem with the tutorials. Kikito's tutorial don't yet focus collision with that tile engine. I'm not sure yet what is failling there (if bad detection or bad offset print). Look, an advice of something that really helps a lot. Start printing all map/player variables you can to screen to start debug. I do it a lot and it really it's a time-saver! I don't think it's strange the fail in right obstacle (fits with tilesize) but left collision is strange.
https://github.com/kikito/love-tile-tutorial/wiki
https://github.com/kikito/love-tile-tutorial
EDITED: Ah, I understand your problem with the tutorials. Kikito's tutorial don't yet focus collision with that tile engine. I'm not sure yet what is failling there (if bad detection or bad offset print). Look, an advice of something that really helps a lot. Start printing all map/player variables you can to screen to start debug. I do it a lot and it really it's a time-saver! I don't think it's strange the fail in right obstacle (fits with tilesize) but left collision is strange.
Re: Collision not working
I actually did, and the result was that when the player approaches the left obstacle all the code after the print line is not running and i got a weird error:coffee wrote:You seem to already use a more oriented Tile engine for Quads. Do you already studied Kikito's nice Tile Tutorial?
https://github.com/kikito/love-tile-tutorial/wiki
https://github.com/kikito/love-tile-tutorial
EDITED: Ah, I understand your problem with the tutorials. Kikito's tutorial don't yet focus collision with that tile engine. I'm not sure yet what is failling there (if bad detection or bad offset print). Look, an advice of something that really helps a lot. Start printing all map/player variables you can to screen to start debug. I do it a lot and it really it's a time-saver! I don't think it's strange the fail in right obstacle (fits with tilesize) but left collision is strange.
Code: Select all
attempt to concentate global 'leftTileNo' (a nil value)
Re: Collision not working
If I'm not wrong and for what I quickly checked you don't have any left/right key control to don't let the player go out of the map. I believe could be something like that doing the nil's values or in text display not yet declared strings.qrux wrote:
I actually did, and the result was that when the player approaches the left obstacle all the code after the print line is not running and i got a weird error:(i changed the local variables to global to be able to print them)Code: Select all
attempt to concentate global 'leftTileNo' (a nil value)
A good thing you could also implement is a routine that convert the mouse position to respective tile map x/y and tile number under.
I have in my experiences a routine that I think you can easily adapt to your game. With that you can start checking what are the map coordinates and map values under cursor (by checking in your case TileTable [mouse_tile_x][mouse_tile_y])
Code: Select all
function mouse2tile () -- retuns the tile under cursor
mouse_x = love.mouse.getX()
mouse_y = love.mouse.getY()
mouse_tile_x = map.x + math.floor((mouse_x) / tile.w)
mouse_tile_y = map.y + math.floor((mouse_y) / tile.h)
if mouse_tile_x <= map.width and mouse_tile_x > 0 and mouse_tile_y <= map.height and mouse_tile_y > 0 then
return mouse_tile_x, mouse_tile_y
else
mouse_tile_x = 0 -- I use 0 to transform "nil" errors in a non-problematic value and use it to know that cursor is outside any area in the map
mouse_tile_y = 0
return mouse_tile_x, mouse_tile_y
end
end
Re: Collision not working
I implemented some code i think should do the trick for map edge boundaries, however it didn't help. I noticed a thing though, when i moved the obstacles the player was still getting stuck at the same places (like if there was invisible blocks there).coffee wrote:If I'm not wrong and for what I quickly checked you don't have any left/right key control to don't let the player go out of the map. I believe could be something like that doing the nil's values or in text display not yet declared strings.qrux wrote:
I actually did, and the result was that when the player approaches the left obstacle all the code after the print line is not running and i got a weird error:(i changed the local variables to global to be able to print them)Code: Select all
attempt to concentate global 'leftTileNo' (a nil value)
A good thing you could also implement is a routine that convert the mouse position to respective tile map x/y and tile number under.
I have in my experiences a routine that I think you can easily adapt to your game. With that you can start checking what are the map coordinates and map values under cursor (by checking in your case TileTable [mouse_tile_x][mouse_tile_y])I hope this helps.Code: Select all
function mouse2tile () -- retuns the tile under cursor mouse_x = love.mouse.getX() mouse_y = love.mouse.getY() mouse_tile_x = map.x + math.floor((mouse_x) / tile.w) mouse_tile_y = map.y + math.floor((mouse_y) / tile.h) if mouse_tile_x <= map.width and mouse_tile_x > 0 and mouse_tile_y <= map.height and mouse_tile_y > 0 then return mouse_tile_x, mouse_tile_y else mouse_tile_x = 0 -- I use 0 to transform "nil" errors in a non-problematic value and use it to know that cursor is outside any area in the map mouse_tile_y = 0 return mouse_tile_x, mouse_tile_y end end
So now i'm completely lost
Re: Collision not working
At first sight, you have a problem in world2map, it should be:
since your tilemap starts at index 1 in X and Y
Secondly, it seams that player.x and player.y point to the top left corner of a 2 tiles height character and that you check for collision only for that point. That explains why your player is only affected by >2 tiles height obstacles, and also why player is blocked 1 tile too early when going left.
Code: Select all
function world2map(x,y)
return math.floor(x/TileW)+1, math.floor(y/TileH)+1
end
Secondly, it seams that player.x and player.y point to the top left corner of a 2 tiles height character and that you check for collision only for that point. That explains why your player is only affected by >2 tiles height obstacles, and also why player is blocked 1 tile too early when going left.
Re: Collision not working
How can i fix this without having to change the size of the player tile? I tried puttingsebast wrote: Secondly, it seams that player.x and player.y point to the top left corner of a 2 tiles height character and that you check for collision only for that point. That explains why your player is only affected by >2 tiles height obstacles, and also why player is blocked 1 tile too early when going left.
Code: Select all
local tileX, tileY = world2map(player.x, (player.y - 40))
Thanks!
Re: Collision not working
For the obstacle height problem, you have to check for the two tiles at right (or left), something like:qrux wrote:How can i fix this without having to change the size of the player tile? I tried puttingsebast wrote: Secondly, it seams that player.x and player.y point to the top left corner of a 2 tiles height character and that you check for collision only for that point. That explains why your player is only affected by >2 tiles height obstacles, and also why player is blocked 1 tile too early when going left.however that didn't change anything.Code: Select all
local tileX, tileY = world2map(player.x, (player.y - 40))
Thanks!
Code: Select all
love.keyboard.isDown("d","right") and TileTable[rightTileY][rightTileX] == 1 and TileTable[rightTileY+1][rightTileX] == 1
Re: Collision not working
Thank you, it fixed the height problem! However, the player is still stopping one block too early when going leftsebast wrote:For the obstacle height problem, you have to check for the two tiles at right (or left), something like:Code: Select all
love.keyboard.isDown("d","right") and TileTable[rightTileY][rightTileX] == 1 and TileTable[rightTileY+1][rightTileX] == 1
Of course, i can change the
Code: Select all
local leftTileX, leftTileY = tileX, tileY
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests