Platformer Character friction

When trying to make platformer-style characters the friction model in Box2D feels unrealistic. This is because characters are moved by sliding across the ground, whereas the character is suppose to be walking/running (i.e. not dragging feet).

This causes the following undesirable behaviors:
1) When a character is standing on the top of a stack of boxes & runs off to the left, the boxes topple to the left. The expected behavior would be that they fall to the right, but the friction between the character & box is pushing left.
2) When a character jumps while running & hits his head on the ceiling, he slows down. This could be fixed by making all ceilings have zero friction, but platformers often have the ground/ceiling as the same 'block'. Making the ceiling zero friction doubles your world shapes (worst case)
3) When a character is running & jumping, he slows down quite a bit when he lands because the friction kicks in.

I've looked at different ways of working around these issues, but so far I've not come up with something that works without modifying box2d itself. What I need is more control over friction...here is what I've tried:

* Make the characters heads have zero friction - downside is that other characters can't stand on the head without sliding off.
* Turn off character friction and fake it - this kinda works until you get into kinematic platforms. Then it gets complicated...
* ignore some of these issues and apply a 'counter force' to the platform (fixes #1)

I'm not happy with any of these, so I'm curious what others have done with character friction?
A lot of this reply is copy-pasted from another post I made on the forums a while ago, but I've updated it a bit with changes I've noticed since the last time I talked about this. Hopefully this will give you some ideas as to what I've tried and seems to be working well now...


It seems that there have been many people who have wanted to make a good classical platformer using box2d. Most of what you want can be accomplished rather simply, but there are things I'm finding which cause me to be fighting box2d the whole way. I spent a while studying other platformers, specifically New Super Mario Bros Wii. They have wall sliding, wall jumping, spinning moving platforms that you can stand on and stick to nicely, variable jump height, and some other odds and ends. It's a whole lot to try and implement all at once, so I decided to do it one piece at a time and build up to something much more complex.

So far what I'm doing is:
Start by making your character have fixed rotation. This will prevent them from toppling over.
Also, my main character is shaped like a capsule. This will help keep your character from getting stuck in certain edges.
Then make sure friction and restitution are both at 0. This makes sure your character wont "stick" to walls, slow down when they hit the ceiling, and wont bounce. You want to have full control over the speed of the character if you want it to feel good. Box2d is a fantastic tool, but it's not a solution for making a platforming game.
Input from the player adjusts an amount of force I currently have as:
Code:
if (mMoveRight) {
    inputXForce = (mMaxXVelocity - xVel);
}
if (onGround)
    inputXForce *= mGroundXAcceleration;
else
    inputXForce *= mAirXAcceleration;

I call ApplyForce with this every time I update the physics simulation (when I add to my accumulator re:gafferongames "fix your timestep" article).

For jumping, I start by checking if the player is on the ground based on the contacts. If so, I call ApplyImpulse with a jump impulse value once as the player presses the jump key. If you want to do variable jump height (the longer the player holds the jump button, the higher they go) I've found something along the lines of:
Code:
float jumpMod = -1*(getYVelocity()/mVelocityFactor);
ApplyImpulse( 0, -( mJumpImpulse*(jumpMod) )/2);

where mJumpImpulse was the value applied when they hit the jump key works quite nicely when the player releases the jump key. This is enough to slow the character's Y velocity enough so the player has the control they'd like.

If you are working with slopes:
Another problem you'll run into is that whenever a player is standing on any type of slope, they'll start to slide down it because of the 0 friction and gravity pushing down on him. To solve this, I don't apply a force of gravity on the player while they are on a slope less than some given angle. The player won't slide when they are on a slight slope or almost flat ground, and when they are on a greater slope they will slide down it. This actually feels really good because the player can run up slopes, but once they hit a certain threshold they seem to slide back down it naturally. BUT this introduces another problem... because the player has no gravity pushing him down, he wont exert a force onto objects he is standing on. Right now I fix this by taking the force that would be applied to the player, and instead apply it to the object the player is standing on. This seems to work well enough, and I haven't noticed any issues with it so far (Note: Does not mean it has no issues!).

If you are planning on including moving platforms in your game:
Every game update, I check what platform the player is currently standing on. If the platform is the same as it was last loop, I check if the position of it has changed. If it has changed, I update the character's position accordingly. Something along the lines of:
Code:
// If I am on the same object I was standing on last update
setPosition( myXPos + (platform->getXPos()-mLastPlatformXPos), myYPos + (platform->getYPos()-mLastPlatformYPos));



Feel free to ask for more explanation on any of this...

Original thread: https://box2d.org/forum/viewtopic.php?t=4733