[OP IS DUMB] Troubles with gravity and yvel... (bump.lua)
Posted: Mon May 18, 2015 12:18 am
So, I have a problem with my code (trying to make a platformer), I'm completely stuck on trying to applying gravity to the character. I've tried doing a simple method however it bugs out most of the time. It involves the for-loop collision detection and resetting the yvel after. Although it didn't work, should the player.yvel+gravity code:
go before or after the collision check/resetting yvel?
Here's the current code I'm working with:
Thanks a bunch,
-redsled
EDIT:
For those looking for reference I fixed it by doing this instead:
The xvel movement is wonky but it works.
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