views:

564

answers:

1

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?

+1  A: 

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
wow. that's weird, but it fixed it.
Rosarch
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