Player movement & max speed

Hello all!

We're making a heavily physics based 2D sidescroller and ran into a fairly big problem.

I want the player to move at a max speed with a slight acceleration/deceleration from/to standstill, without affecting forces caused by gravity or collisions etc too much. The player will often hold other objects, which should weigh him down.
setLinearVelocity won't allow the last requirement, otherwise it'd have to do...
With other (3D) physics engines, I've been able to use a "2D planar joint" which limited the player's movement to the XY plane. On this joint I used a motor to move the character in the way I described above.

The problem is, I can't find a suitable joint for this in Box2D...

Ideas?
How is the player holding an object? Is it a merged body or is it attached by some joint?

You can try applying a corrective impulse/force that is capped.

Creating motors for planar movement should be easy. I'll add that to the TODO list.
Thanks for the quick reply!

The object bodies, when held, are attached to the player's body using a distance joint (to simulate an electromagnet-effect).

Well, there are actually bodies between the actual player and the held object (which gets attached to the end of a "chain"), but those are irrelevant in this case...

I'd give planar motors a shot myself, had it not been too long since I did physics math... :s

I'm currently trying to cap the applied force like you mentioned, based on the player's current linear velocity. But if I stop moving up, gravity gets the upper hand and soon starts pulling the player down faster than the max speed limit, which effectively cuts any applied forces to 0 until the player hits the ground. Will see if ignoring motion in Y+ (down) when capping helps...
Hmm, I can't seem to find a good way to do this, everything I've tried so far has had weird consequences (limiting fall speed while moving left/right for example).

How are everyone else solving things like this?.
@TwoD:

I'm not sure if I'm getting what the problem is exactly, but here's how I understand you: You want to limit the player speed because of carrying heavy objects, but limiting the x-axis velocity results in the player slowing down when falling down a slanted hill or something similar. Close?

In that case maybe you wouldn't want to really limit the speed, but just stop the player from making their character move any faster with the controls while going above some maximum speed. Sort of like pedaling your bike when you're in a really low gear going down a big hill (does nothing). I would just put a check in the input handling code for horizontal movement that if the player is above a certain velocity on the x-axis to not apply any impulse/force.

This way the player could fall down a hill potentially faster than they could move normally. If you still want to limit maximum speed overall for physics engine stability, I have already posted some code in the Flash section (it's still the most recent post as of when I started writing this) that does exactly that, which I am using in my space-like game. Actually I think there is code somewhere in the SVN that does the same thing because I see that feature is marked as done on the TODO list.

Hope this helps!
@brainclog:

Nearly, I want to limit the player speed, while being able to carry heavy objects (which would just cause less acceleration with heavier objects). Some objects will be too heavy to lift but the player should still be able to push/pull them around.

The player is able to move both himself and objects in both x and y (ie, he's floating around), but with gravity pulling down in y (might change with the level). So his speed limiting should be act as if the length of the linear velocity vector was clipped or he'd be able to move faster diagonally than just horizontally/vertically, without affecting gravitational effetcs. This is what I'm having problems with.

When I tried to clip the vector like that (by normalizing and multiplying by max speed if it was too high), it meant gravity would also be limited while I gave input commands since that was the only time the clipping was done.

I tried your suggestion to not apply force in a direction when moving too fast that way, but this resulted in jerky movement and allowed the player to move fast diagonally.

Here's what the my current movement-related code looks like:
Code:
...
   b2Vec2 movement(0.0f, 0.0f);
   while(iter!=list.end()) // Loops through a list of player-related events
   {
      switch((*iter).type)

      {
      case PLAYER_MOVE_DOWN:
         if(m_body->GetLinearVelocity().y<m_maxSpeed)
         {
            movement.y=1;
         }
         break;
      case PLAYER_MOVE_LEFT:
         if(m_body->GetLinearVelocity().x>-m_maxSpeed)
         {
            movement.x=-1;
         }
         break;
      case PLAYER_MOVE_RIGHT:
         if(m_body->GetLinearVelocity().x<m_maxSpeed)
         {
            movement.x=1;
         }
         break;
      case PLAYER_MOVE_UP:
         if(m_body->GetLinearVelocity().y>-m_maxSpeed)
         {
            movement.y=-1;
         }
         break;
...
   movement.Normalize();
   m_body->WakeUp();   // Wake the body in case it has fallen asleep
   m_body->ApplyForce( m_accelerationForce*movement , m_body->GetCenterPosition() );
...


I'll look through your source and see if I can use some of it.
Have you tried just capping the velocity vector all the time? If so, what is it about that behavior that you don't like? If that isn't the desired behavior, and you want to be able to fall faster because of gravity than you can accelerate to with the controls then I don't think it would be incredibly hard, just a little tricky. You would have to check the velocity vector when moving, and if it's less than the maximum that you want the player to be able to accelerate to, don't just apply the full acceleration, but scale it so that it only goes up to the maximum. This might take a little math to figure out though. An easier solution might be to just apply impulses instead of forces and then check the velocity in the movement code after movement is allowed, and cap the velocity if necessary. If you're already moving faster than the maximum, then no movement acceleration will take place, and thus no capping.

This should allow the player controls to have no effect above a certain movement speed, so they can fall fast, but only move up to whatever speed you want to allow because of the weight attached to them.

Any luck yet?

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