I have stumbled upon a problem while writing a code for my top-down tile-map game.
Note that the player's movement isn't restricted to tiles, it's free movement.
Firstly, to get any idea of what I'm trying to do I'll post the way I use the basic default
Code: Select all
for i,lev in ipairs(level_draw) do
if CheckCollision(player.x + (player.xvel * dt)*2, player.y + (player.yvel * dt)*2, player.height, player.width, lev.x, lev.y, level.tile_h, level.tile_w) then
if lev.tile_id == '1' then
player.xvel = 0
player.yvel = 0
end
end
end
Code: Select all
if love.keyboard.isDown('d') and player.xvel < player.speed then
player.xvel = player.xvel + player.speed * dt
end
if love.keyboard.isDown('a') and player.xvel > -player.speed then
player.xvel = player.xvel - player.speed * dt
end
if love.keyboard.isDown('w') and player.yvel > -player.speed then
player.yvel = player.yvel - player.speed * dt
end
if love.keyboard.isDown('s') and player.yvel > -player.speed then
player.yvel = player.yvel + player.speed * dt
end
Code: Select all
player.x = player.x + player.xvel * dt
player.y = player.y + player.yvel * dt
player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))
The collision works perfectly fine, however, the issue I found is that this makes the controls bad, for example, if I move down causing a collision with a tile below me, and then, while still holding the move down key I press left, I would expect to be able to move left while not being allowed to move down. Note that I know that when the collision is detected, it sets both velocities to 0 therefor disabling the behavior I want from even happening, but that is the entire problem here, I have spent 2 days now stumped on how to make it happen.
Any input would be appreciated.
P.S. to the board admins, sorry I submitted this issue twice, I apparently missed the part where it says posts have to be approved and though it was just somehow eaten.