[Solved]Platformer movement with [Bump]

General discussion about LÖVE, Lua, game development, puns, and unicorns.
LXDominik
Prole
Posts: 26
Joined: Wed Oct 01, 2014 9:05 am

[Solved]Platformer movement with [Bump]

Post by LXDominik »

Hi guys! Can some one explain me how to set up platformer:like movement using [bump].I just can't set gravity and jumping properly. Simple up\down\left\right i did like in bump simple demo, and i think got it how it works,i guess.
All info about player and collisions in player.lua
~sorry for bad english~
Last edited by LXDominik on Thu Oct 02, 2014 12:18 pm, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [Help]Platformer movement with [Bump]

Post by kikito »

Hi there,

This should be moved to the Support and Development forum.

How much Physics do you know? Do you know what acceleration is, and how it affects velocity? And what velocity is, and how it affects space?

I am not asking to be offensive or anything, I need to know what you know in order to communicate in the correct level.
When I write def I mean function.
LXDominik
Prole
Posts: 26
Joined: Wed Oct 01, 2014 9:05 am

Re: [Help]Platformer movement with [Bump]

Post by LXDominik »

kikito wrote:Hi there,

This should be moved to the Support and Development forum.

How much Physics do you know? Do you know what acceleration is, and how it affects velocity? And what velocity is, and how it affects space?

I am not asking to be offensive or anything, I need to know what you know in order to communicate in the correct level.
I need to create new thread there?

Well, velocity is basically speed and it affects object "a" in space, so the obj a will move to point b at "speed" per second, i guess.
And acceleration is how fast velocity grows from say 0 to 200.Smth like that ;),am i right?
~sorry for bad english~
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [Help]Platformer movement with [Bump]

Post by kikito »

Ok so you got the basics :).

I am not going to give you the code, but the steps you need to do.

First, add a vx and vy attribute to your player. Pressing keys should modify vx and vy instead of modifying its position directly (maybe you have it set it up that way, i don't know). The player position must be calculated using vx, vy and dt, after you have finished transforming the keypresses into vx and vy. Make sure it works: if you modify the code and make vx and vy smaller, then the player should move more slowly.

Second, add gravity. Gravity is a constant acceleration that makes vy increasingly higher with time. Put a variable in the top of player.lua and call it gravity. Set it up to a value (i.e. 60). Now, after transforming the keypresses into vx and vy, make vy increase by gravity * dt. Your player will "constantly fall" until you press "up" or it hits a floor. Tweak the number until it "feels right" for your game. It'll be already jumping, but there are a couple things to fix before it's perfect.

One of the things to fix is that he'll "stick to the ground" in some occasions. This is because gravity will keep increasing his vx forever. In order to fix this, on the collisions, detect when the player touches a floor (the normal of the collision, ny, should be -1). When this happens, and if his vy is positive, set vy to 0.

The other thing is that the player will be able to "fly" by keeping pressing "up". If you don't want this, you need to set a variable in the player called "isOnGround". Make it false at the beginning of every collision detection cycle. When you detect a floor (as mentioned on the previous paragraph) make that variable true. Now, the "up" key should only change vy when "isOnGround" is true.

That's it. Let me know if you get stuck in any of these steps.
When I write def I mean function.
LXDominik
Prole
Posts: 26
Joined: Wed Oct 01, 2014 9:05 am

Re: [Help]Platformer movement with [Bump]

Post by LXDominik »

Yay i did it! Thank you very much!
But insted of applying gravity all the time,i apply it when player is not jumping, and when player is jumping i apply -gravity for 0.5 sec.
Works great and seems fine by me :)
Thank You again!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [Help]Platformer movement with [Bump]

Post by kikito »

I am happy that you got it working :)
LXDominik wrote: But insted of applying gravity all the time,i apply it when player is not jumping, and when player is jumping i apply -gravity for 0.5 sec.
That is a bit weird. When you press "up", vx should be reset to some negative value (whatever you used on step 1). Even if after that you decrease vx with gravity a bit, the player should still jump. But hey, whatever floats your boat :)
When I write def I mean function.
LXDominik
Prole
Posts: 26
Joined: Wed Oct 01, 2014 9:05 am

Re: [Help]Platformer movement with [Bump]

Post by LXDominik »

kikito wrote:I am happy that you got it working :)
LXDominik wrote: But insted of applying gravity all the time,i apply it when player is not jumping, and when player is jumping i apply -gravity for 0.5 sec.
That is a bit weird. When you press "up", vx should be reset to some negative value (whatever you used on step 1). Even if after that you decrease vx with gravity a bit, the player should still jump. But hey, whatever floats your boat :)
Thats how i did it. maybe it is overcomplicated but it worked xD. Also can i ask u about collison filter? How to set it up?

Code: Select all

