I know there has been a lot of discussion about this already, but now I'd like to share my (not quite complete) implementation.
I added a test entry "Player Test" to Box2D 1.4.3, available for download here: http://www.sfu.ca/~mnb2/player_test.zip (using MSVS2008)
I'd like to point out a few hurdles, and how I've resolved them so that newer developers can get an idea of how to tackle these problems, and veterans can perhaps assist me in improving them further.
1. Player sticks to wall
If you set up your character with friction, and make it so that when you press 'd' for example your character moves right, and then jump into a wall, and continue to hold 'd', your player can get stuck to the wall because you are continuously applying a force against the player, which creates friction between the player and the wall, and won't allow him to fall.
There are a couple solutions to this problem. The easiest solution to set your character's friction to 0. The other solution is to detect if your player is touching a wall, and if so, don't apply a force.
2. Player moves too fast
I don't claim that this is the best solution, but it seems to work decently. You can apply an impulse at every frame to bring the player up to his maximum speed, but only if he is below the max speed already. This can be accomplished like so
The only problem I can see with this is that the impulse cap is frame rate dependent. For example, there is a difference if you apply an impulse of 50 every frame at 20 FPS or at 60 FPS. The article "fix your timestep" should help resolve this issue though.
3. Poor air control
In real life, if you're traveling 5 m/s horizontally when you jump, you will to continue to travel at that speed until you land again (air friction negligible). However, in games, this doesn't work so well.
Fortunately, this has a pretty simple solution too. You just have to check if your character is not on the ground, and then apply some linear damping like
if(!on_ground) {
m_body->m_linearVelocity.x *= 0.70f;
}
Although I did notice a peculiar problem with this. Usually it causes your character to gradually slow down when he's in the air, but sometimes he abruptly stops in mid-air. I'm not sure if this is because I'm hitting a min velocity threshold, or what's going on.
4. Player gets a lot of air time when going over a small bump
This problem has caused me the most grief. When walking at 7 m/s, and my character would walk over a little triangle or something, he'd "jump" several meters into the air. The only solution I've come up with so far, which doesn't work 100% of the time, is most easily expressed in code:
This says that if the character is not standing on the ground, and he hasn't jumped, but he's somehow traveling upwards, set his velocity to 0. It doesn't seem to catch every case though, thus sometimes he gets a lot more air than he should. Even when it does work, it doesn't look very good. It would be better if he followed the curvature of the ground.
5. Character slides down hills
In most games, you won't slide down a 45 degree incline. Increasing the friction might stop this, but then it makes it very difficult for your character to walk up the incline as well, plus, this recreates the wall-stick problem. Thus I haven't come up with an adequate solution to this yet (suggestions?).
A couple notes on the shape of your character:
I've split my character up into lower legs (a triangle to make it easier to walk up hills), upper legs, pelvis, stomach, chest and head. This is simply so that I can detect where a player has been shot and scale damages (a head shot does more damage than a leg shot). I'm also taking advantage of the fact that his legs have only 3 points to detect which shape I'm actually looking at... I haven't found a better way around this yet.
Anyways, I'd really like some discussion on this.
I added a test entry "Player Test" to Box2D 1.4.3, available for download here: http://www.sfu.ca/~mnb2/player_test.zip (using MSVS2008)
I'd like to point out a few hurdles, and how I've resolved them so that newer developers can get an idea of how to tackle these problems, and veterans can perhaps assist me in improving them further.
1. Player sticks to wall
If you set up your character with friction, and make it so that when you press 'd' for example your character moves right, and then jump into a wall, and continue to hold 'd', your player can get stuck to the wall because you are continuously applying a force against the player, which creates friction between the player and the wall, and won't allow him to fall.
There are a couple solutions to this problem. The easiest solution to set your character's friction to 0. The other solution is to detect if your player is touching a wall, and if so, don't apply a force.
2. Player moves too fast
I don't claim that this is the best solution, but it seems to work decently. You can apply an impulse at every frame to bring the player up to his maximum speed, but only if he is below the max speed already. This can be accomplished like so
Code:
if(move_right_key_is_being_pressed)
{
float impulse = mass_of_player*(max_run_velocity - current_velocity.x);
if(impulse > 0)
{
if(impulse > 50) impulse = 50; // this allows the character to accelerate up to his max speed, instead of instantly traveling at max_run_velocity
m_body->ApplyImpulse(b2Vec2(impulse,0),b2Vec2(0,0)); // apply the impulse to the center of your character's body
}
}
}
{
float impulse = mass_of_player*(max_run_velocity - current_velocity.x);
if(impulse > 0)
{
if(impulse > 50) impulse = 50; // this allows the character to accelerate up to his max speed, instead of instantly traveling at max_run_velocity
m_body->ApplyImpulse(b2Vec2(impulse,0),b2Vec2(0,0)); // apply the impulse to the center of your character's body
}
}
}
The only problem I can see with this is that the impulse cap is frame rate dependent. For example, there is a difference if you apply an impulse of 50 every frame at 20 FPS or at 60 FPS. The article "fix your timestep" should help resolve this issue though.
3. Poor air control
In real life, if you're traveling 5 m/s horizontally when you jump, you will to continue to travel at that speed until you land again (air friction negligible). However, in games, this doesn't work so well.
Fortunately, this has a pretty simple solution too. You just have to check if your character is not on the ground, and then apply some linear damping like
if(!on_ground) {
m_body->m_linearVelocity.x *= 0.70f;
}
Although I did notice a peculiar problem with this. Usually it causes your character to gradually slow down when he's in the air, but sometimes he abruptly stops in mid-air. I'm not sure if this is because I'm hitting a min velocity threshold, or what's going on.
4. Player gets a lot of air time when going over a small bump
This problem has caused me the most grief. When walking at 7 m/s, and my character would walk over a little triangle or something, he'd "jump" several meters into the air. The only solution I've come up with so far, which doesn't work 100% of the time, is most easily expressed in code:
Code:
if(!on_ground && !has_jumped && vel.y > 0)
{
m_body->m_linearVelocity.y = 0;
}
{
m_body->m_linearVelocity.y = 0;
}
This says that if the character is not standing on the ground, and he hasn't jumped, but he's somehow traveling upwards, set his velocity to 0. It doesn't seem to catch every case though, thus sometimes he gets a lot more air than he should. Even when it does work, it doesn't look very good. It would be better if he followed the curvature of the ground.
5. Character slides down hills
In most games, you won't slide down a 45 degree incline. Increasing the friction might stop this, but then it makes it very difficult for your character to walk up the incline as well, plus, this recreates the wall-stick problem. Thus I haven't come up with an adequate solution to this yet (suggestions?).
A couple notes on the shape of your character:
I've split my character up into lower legs (a triangle to make it easier to walk up hills), upper legs, pelvis, stomach, chest and head. This is simply so that I can detect where a player has been shot and scale damages (a head shot does more damage than a leg shot). I'm also taking advantage of the fact that his legs have only 3 points to detect which shape I'm actually looking at... I haven't found a better way around this yet.
Anyways, I'd really like some discussion on this.