Collision not working

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Collision not working

Post by qrux »

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!
Attachments
game.love
(400.79 KiB) Downloaded 328 times
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Collision not working

Post by coffee »

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.
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Re: Collision not working

Post by qrux »

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.
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:

Code: Select all

attempt to concentate global 'leftTileNo' (a nil value)
(i changed the local variables to global to be able to print them)
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Collision not working

Post by coffee »

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:

Code: Select all

attempt to concentate global 'leftTileNo' (a nil value)
(i changed the local variables to global to be able to print them)
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.

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
I hope this helps.
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Re: Collision not working

Post by qrux »

coffee wrote:
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:

Code: Select all

attempt to concentate global 'leftTileNo' (a nil value)
(i changed the local variables to global to be able to print them)
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.

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
I hope this helps.
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).
So now i'm completely lost :roll:
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Re: Collision not working

Post by qrux »

bump? :o:
User avatar
sebast
Prole
Posts: 27
Joined: Tue Nov 08, 2011 2:17 pm

Re: Collision not working

Post by sebast »

At first sight, you have a problem in world2map, it should be:

Code: Select all

function world2map(x,y)
  return math.floor(x/TileW)+1, math.floor(y/TileH)+1
end
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.
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Re: Collision not working

Post by qrux »

sebast 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.
How can i fix this without having to change the size of the player tile? I tried putting

Code: Select all

local tileX, tileY = world2map(player.x, (player.y - 40))
however that didn't change anything.

Thanks!
User avatar
sebast
Prole
Posts: 27
Joined: Tue Nov 08, 2011 2:17 pm

Re: Collision not working

Post by sebast »

qrux wrote:
sebast 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.
How can i fix this without having to change the size of the player tile? I tried putting

Code: Select all

local tileX, tileY = world2map(player.x, (player.y - 40))
however that didn't change anything.

Thanks!
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
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Re: Collision not working

Post by qrux »

sebast 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
Thank you, it fixed the height problem! However, the player is still stopping one block too early when going left :|
Of course, i can change the

Code: Select all

local leftTileX, leftTileY = tileX, tileY
like so, but i don't know if that's a good solution.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests