Platformer Collsion With 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
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Platformer Collsion With Maps

Post by Davidobot »

How make the char collide with maps like this? https://love2d.org/wiki/Tutorial:Gridlocked_Player
Attachments
Platformer X.love
(9.03 MiB) Downloaded 195 times
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Platformer Collsion With Maps

Post by Davidobot »

Bump? please help me on this one. :(
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Platformer Collsion With Maps

Post 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].
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Platformer Collsion With Maps

Post 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?
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Platformer Collsion With Maps

Post by Davidobot »

Could you?
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Platformer Collsion With Maps

Post 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
I LÖVE, therefore I am.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 53 guests