Tile "collisoin"

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.
Post Reply
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Tile "collisoin"

Post by veethree »

Allright. This has been bugging me for the last 2 days. Ran out of ideas.

So what i'm trying to achieve is player/tile "collision". The player is gridlocked so i figured this would be simple..But i must be doing something wrong since i cant get this to work. Basically my setup is everytime you press for example the Right key it loops through the solid tiles (which are in a separate table) and checks if any of them are in the same x position as the player + 1.

Code: Select all

	local go = true
	if key == "d" then
		for i=1, #tiles do
			if tiles[i].x == player.x + 1 then
				go = false
				break
			end
		end
		if go then
			player.x = player.x + 1
		end
That is the current code, I've written like 15 variations with the same/similar logic. Sometimes it would work but only with the last tile added, and if i used a backward loops it would only work for the first tile added.

Here's the whole code (this is a test project, not the actual game where i'll be using this stuff)

Code: Select all

function love.load()
	tiles = {}
	
	w = 12
	h = 12
	xs = 0
	ys = 0
	for y=1, h do
		for x=1, w do
			local r = math.random(0, 100)
			if r <= 80 then
				--addTile(xs, ys, false)
			else
				addTile(xs, ys, true)
			end
			xs = xs + 1
		end
		xs=0
		ys = ys + 1
	end
	
	player = {
		x = 5,
		y = 5
	}
end

function love.keypressed(key)
	local go = true
	if key == "d" then
		for i=1, #tiles do
			if tiles[i].x == player.x + 1 then
				go = false
				break
			end
		end
		if go then
			player.x = player.x + 1
		end
	elseif key == "a" then
		player.x = player.x - 1
	elseif key == "w" then
		player.y = player.y - 1
	elseif key == "s" then
		player.y = player.y + 1
	end
end

function addTile(x, y, s)
	tiles[#tiles + 1] = {
		x = x,
		y = y,
		s = s
	}
end

function love.update(dt)

end

function love.draw()
	for i=1, #tiles do
		if tiles[i].s then
			love.graphics.setColor(255, 0, 0)
		else
			love.graphics.setColor(150, 150, 150)
		end
		love.graphics.rectangle("fill", tiles[i].x*32, tiles[i].y *32, 32, 32)
	end
	love.graphics.setColor(255, 255, 255)
	love.graphics.rectangle("fill", player.x * 32, player.y * 32, 32, 32)
end
Any help is greatly appreciated, Thanks in advance.
-Veethree
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Tile "collisoin"

Post by Nixola »

Can't you use X and Y indexing for the tiles and then simplly check for the tile[player.x+1][player.y]?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Tile "collisoin"

Post by coffee »

Hmm, I think I already say your kind of problem or already was posted before by you before right?... You keeping your maps in the wrong way I think (if I decoded right your code). Instead of have tiles that have X,Y positions use a dimensional map grid that keeps tiles. That way you can't directly address to X,Y positions and need to sweep all your tiles.
Last edited by coffee on Wed Sep 19, 2012 5:16 pm, edited 1 time in total.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Tile "collisoin"

Post by veethree »

coffee wrote:Hmm, I think I already say your kind of problem or already was posted before by you before right?... You keeping your maps in the wrong way I think. Instead of have tiles that have X,Y positions use a dimensional map grid that keeps tiles.
By dimensional map grid do you mean like the stuff in the gridlocked player tutorial on the wiki? If so, I'm not sure that'd work for me, Eventually i'd like to have all the tiles in the same table and each tile would have properties like if its solid, if you can pick it up etc.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Tile "collisoin"

Post by coffee »

veethree wrote:
coffee wrote:Hmm, I think I already say your kind of problem or already was posted before by you before right?... You keeping your maps in the wrong way I think. Instead of have tiles that have X,Y positions use a dimensional map grid that keeps tiles.
By dimensional map grid do you mean like the stuff in the gridlocked player tutorial on the wiki? If so, I'm not sure that'd work for me, Eventually i'd like to have all the tiles in the same table and each tile would have properties like if its solid, if you can pick it up etc.
Are you doing some terraria clone right? I think you won't have problems but it will actually help you a lot more keep things right. You have a lot of disadvantages and problems keep things in that way. But it's your choice of course.

You can also keep this in "columns/piles" but also not a good thing. Compare and checking tiles at left/right would be a bit confusing and would require a lot more work than dealing with a X/Y indexed map.

As you notice Nixola also propose a bit the same 'cause it's a more natural way of keep that kind of info.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Tile "collisoin"

Post by veethree »

coffee wrote:
veethree wrote:
coffee wrote:Hmm, I think I already say your kind of problem or already was posted before by you before right?... You keeping your maps in the wrong way I think. Instead of have tiles that have X,Y positions use a dimensional map grid that keeps tiles.
By dimensional map grid do you mean like the stuff in the gridlocked player tutorial on the wiki? If so, I'm not sure that'd work for me, Eventually i'd like to have all the tiles in the same table and each tile would have properties like if its solid, if you can pick it up etc.
Are you doing some terraria clone right? I think you won't have problems but it will actually help you a lot more keep things right. You have a lot of disadvantages and problems keep things in that way. But it's your choice of course.

You can also keep this in "columns/piles" but also not a good thing. Compare and checking tiles at left/right would be a bit confusing and would require a lot more work than dealing with a X/Y indexed map.

As you notice Nixola also propose a bit the same 'cause it's a more natural way of keep that kind of info.
Nah i'm planning on using this in a supaplex clone. What would be the right way to handle this?
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Tile "collisoin"

Post by coffee »

veethree wrote:Nah i'm planning on using this in a supaplex clone. What would be the right way to handle this?
Didn't knew that one. Seems a derivative from older Boulder Dash (http://en.wikipedia.org/wiki/Boulder_Dash).

Yeah, you should turn it in a

Code: Select all

map = {
{0,0,0},
{2,1,3}
}
style of map.

Then keep tiles in

Code: Select all

tile[1] = { name = "rock", solid = true, fall = true, dig = false,  kill_player = true, graphic = "rock.png" }
tile[2] = { name = "sand", solid = false, fall = false, dig = true,  kill_player = false, graphic = "sand.png"  } 
and so on...

and check tiles at right a bit like Nixola said

Code: Select all

if tile[map[player.x+1][player.y]].solid == false then (do move things/stuff) end
An advice. You should check/sweep your map from bottom to top for map positions with objects that bellow have a void (0 in my example) and make it fall if tile.fall == true.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Tile "collisoin"

Post by Kadoba »

If your map has a fixed width here's another way you can have a 2d map in a single table.
SingleTableMap.love
(386 Bytes) Downloaded 86 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 202 guests