Page 1 of 2

Character movement on collision problem

Posted: Tue Mar 20, 2012 12:00 pm
by MiniDemonic
Hey, I have a problem trying to make the character move smoothly when colliding with a wall or ground.
Currently the character is jumping up&down/left&right and I can't figure out how to make him move smoothly.
I followed the AdvTileLoader tutorial for the collision detection and the code it gave me was:
I should not need to provide the collision code because it is the movement code that is messing with me.

Code: Select all

function on_collision(dt, shape_a, shape_b, mtv_x, mtv_y)
    -- sort out which one our hero shape is
    local hero_shape, tileshape
    if shape_a == circ and shape_b.type == "tile" then
        hero_shape = shape_a
    elseif shape_b == dude and shape_a.type == "tile" then
        hero_shape = shape_b
    else
        -- none of the two shapes is a tile, return to upper function
        return
    end
    
    -- why not in one function call? because we will need to differentiate between the axis later
    hero_shape:move(mtv_x, 0)
    hero_shape:move(0, mtv_y)
    emitterpos:move(mtv_x, 0)
    emitterpos:move(0, mtv_y)
end
I have searched but could not find anything about this problem. (might have searched for the wrong keywords tho)

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 1:35 pm
by molul
Could you attach a .love file so we can check it?

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 3:38 pm
by MiniDemonic
Sure, as of now the game looks like crap tho so don't hold it against me! :D

Updating the OP to include .love

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 4:35 pm
by molul
No offense, but the code it's a bit difficult to read :( You should try to split some functions like love.update in smaller functions (for the player, for the bullets, etc) and work better on comments (you could remove comments like "--load the level" before a line like "map = loader.load("level.tmx")", and instead ellaborate more on complicated chunks).

I'm sorry but I won't be able to help you (I'd need much more time) :(

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 4:50 pm
by MiniDemonic
Yes, I know. I'm currently cleaning up my code and such, but the problem I have is in the code I posted in the OP.
The character is flashing in all directions when moving towards a wall, I do not know how to fix this, trying to search for inspiration from other projects and such but I can't seem to find a way.

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 5:55 pm
by trubblegum
Who is circ?
What is dude?
Why will the axes become indistinguishable if they are altered?
What does this do?

Code: Select all

if shape_a.type == 'platform' or shape_b.type == 'platform' then
   if shape_a == dude or shape_b == dude then dude:move(...) else return end
else return end
And why, in heaven's name, is the sky blue?

It is usual to move, collide, adjust, draw, in that order.

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 6:08 pm
by MiniDemonic
You don't even need to know who dude is, or who circ is.
My problem as explained before and the only thing I need help with, is that the character is flashing in all directions when moving against a wall.
The only thing I need to know is how to counter this, as the current code moves him back out, is how to make it look smooth when he moving against a wall.

If you want to see what the problem looks like you can download and run the .love

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 6:16 pm
by coffee
trubblegum wrote: And why, in heaven's name, is the sky blue?
"dude", let me fix that

Code: Select all

sky = bloody_red
Look, it's MiniDemonic blood all over the sky! Someone smashed him a bit!

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 6:36 pm
by trubblegum
Aw, shucks .. well, if that's the only problem, try love.graphics.setwibblin_an_a_wobblinalloveradamnplace(false)

Re: Character movement on collision problem

Posted: Tue Mar 20, 2012 8:19 pm
by Ellohir
The usual first approach to collision resolution is "if the player enters a wall, push it out quickly". It works, but that makes it really ugly. What you want is to, in one only function, decide if the player can move or not. Doing so, the player is never "inside" the wall. You move the player where it wants, test collision, and let it be or return to previous position. You can also divide it along X and Y axis (so a diagonal move next to a wall moves along the wall).

If it's not clear yet, here's my implementation:

Code: Select all

function player_move(dt)

    local dx,dy=0,0
    
        -- player movement, including diagonal
    if love.keyboard.isDown("d") and love.keyboard.isDown("w") then
        dx = player.speed * dt * 0.7
        dy = - player.speed * dt * 0.7
    elseif love.keyboard.isDown("a") and love.keyboard.isDown("w") then
        dx = - player.speed * dt * 0.7
        dy = - player.speed * dt * 0.7
    elseif love.keyboard.isDown("d") and love.keyboard.isDown("s") then
        dx = player.speed * dt * 0.7
        dy = player.speed * dt * 0.7
    elseif love.keyboard.isDown("a") and love.keyboard.isDown("s") then
        dx = - player.speed * dt * 0.7
        dy = player.speed * dt * 0.7
    -- straight player movement
    elseif love.keyboard.isDown("d") then
        dx = player.speed * dt
    elseif love.keyboard.isDown("a") then
        dx = - player.speed * dt
    elseif love.keyboard.isDown("w") then
        dy = - player.speed * dt
    elseif love.keyboard.isDown("s") then
        dy = player.speed * dt
    else
        player.walked = player.walked - player.speed* dt
    end
    
    local currX=player.x
    player.x=player.x+dx    
    if player_collides(player,tiles) then 
        player.x=currX
    end
    
    
    local currY= player.y
    player.y=player.y+dy
    if player_collides(player,tiles) then 
        player.y=currY
    end
    
end