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: 1150
Joined: Sat Mar 31, 2012 5:18 am
Location: Game-Dev. Land
Contact:

Platformer Collsion With Maps

Post by Davidobot » Sun Apr 22, 2012 11:21 am

How make the char collide with maps like this? https://love2d.org/wiki/Tutorial:Gridlocked_Player
Attachments
Platformer X.love
(9.03 MiB) Downloaded 93 times
My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

User avatar
Davidobot
Party member
Posts: 1150
Joined: Sat Mar 31, 2012 5:18 am
Location: Game-Dev. Land
Contact:

Re: Platformer Collsion With Maps

Post by Davidobot » Tue Apr 24, 2012 4:56 am

Bump? please help me on this one. :(
My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Platformer Collsion With Maps

Post by trubblegum » Tue Apr 24, 2012 6:21 am

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: 1150
Joined: Sat Mar 31, 2012 5:18 am
Location: Game-Dev. Land
Contact:

Re: Platformer Collsion With Maps

Post by Davidobot » Tue Apr 24, 2012 3:38 pm

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?
My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

User avatar
Davidobot
Party member
Posts: 1150
Joined: Sat Mar 31, 2012 5:18 am
Location: Game-Dev. Land
Contact:

Re: Platformer Collsion With Maps

Post by Davidobot » Tue May 08, 2012 1:33 pm

Could you?
My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Platformer Collsion With Maps

Post by Puzzlem00n » Tue May 08, 2012 10:08 pm

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: Google [Bot] and 6 guests