Page 1 of 1

Platformer Collsion With Maps

Posted: Sun Apr 22, 2012 11:21 am
by Davidobot
How make the char collide with maps like this? https://love2d.org/wiki/Tutorial:Gridlocked_Player

Re: Platformer Collsion With Maps

Posted: Tue Apr 24, 2012 4:56 am
by Davidobot
Bump? please help me on this one. :(

Re: Platformer Collsion With Maps

Posted: Tue Apr 24, 2012 6:21 am
by trubblegum
A grid-locked object is always aligned with the grid. That means its location is normally exactly equal to that of one of the grid squares, so all you have to do is find which grid space has the same co-ordinates as the player.
If you've been smart about it, you can find the map square at the player's location using something like map[player.y / gridsize][player.x / gridsize].

Re: Platformer Collsion With Maps

Posted: Tue Apr 24, 2012 3:38 pm
by Davidobot
trubblegum wrote:A grid-locked object is always aligned with the grid. That means its location is normally exactly equal to that of one of the grid squares, so all you have to do is find which grid space has the same co-ordinates as the player.
If you've been smart about it, you can find the map square at the player's location using something like map[player.y / gridsize][player.x / gridsize].
Can you post an example of that?

Re: Platformer Collsion With Maps

Posted: Tue May 08, 2012 1:33 pm
by Davidobot
Could you?

Re: Platformer Collsion With Maps

Posted: Tue May 08, 2012 10:08 pm
by Puzzlem00n
I can.

Code: Select all

function love.load()
	player = {
		x,
		y,
		ySpeed = 0,
		grav = 1.5
		}

	map = {
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1},
		{1, X, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1},
		{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1},
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	}

	for y = 1, #map do
		for x = 1, #map[y] do
			if map[y][x] == X then
				player.x = x * 40 - 40
				player.y = y * 40 - 40
			end
		end
	end
end

function love.update(dt)
	dt = math.min(dt, 0.07)
	frame = dt * 30
	player.ySpeed = player.ySpeed + player.grav * frame
	player.y = player.y + player.ySpeed * frame
	boundingChecks()
end

function love.keypressed(key)
	if key == "up" and player.ySpeed == 0 then
			player.ySpeed = -20
	end
	if key == "escape" then
		love.event.quit()
	end
end

function boundingChecks()
	if mapCollide(player.x + 1, player.y + 40) == 1 or mapCollide(player.x + 39, player.y + 40) == 1 then
		player.y = player.y - player.ySpeed * frame
		player.ySpeed = 0
	end
	if mapCollide(player.x + 1, player.y) == 1 or mapCollide(player.x + 39, player.y) == 1 then
		player.ySpeed = .5
	end
	if love.keyboard.isDown("left") then
		player.x = player.x - 8 * frame
	end
	if love.keyboard.isDown("right") then
		player.x = player.x + 8 * frame
	end
	if mapCollide(player.x, player.y + 1) == 1 or mapCollide(player.x, player.y + 39) == 1 then
		player.x = player.x + 8 * frame
	end
	if mapCollide(player.x + 40, player.y + 1) == 1 or mapCollide(player.x + 40, player.y + 39) == 1 then
		player.x = player.x - 8 * frame
	end
end

function love.draw()
		for y = 1, #map do
			for x = 1, #map[y] do
				if map[y][x] == 0 then
				end
				if map[y][x] == 1 then
					love.graphics.setColor(255, 255, 255)
					love.graphics.rectangle("fill", x * 40 - 40, y * 40 - 40, 40, 40)
				end
			end
		end
		love.graphics.setColor(0, 255, 255)
		love.graphics.rectangle("fill", player.x, player.y, 40, 40)
end

function mapCollide(x, y)
	checkMap = map[math.floor(y/40) + 1][math.floor(x/40) + 1]
	return checkMap
end