Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

c# - Box2d: Maximum possible linear velocity?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...