8

I think I've configured Box2d to have some sort of maximum velocity for any body, but I'm not sure. I apply an impulse like (100000000, 100000000), and the body moves just as fast as (100, 100) - which is not that fast at all.

I'm using the Box2d XNA C# port.

My game is a top-down 2d.

Here is some code that may be relevant:

private readonly Vector2 GRAVITY = new Vector2(0, 0);

    public void initializePhysics(ContactReporter contactReporter)
    {
        world = new World(GRAVITY, true);
        IContactListener contactListener = contactReporter;
        world.ContactListener = contactListener;
    }

    public void Update(GameTime gameTime)
        {
     // ...

            worldState.PhysicsWorld.Step((float)gameTime.ElapsedGameTime.TotalSeconds, 10, 10);

     //...
        }

Here is some example code that applies the impulse:

    private void ApplyImpulseFromInput()
    {
        Vector2 movementImpulse = new Vector2();

        if (inputReader.ControlActivation(ActionInputType.MOVE_LEFT) == 1f)
        {
            movementImpulse.X = -Constants.PLAYER_IMPULSE_CONSTANT;
        } else if (inputReader.ControlActivation(ActionInputType.MOVE_RIGHT) == 1f)
        {
            movementImpulse.X = Constants.PLAYER_IMPULSE_CONSTANT; ;
        }

        if (inputReader.ControlActivation(ActionInputType.MOVE_UP) == 1f)
        {
            movementImpulse.Y = -Constants.PLAYER_IMPULSE_CONSTANT; ;
        } else if (inputReader.ControlActivation(ActionInputType.MOVE_DOWN) == 1f)
        {
            movementImpulse.Y = Constants.PLAYER_IMPULSE_CONSTANT; ;
        }

        model.Body.ApplyImpulse(movementImpulse, model.Position);
    }

If Constants.PLAYER_IMPULSE_CONSTANT is anywhere from 1000f to 1000000000f, the player can move at most (-120, -120) to (120, 120). If the constant is less, like 1f, the player will move more slowly.

This code is used to set up physics for everything in the game world:

        controller.Model.BodyDef = new BodyDef();
        controller.Model.BodyDef.type = controller.Model.Mobile ? BodyType.Dynamic : BodyType.Static;
        controller.Model.Body = worldState.PhysicsWorld.CreateBody(controller.Model.BodyDef);
        controller.Model.Body.SetLinearDamping(10.0f);

Could it possibly be the linear damping? I changed it from 10.0f to 0, with no effect.

UPDATE: Weirdness with linear damping: I have made these observations on the body that is moved with the apply impulse method above:

Linear Damping       Max Speed
0f                   120
10f                  120
50f                  120
55f                  90
60f                  0
70f                  0
100f                 0
100000f              0

Why is there a range of sensitivity in linear damping between 50f and 60f?

Nick Heiner
  • 108,809
  • 177
  • 454
  • 689

2 Answers2

15

That's not the way to fix that problem. You are supposed to scale down your objects when creating them in the box2D world. Then you can just scale the information that box2D gives you back to the size of your world.

Let's say for example that a box in my world is 120 pixels long. If I scaled the object down by 30 times so that it can be simulated by box2d properly then the length of my box in the box2D world would be 4 "pixels" long (120 / 30 or size of your object / scale). Now, let's say that box2D calculates that my box moved 3 "pixels" in the box2d world. I could then grab that information and scale it back up to my world's size which would mean that the box just moved 90 pixels (3 * 30). I hope that didn't sound too confusing. I usually find it hard to explain myself.

Robert
  • 151
  • 1
  • 3
  • I had the same problem, and scaling down the size of my simulation fixed things. – Nathan S. Mar 14 '11 at 17:36
  • 5
    +1. From the Box2D manual: "Box2D is tuned for MKS units. Keep the size of moving objects roughly between 0.1 and 10 meters. You'll need to use some scaling system when you render your environment and actors. The Box2D testbed does this by using an OpenGL viewport transform." Use the engine examples eg. HelloWorld to reference your own units against. – Engineer Aug 02 '11 at 10:53
3

I have the same problem with version 2.1a i found out that changing line 128 in b2Settings.as can help. Change static public const b2_maxTranslation:Number = 2.0 to a higher number. sure that this isn't the right way, but right now dont really know how to do it correctly.

Gillis
  • 46
  • 1
  • 8
    I just ran into this problem today, except I was using the C++ version of Box2D. The comments read: "The maximum linear velocity of a body. This limit is very large and is used to prevent numerical problems. You shouldn't need to adjust this." My simulation was set to 1 pixel equals 1 Box2D unit. I was having the same problem with velocity capping out. Sounds like Box2D likes you to keep your simulation at a smaller scale. – don May 27 '10 at 22:36
  • Yep, you can do this, but it's not the right way. See Robert's answer. – Engineer Aug 02 '11 at 09:44