## Platformer Collsion With Maps

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Davidobot
Party member
Posts: 1150
Joined: Sat Mar 31, 2012 5:18 am
Location: Game-Dev. Land
Contact:

### Platformer Collsion With Maps

How make the char collide with maps like this? https://love2d.org/wiki/Tutorial:Gridlocked_Player
Attachments
Platformer X.love
My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

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

### Re: Platformer Collsion With Maps

My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

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

### Re: Platformer Collsion With Maps

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].

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

### Re: Platformer Collsion With Maps

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!

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

### Re: Platformer Collsion With Maps

Could you?
My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

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

### Re: Platformer Collsion With Maps

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.

### Who is online

Users browsing this forum: No registered users and 22 guests