Trouble figuring out collisions for array-based maps

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
kshift
Prole
Posts: 2
Joined: Sat Jan 27, 2018 7:04 pm

Trouble figuring out collisions for array-based maps

Post by kshift »

I'm working on grid-based movement and can't figure out collisions for the way I generate maps.

Here is the code for the game, it's basically a simple mock-up for movement and map creation:

Code: Select all

function love.load()
	player = {
		grid_x = 256,
		grid_y = 256,
		spd = 10,
		walkable = false
	}
	map1 = {
		{1, 1, 1, 1, 1, 1, 1, 1, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 1, 1, 1, 1, 1, 1, 1, 1}
	}
end

function drawMap(map)
	for y=1, #map do
		for x=1, #map[y] do
			if map[y][x] == 1 then
				love.graphics.rectangle("line", x * 32, y * 32, 32 ,32)
			end
		end
	end
end

function love.update(dt)

end

function love.draw()
	drawMap(map1)
	love.graphics.rectangle("fill", player.grid_x, player.grid_y, 32, 32)
end
 
function love.keypressed(key)
	if key == "up" or key == "w" then
		player.grid_y = player.grid_y - 32
	elseif key == "down" or key == "s" then
		player.grid_y = player.grid_y + 32
	elseif key == "left" or key == "a" then
		player.grid_x = player.grid_x - 32
	elseif key == "right"  or key == "d" then
		player.grid_x = player.grid_x + 32
	end
end
I looked at the grid-locked movement tutorial, but I didn't like the way it handled collisions since the game crashes if the player leaves the map. Any ideas on the best way to implement simple collisions into this system? Thanks!
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Trouble figuring out collisions for array-based maps

Post by MadByte »

Welcome to the forums!
If you stick to grid-based movement then it's less like having collisions - its more like having movement restrictions.
You can check if the target position can be entered (no tile or not walkable) and if so, move the player there.
That's it.

Code: Select all

-- ...

function player:move(dx, dy)
	local newx, newy = self.x+dx, self.y+dy
	local tile = map:getTile(newx, newy)
	if tile and tile == 0 then -- 0 = no tile (maybe shouldn't have called the var tile then ...hmm)
		self.x = newx
		self.y = newy
	end
end

-- ...
	
function love.keypressed(key)
	if key == "left" then player:move(-1, 0)
	elseif key == "right" then player:move(1, 0)
	elseif key == "up" then player:move(0, -1)
	elseif key == "down" then player:move(0, 1)
	elseif key == "escape" then love.event.quit() end
end
Here is the full example. (Move with Arrow Keys)
kshift
Prole
Posts: 2
Joined: Sat Jan 27, 2018 7:04 pm

Re: Trouble figuring out collisions for array-based maps

Post by kshift »

Thanks for the reply! I'm looking at the source for your example right now. I had a question about if I wanted to add actual bullet physics. Would I use something similar to map:getTile to handle bullet ricochet, impact, etc, or would I be better off using a dedicated library?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Trouble figuring out collisions for array-based maps

Post by MadByte »

kshift wrote: Sat Jan 27, 2018 11:11 pm Thanks for the reply! I'm looking at the source for your example right now. I had a question about if I wanted to add actual bullet physics. Would I use something similar to map:getTile to handle bullet ricochet, impact, etc, or would I be better off using a dedicated library?
Not sure what you mean by "similar to map:getTile". The method just returns a value of an two dimensional array, which won't help you with bullet physics.

Also your question isn't very specific. Do you want the bullets to be grid-locked as well? Or do you mean "real" physics?
If you want bullets to be grid-locked, then I would say yes - you can use map:getTile the same way as for the player to check if the bullet can move to a tile or not. But it has nothing to do with ricochet, impact, etc. Otherwise if you want normal movement for the bullets then you need to check for collisions and resolve it yourself, or you use something like love.physics, Hardon Collider or bump depending on your skill in LÖVE/Lua and what kind of collisions (and collision resolution) you want.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 205 guests