Games Player is stuck on walls (Box2d - Tutorial)


Licensed User
Longtime User
[PLAYER = guy that plays the game | player = character in the game]


one thing you will notice when you start working with box2d is that not everything works as you imagine it :p

a box2d body has much definition (restitution, Density, friction, type,...)
now moving your body in a box2d world should be done by applying forces or impulse to your body to get the right physic filling. just setting the position each frame or applying a constant velocity wont look very good.

the problem is when you have a static body (wall) and a dynamic body (player) and you apply consistently force on the x axis to the player then when he will be pushed on a wall and under that wall there is no ground ...

example image:


he wont slide down and will be stuck on this wall IF the friction of the body and wall is > 0

the friction and consistently force will hold the player on his position. now if you ask why is the force consistently?? the reason is if the PLAYER (the guy that plays the game) wont release the left/right button (where you apply the force to the player) then the player (game character) will always be applied a force since the button is still clicked by the PLAYER.

now you can say just change the friction of the wall. ok this will work but a wall is often also a ground! so that means the player will slide on it.
so we could always check when player hit the wall we stop applying force but you will need to check from where the collision was because a wall is often a ground too and we still want our player run on it.

so what i did is i changed the player friction to 0. so now the player is sliding on walls AND on ground.

and what i also do is when PLAYER release the button i change slowly the player velocity until it is 0 and like this the player wont slide consistently on the ground.

this solved me the problem my player stucking on walls.


            If drawleft = True Then
                Dim desiredVel As Float = Min(0,player.getLinearVelocityFromWorldPoint(player.WorldCenter).x+(speed*0.1))
                Dim desiredVel As Float = Max(0,player.getLinearVelocityFromWorldPoint(player.WorldCenter).x-(speed*0.1))
            End If

changing the stepp (speed*0.1) will let your player slide longer on the ground like if he is on ice you could change the step to (speed*0.01)

Have fun coding! :)