[OP IS DUMB] Troubles with gravity and yvel... (bump.lua)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

[OP IS DUMB] Troubles with gravity and yvel... (bump.lua)

Post by redsled »

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:

Code: Select all

if not player.onGround then
    yvel = yvel + gravity * dt
end
go before or after the collision check/resetting yvel? :?

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
Thanks a bunch,
-redsled :awesome:

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
The xvel movement is wonky but it works.
Attachments
simpleplatformer.love
(10.92 KiB) Downloaded 70 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 62 guests