Timer = require "hump.timer"
--player collison box
gravity = 500
player = { x=50,y=50,w=30,h=30 , vx = 500}
player.onGround = false
player.hasJumped = false


function updatePlayer(dt)
  local vx = player.vx
  
  dx, dy = 0, 0
  if player.hasJumped == false then
  dy = gravity * dt
  end

  if love.keyboard.isDown('d') then
    dx = vx * dt
  elseif love.keyboard.isDown('a') then
    dx = -vx * dt
  end

if player.hasJumped == true then
  dy = -gravity * dt
end





  if dx ~= 0 or dy ~= 0 then
    player.onGround = false
    local future_l, future_t = player.x + dx, player.y + dy
    local cols, len = world:check(player, future_l, future_t)
  
    if len == 0 then
      player.x, player.y = future_l, future_t
      world:move(player, future_l, future_t)
      
    else
      local col, tl, tt, nx, ny, sl, st
      while len > 0 do
        col = cols[1]
        tl,tt,nx,ny,sl,st = col:getSlide()
        PlayercheckIfOnGround(ny)
        if player.onGround then 
          dy = 0
        end
        if ny > 0 then 
          player.hasJumped = false
        end
        player.x, player.y = tl, tt
        world:move(player, tl, tt)
        cols, len = world:check(player, sl, st)
        if len == 0 then

          player.x, player.y = sl, st
          world:move(player, sl, st)
        end
      end
    end
  end
end

function PlayercheckIfOnGround(ny)
  if ny < 0 then 
    player.onGround = true 
  end
end

function playerkeypressed(key)
  if key == " " then
    if player.hasJumped == false and player.onGround then
      player.hasJumped = true
      PlayerJumpTimer:add(.25, function() player.hasJumped = false end)
    end
  end
end
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [Solved]Platformer movement with [Bump]

Post by kikito »

I think it is a bit overcomplicated, yes. You should not need to use two variables (player.hasJumped vs player.isOnGround). One should be enough - they should be opposite (when one is true, the other is false).

It's ok to make vy "go down a bit" when you are on the ground. The player will "try to go through the ground" but bump will catch and resolve that correctly. That's how you detect that you are on the ground in the first place - and where you set vy to 0. You don't need to "not do it when you are not jumping". In other words: I don't think you need that if player.hasJumped == true around dy = -gravity * dt

Also, correct me if I'm wrong, but I think your player will not "fall from cliffs": if you are near a cliff and keep walking, the way the code is structured, I am not sure you can fall (it seems you need to jump first in order to do that).
Also can i ask u about collison filter? How to set it up?
A collision filter is a function which allows you to ignore some collisions. The first parameter is an item - anything you have added to the world with world:add and it's near the player (or whatever you are checking collisions for). If the function returns true, then the item will be checked for collisions. If it returns false, it will be skipped. You use it by putting it at the end of check. For example, in order to "collide only with blocks", and assuming that your blocks had a property called 'kind' set to 'Block', you could do this:

Code: Select all

local collideWithBlocks = function(item) return item.kind == 'Block' end
...
world:check(player, future_l, future_t, collideWithBlocks)
If you add more kinds of things to the world (like Clouds, or sparks) they will be ignored by the player when checking for collisions.
When I write def I mean function.
LXDominik
Prole
Posts: 26
Joined: Wed Oct 01, 2014 9:05 am

Re: [Solved]Platformer movement with [Bump]

Post by LXDominik »

Player can fall properly cause player.hasJumped always = false and it changes only for 0.25 sec to true using

Code: Select all

function playerkeypressed(key)
  if key == " " then
    if player.hasJumped == false and player.onGround then
      player.hasJumped = true
      PlayerJumpTimer:add(.25, function() player.hasJumped = false end)
    end
  end
end
, here goes reversed gravity for those 0.25 sec and then player.hasJumped changed back to false so the gravity is working again, and player.onGround i need to keep tracking of player being on ground so the player can jump only being on ground atm.Thats why i need both player.onGround and player.hasJumped :)

About filter, the way iam adding "solid" blocks is

Code: Select all

for i,v in pairs(blocks) do 
    	world:add(blocks[i], v.x,v.y,v.w,v.h)
end
So how to add this "kind" for all blocks?

also i can provide .love to sure u that it works ;)
edit : found bug ;) attached pic ^^
Attachments
bug.jpg
bug.jpg (40.79 KiB) Viewed 7293 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [Solved]Platformer movement with [Bump]

Post by kikito »

LXDominik wrote:Thats why i need both player.onGround and player.hasJumped
Ok! As I said, if it works for you, that's fine. I still think it is overcomplicated and you could do the same with just player.onGround.
So how to add this "kind" for all blocks?
The same way you add x,y,w, and height to the blocks. When you create them, you do something similar to blocks.kind = 'Block'

<Bug>


That's almost certainly related with how vx is calculated. Please upload a .love.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 80 guests