Page 1 of 1

bump.lua library player problems

Posted: Tue Mar 01, 2016 9:18 am
by Garb
Hello, I'm using Kikito's bump.lua for my collisions and it's working fairly well except for a fairly serious problem I'm having.

I want to make my player able to crouch but when he returns to his original height he teleports through the bump blocks, which should never happen. I need to make a way to prevent the player from "uncrouching" when the area he is in is too small.

The problem becomes obvious if you run the love file, crouch pressing down and enter one of the small block channels, then release the crouch button and watch the player glitch around because he returned to his original height while too close to a block above. You can also sometimes replicate this problem by crouching, walking directly onto a wall so the player is colliding on his left or right side, then standing.

I'm not sure how to prevent this using bump and any input would be greatly appreciated.

I've attached an example love which contains the error. Press down to crouch, and the game will close if you go out of bounds.
testbed.love
move with arrow, jump with c, crouch with down arrow
(15.82 KiB) Downloaded 134 times
Thanks.

Re: bump.lua library player problems

Posted: Tue Mar 01, 2016 10:24 am
by kikito
Hi there,

I did not look at your code, but I can think of two ways of solving this.

The first option is using the world:queryRect method - if the player is crouching but no longer pressing down, you calculate the rectangle he would occupy if he was standing up, and pass it to queryRect (probably ignoring the player). If it returns solid walls, then it is not safe to stand up.

The other option is adding a second invisible rectangle for your player. You could call it "shadow". It is invisible. You have to move it with the player (probably you can use world:update instead of world:move, since it's faster and we're not looking for , and is as wide and tall as the player without crouching. It collides using "cross" - so it detects collisions but doesn't "react" to them (it is probably a good idea that it filters out collisions with the player).

Before the player can "uncrouching", he must ask the shadow: "Are you colliding with anything? (probably this can be improved by asking "are you colliding with anything from above", using the collision normal. I am not sure). If the shadow is colliding with anything, then the player can not stand up.

This kind of "shadows" are useful for other features - you can have several of them, and use them to detect things like "the end of the platform is near". But I think both approaches will work.

Re: bump.lua library player problems

Posted: Tue Mar 01, 2016 7:43 pm
by Garb
Thanks for the reply, world:queryRect seems to be exactly what I need but i'm a bit confused on how to make it check above the player?
For example, if I do this it just stays crouched when I press down.

Code: Select all

  --CROUCHING
  len = world:queryRect(player.x,player.y,player.w,80)
  if love.keyboard.isDown("down") then
    player.crouching = true
  elseif len == 0 then
    player.crouching = false
  end
        if player.crouching == true then
          world:update(player, player.x, player.y, player.w, player.ch)
          player.h = player.ch
        elseif player.crouching == false then
          world:update(player, player.x, player.y, player.w, player.h)
          player.h = 80
        end
Once again, huge thanks for the help.

Re: bump.lua library player problems

Posted: Tue Mar 01, 2016 9:01 pm
by kikito

Code: Select all

len = world:queryRect(player.x,player.y,player.w,80)
Are you sure those are the right parameters? If you just "reuse" player.y, the rectangle would be "buried in the ground", wouldn't it? Try drawing it.

Re: bump.lua library player problems

Posted: Tue Mar 01, 2016 10:40 pm
by Garb
I don't fully understand bump's querying functions just yet, so sorry if I sound a little senseless here but, I tried drawing the rectangle as you had suggested but it was just positioned where the player box was.

I also tried this code

Code: Select all

  local actualX, actualY, cols, crouchcheck = world:queryRect(player.x, player.y, player.h, player.h-1)
  if crouchcheck == false then
    player.crouching = false
  end

  if love.keyboard.isDown("down") then
    player.crouching = true
  end
which still didn't work. In world:queryRect(l,t,w,h, filter), I don't quite understand what l and t represent other then the players x and y?

Thanks again for the help, I'm still somewhat new to lua but I will get this eventually haha

Re: bump.lua library player problems

Posted: Wed Mar 02, 2016 12:35 am
by kikito
queryrect knows nothing about your "player". You give it a rectangle (any rectangle) and it tells you "how many items in the world touch that rectangle". (Unless the filter function says to ignore them). Each item is defined by its own rectangle in the world, so you are basically saying "give me the items whose rectangles touch this other rectangle".

In bump, all rectangles (the ones defining an item and the one used for querying) are made of the same 4 numbers: x,y,w,h. The first two represent the left & top coordinate of the rectangle, and the other two are its width and height.

Your problem is that you don't fully understand how your top coordinate (the "y") works.

Let's assume your player is 32 px high. When it's standing on top of a platform at height 0, its "y" coordinate is -32 (since in LÖVE negative y goes "up" on the screen).

Now, when the player is crouching, he's less tall. Let's assume its height is 16px. The first frame you set it to "crouch" after standing up, you see the player "fall". That's because its "y" coordinate was -32, but now there is no floor beneath it, because its height is 16 now, so it falls, until its new feet touch the ground. But now its "y" coordinate is not -32 any more. It's -16.

If you try to use queryrect with that "y", you will be using the wrong value, since you should be using -32, which is what the player would be if he was standing. You must calculate this number using the "crouching y", the "crouching height" and the "standing height".

Also, queryrect returns a list of items. Not true or false. Your "crouchcheck" is a table, so it is always "truthy". You must parse it with a loop to see if there are any blocks instead.

Re: bump.lua library player problems

Posted: Wed Mar 02, 2016 5:07 am
by Garb
I managed to get it working by doing something like this.

Code: Select all

    local items, len = world:queryRect(player.x,player.y-25,player.w,player.h-25)
    for i=2,len do
    player.crouching = true
    end
I'm only using bump for collision with my main world so I should be able to get away with this.

Thanks a bunch for the input, it is greatly appreciated!