Character movement on collision problem

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.
MiniDemonic
Prole
Posts: 28
Joined: Tue Mar 20, 2012 10:39 am

Character movement on collision problem

Post 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)
Attachments
thedude.love
control with w,a,s,d
(65.58 KiB) Downloaded 158 times
Last edited by MiniDemonic on Tue Mar 20, 2012 3:45 pm, edited 2 times in total.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Character movement on collision problem

Post by molul »

Could you attach a .love file so we can check it?
MiniDemonic
Prole
Posts: 28
Joined: Tue Mar 20, 2012 10:39 am

Re: Character movement on collision problem

Post 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
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Character movement on collision problem

Post 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) :(
MiniDemonic
Prole
Posts: 28
Joined: Tue Mar 20, 2012 10:39 am

Re: Character movement on collision problem

Post 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.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Character movement on collision problem

Post 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.
MiniDemonic
Prole
Posts: 28
Joined: Tue Mar 20, 2012 10:39 am

Re: Character movement on collision problem

Post 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
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Character movement on collision problem

Post 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!
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Character movement on collision problem

Post by trubblegum »

Aw, shucks .. well, if that's the only problem, try love.graphics.setwibblin_an_a_wobblinalloveradamnplace(false)
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Character movement on collision problem

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests