Code: Select all
if not player.onGround then
yvel = yvel + gravity * dt
end
Here's the current code I'm working with:
Code: Select all
function love.update(dt)
-- update the player.x and player.y when arrow keys are pressed
if love.keyboard.isDown('a') then
player.x = player.x - player.speed * dt
end
if love.keyboard.isDown('d') then
player.x = player.x + player.speed * dt
end
-- local cols, len
-- local _, _, cols, len = world:check(player, player.x, player.y)
-- print debug-
if player.yvel ~= 0 then
for i=len,1,-1 do
if cols[i].normal.y == 1 then
player.yvel = 0
player.onGround = true
end
end
end
if not player.onGround then
end
-- update the player associated bounding box in the world
newX, newY, cols, len = world:move(player, player.x, player.y)
player.x, player.y = newX, newY
end
-redsled
EDIT:
For those looking for reference I fixed it by doing this instead:
Code: Select all
-- gravity
player.yvel = player.yvel + player.gravity * dt
if player.yvel > player.gravity then player.yvel = player.gravity end
-- movement
if love.keyboard.isDown("d") then
player.xvel = player.xvel + player.speed * dt
end
if love.keyboard.isDown("a") then
player.xvel = player.xvel - player.speed * dt
end
local nx, ny = player.x + (player.xvel * dt), player.y + (player.yvel * dt)
local finalX, finalY, collisions = world:move(player, nx, ny)
if #collisions > 0 then
for i,v in ipairs(collisions) do
if v.normal.y == -1 or v.normal.y == 1 then
player.yvel = 0
end
end
end
player.x = finalX
player.y = finalY