Platformer Collsion With Maps
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Platformer Collsion With Maps
How make the char collide with maps like this? https://love2d.org/wiki/Tutorial:Gridlocked_Player
- Attachments
-
- Platformer X.love
- (9.03 MiB) Downloaded 219 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
personal page and a raycaster
Re: Platformer Collsion With Maps
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
personal page and a raycaster
- 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].
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
Can you post an example of that?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].
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
personal page and a raycaster
Re: Platformer Collsion With Maps
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
personal page and a raycaster
- 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: Google [Bot] and 8 guests