I've been trying to make a character that can freely move around a tile map (non-gridlocked). I can get the collision detection to work, but if the player isn't moving one tile then there's a gap between the player and the tile.
yr9OGQw.jpg (3.86 KiB) Viewed 143 times
This error only happens when going up or left. Here's the code I'm using to handle player movement:
local player = {}
function player:load()
self.x = 0
self.y = 0
self.h = 32
self.w = 32
self.mode = 'fill'
self.speed = 64
self.o = -1
self.map = {}
end
function player:setPos(x,y)
self.x = x
self.y = y
end
function player:getPos()
return self.x, self.y
end
function player:setMap(map,current)
self.map = map[current]
end
function player:draw()
love.graphics.setColor(0,186,186)
love.graphics.rectangle(self.mode, (self.x + self.o) * 32, (self.y + self.o)* 32, self.h, self.w)
end
function player:update(dt)
if love.keyboard.isDown('d') and self.map[math.floor(self.x) + 1][math.floor(self.y)] ~= "wall" then
self.x = self.x + 0.5
end
if love.keyboard.isDown('a') and self.map[math.floor(self.x) - 1][math.floor(self.y)] ~= "wall" then
self.x = self.x - 0.5
end
if love.keyboard.isDown('w') and self.map[math.floor(self.x)][math.floor(self.y) - 1] ~= "wall" then
self.y = self.y - 0.5
end
if love.keyboard.isDown('s') and self.map[math.floor(self.x)][math.floor(self.y) + 1] ~= "wall" then
self.y = self.y + 0.5
end
end
function player:nextTile()
end
return player
Because the (x, y) coordinates refer to the upper-left corner of things. That means there is a slightly different formula for the bottom and the right, and the one you already had worked.