Get the direction of a current collision

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
JuanjoSalvador
Prole
Posts: 26
Joined: Wed Nov 27, 2019 9:19 pm
Location: Almeria, ES
Contact:

Get the direction of a current collision

Post by JuanjoSalvador »

Hi!

I'm working on a gravity-based puzzle game, which actually is like any platformer but without the jumping skills, instead of jump, you can invert of the gravity. I'm using the love.physics to set movement and collisions, also Tiled and STI to create the maps, so to set which tiles are ground and roof, I added a custom property to the tiles, and to set whenever the player is "grounded" (on floor or root, so it can move and invert gravity), it has its own property which is updated on the preSolve() callback.

Code: Select all

function preSolve(a, b, coll)
    if a:getUserData().properties.type == "ground" then
        player.grounded = true
    else
	player.grounded = false
    end
end
This solves my last problem, but created a new one. When the player is hitting a wall (not marked as ground), player.grounded becomes false and the player cannot move or invert the gravity. I think the best solution will be get the direction of the ground+player collision ir order to set the grounded property, but... I don't know how to get this.
itch.io profile - When I'm uploading my works and devblogs
GitHub - All my code is here!
Horchata - A set of Lua modules I wrote to make some task easier
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Get the direction of a current collision

Post by pgimeno »

hi Juanjo! Hm, I see a problem with setting player.grounded = false. I think you should set it to false right before updating the world, and set it to true when the player is touching the ground (but not to false). That is:

Code: Select all

function preSolve(a, b, coll)
    if a:getUserData().properties.type == "ground" then
        player.grounded = true
    end
end

function love.update(dt)
    player.grounded = false
    world:update(dt)
    if player.grounded then
        -- allow jump
    end
end
I have no idea about the layout of your program, so consider the love.update function just the schematics of what I meant.

P.S. Is that horchata from Alboraya? ;)
User avatar
JuanjoSalvador
Prole
Posts: 26
Joined: Wed Nov 27, 2019 9:19 pm
Location: Almeria, ES
Contact:

Re: Get the direction of a current collision

Post by JuanjoSalvador »

I had a workaround by setting the restitution of the walls to 0.8, so the player would be never stucked them, but switching the order of the world:update() works better! All the player's update functions are into the map:update(), following the STI guidelines (but not my cup of tea, to be honest).

Thanks for the solution Pedro! And, maybe, actually I took that name because I was drinking horchata when I wrote the modules (and becayse I love it) :-)
itch.io profile - When I'm uploading my works and devblogs
GitHub - All my code is here!
Horchata - A set of Lua modules I wrote to make some task easier
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 119 guests