Problem with wall jumping in platformers and bump.lua

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Problem with wall jumping in platformers and bump.lua

Post by redsled »

Hey there lover! So, I have a problem with my platformer and walljumps. The way it's currently setup- actually lemme show you the code first:

Code: Select all

function player:collisions(dt)
    --solid tile collisions
    local nx, ny = self.x + (self.xvel * dt), self.y + (self.yvel * dt)
    local finalX, finalY, collisions = wrld:move(self, nx, ny)

    if #collisions > 0 then
        for i,v in ipairs(collisions) do
            if v.other.type == "solid" then
                if v.normal.y == -1 then
                    self.yvel = 0
                    self.onGround = true
                elseif v.normal.y == 1 then
                   self.yvel = 0
                end
                if v.normal.x == 1 then
                    self.walljump = true
                    self.wallDir = "l"
                    self.xvel = 0
                elseif v.normal.x == -1 then
                    self.walljump = true
                    self.wallDir = "r"
                    self.xvel = 0
                end

            elseif v.other.type == "finish" then
                self.state = "finish"
            end
        end

    -- reset player bool variables
    elseif #collisions < 1 then
        self.walljump = false
        self.onGround = false
    end

    self.x = finalX
    self.y = finalY
end
Assuming you are code-literate, you can see that I set walljump to true when the player collides with a left or right side of a tile. And if there is no collisions, walljump is set to false.

While this works if the player is moving into a tile, it doesn't work if it's right against it and not moving. This happens purely because of the way bump.lua works, I know. But is there a way that'd basically allow walljumping easier for anyone who hasn't tried it for a day. Don't believe me? Check the .love and try it out.

Should I put in a bounding box that is 1-pixel out of the player and check all tiles for a simple AABB check? Currently, that's my only idea however I'm scared that it'd be too memory intensive.

Any suggestions would help,
- redsled :awesome:
Attachments
simpleplatformer.love
(17.1 KiB) Downloaded 159 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Problem with wall jumping in platformers and bump.lua

Post by kikito »

This could be implemented by using extra rectangles, which would work as "wall detectors".

The setup is as follows: You still have one "main" rectangle for your player, which is used for "sliding through the level". But in addition, you add one rectangle which is a bit wider (or you could add two smaller rectangles, one on each side of the player). These act as "wall detectors" . They move with the "main" rectangle and use "cross" collisions, so they don't "push the player away from the wall".

The "extra width" you need to use depends on how much "space" you want to leave your players before they start sliding. Give it 20 pixels and they will "grab walls from far away". If you make them 2 pixels wider than the player, then the player will need to almost be touching the wall. You will have to experiment. I would recommend moving the player towards the wall when there is a wall jump, otherwise it will seem as if they jumped through the air.

This concept can also be used to make "wall slides" - the player touches the wall and "falls down more slowly" than in free fall. That mechanism also helps new players.

EDIT:
Should I put in a bounding box that is 1-pixel out of the player and check all tiles for a simple AABB check? Currently, that's my only idea however I'm scared that it'd be too memory intensive.
This won't be memory intensive at all. Adding one item to bump is in fact quite cheap (it means adding 1 Lua table with 4 numbers and one reference to it on each cell it touches)
When I write def I mean function.
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: Problem with wall jumping in platformers and bump.lua

Post by redsled »

kikito wrote: They move with the "main" rectangle and use "cross" collisions, so they don't "push the player away from the wall".
When I try to pass the "cross" filter like this, it gives me an error.

Code: Select all

world:move(WALL_JUMP, player.x,player.y, "cross")
It gives this error:
Error: bump.lua:701: attempt to call upvalue 'filter' (a string value)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Problem with wall jumping in platformers and bump.lua

Post by kikito »

The filter must be a function, not a string. I don't know how you are detecting that something is a wall, I will asume you can do other.isWall - adapt it to your needs:

Code: Select all

local detectorFilter = function(item, other) if other.isWall then return 'cross' end end

...

world:move(WALL_JUMP, player.x,player.y, detectorFilter)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Ant2.71828182845904 and 1 guest