Page 1 of 1

Trying to make platformer from scratch, how do I keep my player falling when it collides on the side?

Posted: Mon Jul 24, 2023 7:32 pm
by MaxGamz
I didn't like bump.lua because it was a little bit complicated for me so I decided to make my own collision system from scratch. The play stops when it hits the platform but it stays at that y level, so it can't fall off the ledge. And when the player falls and hits the side of the ledge, it clips onto the top of the platform. I am aware of the issue but I am having trouble trying to find a solution.

Here is my algorithm for the move function

Code: Select all

function move(dt)
    if not collide(rectA, rectB) or (rectA.x == rectB.x - rectA.w) then -- supposed to prevent player from stopping when colliding on the sides
        rectA.y = rectA.y + rectA.yVel*dt                               -- accelerated player downward with the force of gravity
        rectA.yVel = rectA.yVel - rectA.gravity*dt
    else
        rectA.gravity = 0                                               -- player lands vertically on platform it is soppoed to stop
        rectA.yVel = 0
        rectA.y = rectB.y - rectA.h
    end

    if love.keyboard.isScancodeDown("a") then 
        rectA.x = rectA.x - rectA.s*dt
    end

    if love.keyboard.isScancodeDown("d") then 
        rectA.x = rectA.x + rectA.s*dt
    end
end
The "collide" function returns true anytime the player (rectA) collides with the platform (rectB) from any direction.

Re: Trying to make platformer from scratch, how do I keep my player falling when it collides on the side?

Posted: Mon Jul 24, 2023 10:03 pm
by zingo
Personally, I'm going to use "box2d" for my physics because it's already available in Love2d and does most of the work. However, for a nice tutorial on getting started with a simple platformer, you could check out https://sheepolution.com/learn/book/24 and maybe get some ideas. Also, attached is an example for some basic physics using box2d and bitmap collisions. Sorry I couldn't be more helpful, maybe someone else will have a better idea of how to address the issue with your code.

Re: Trying to make platformer from scratch, how do I keep my player falling when it collides on the side?

Posted: Mon Jul 24, 2023 10:59 pm
by MaxGamz
zingo wrote: Mon Jul 24, 2023 10:03 pm Personally, I'm going to use "box2d" for my physics because it's already available in Love2d and does most of the work. However, for a nice tutorial on getting started with a simple platformer, you could check out https://sheepolution.com/learn/book/24 and maybe get some ideas. Also, attached is an example for some basic physics using box2d and bitmap collisions. Sorry I couldn't be more helpful, maybe someone else will have a better idea of how to address the issue with your code.
Thanks for the tip! Anything helps honestly, I'm just trying to see my options

Re: Trying to make platformer from scratch, how do I keep my player falling when it collides on the side?

Posted: Tue Jul 25, 2023 7:10 am
by darkfrei
You can use 4 collision points as up, left, down, right.
So if you have collision with left or right point then stop the horizontal velocity, is with top or bottom points then stop the vertical velocity.

See code with this colliding physics here:
https://darkfrei.itch.io/connection-looper