Player Control

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

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
    }
}
}


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;
      }


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.
Just a little update:

It's seems to be better to apply a force parallel to the ground when walking left or right. This makes it much easier for your character to walk up hills and over little bumps, but more importantly, it also prevents your character from getting air when walking down a slope because it also adds a downwards force. Actually, this is so effective that instead of pushing the little blocks in my test demo like he used to, he walks right over top of them. Your character should even be able to walk up stairs quite easily with this method.

(Is anyone interested in this stuff, or are you all too intimidated by my really long post?)

Updated: http://www.sfu.ca/~mnb2/player_test2.zip
Interesting stuff, I was going to try your demo out but haven't gotten around to it. Unfortunately my current game is space-ish, no gravity 🙁 For this first game I can't afford gravity 😉 Your posts are appreciated though and could be very useful when I get to making my grappling hook plaformer 😀
darkzerox wrote:
Just a little update:

It's seems to be better to apply a force parallel to the ground when walking left or right. This makes it much easier for your character to walk up hills and over little bumps, but more importantly, it also prevents your character from getting air when walking down a slope because it also adds a downwards force. Actually, this is so effective that instead of pushing the little blocks in my test demo like he used to, he walks right over top of them. Your character should even be able to walk up stairs quite easily with this method.

(Is anyone interested in this stuff, or are you all too intimidated by my really long post?)

Updated: http://www.sfu.ca/~mnb2/player_test2.zip



WTH!?!? newer project files...I can't open this ...
allanon wrote:
WTH!?!? newer project files...I can't open this ...


Yeah... sorry about that, I like to use the latest software 🙂 Anyways, all the *.cpp and *.h files are there if you really wanted to go through the trouble of making the project file yourself (could just use the old Box2D 1.4.3 solution and update the files). Or, I could post up the .exe if you wanted.

brainclog wrote:
Interesting stuff, I was going to try your demo out but haven't gotten around to it. Unfortunately my current game is space-ish, no gravity 🙁 For this first game I can't afford gravity 😉 Your posts are appreciated though and could be very useful when I get to making my grappling hook plaformer 😀


Haha..good thinking. Even though Box2D does most of the work, it still managed to create a lot of hassle!
(I was planning on putting grappling hooks in too... but not as a major part of the game)
Hi,

I'm trying to port your demo to the Java version and I've got the bits working quite well so far. For the project I need this we didn't need jumping so I removed that part of the code. I did, however, have some problems with the loop that checks if you're on the ground. How can I make this work with other shapes like circles?

Here's what I have now:
Code:
        for (ContactNode c = player.getContactList(); c != null; c = c.next) {
            // only check if legs are touching ground
            for (Manifold m : c.contact.GetManifolds()) {
                // make sure the ground is not vertical
                if (m.normal.y > 0) {
                    norm = m.normal;
                    on_ground = true;
                    break;
                }
            }
            if (on_ground) {
                break;
            }
        }


I had to remove the code that checked the vertexcount (I didn't realy get this part anyway?) because it threw an exception when hitting a circle shape (obviously).
SaphuA wrote:
Hi,

I'm trying to port your demo to the Java version and I've got the bits working quite well so far. For the project I need this we didn't need jumping so I removed that part of the code. I did, however, have some problems with the loop that checks if you're on the ground. How can I make this work with other shapes like circles?

Here's what I have now:
Code:
        for (ContactNode c = player.getContactList(); c != null; c = c.next) {
            // only check if legs are touching ground
            for (Manifold m : c.contact.GetManifolds()) {
                // make sure the ground is not vertical
                if (m.normal.y > 0) {
                    norm = m.normal;
                    on_ground = true;
                    break;
                }
            }
            if (on_ground) {
                break;
            }
        }


I had to remove the code that checked the vertexcount (I didn't realy get this part anyway?) because it threw an exception when hitting a circle shape (obviously).


Sorry I didn't see this earlier. That part about checking vertex count -- it was sort of a hack to check that the shape I was looking at was the legs. I knew the legs had 3 vertices, and the other shapes didn't.

I don't see any reason this shouldn't work for other shapes... try printing out the normal and make sure it looks right.

I haven't worked on this player test for awhile now... it's far from complete. Let me know if you come up with any improvements?
Extremely useful bit of code 🙂

Has anyone got this working in 2.0.1? I'm new to Box2D, so I'm having a bit of difficulty working out how the API has changed between 1.4.3 and 2.0.1. I've managed to change the stuff for creating the shapes and bodies but the stuff to check the if the "legs" are touching the ground is confusing me a bit. There doesn't seem to be a struct or class called b2ContactNode any more, and b2Body no longer has a GetContactList() method. I guess this has been replaced with the contact listener stuff but I'm not sure how to translate what's there.

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