尽管在 box2d 中具有巨大的重力,但移动速度太慢

本文关键字:移动 速度 box2d 巨大 | 更新日期: 2023-09-27 18:35:19

我在CocosSharp中玩box2d。而且我确实有一个问题 - 物体移动太慢了!尽管有非常大的价值...

// Physics
    public const float GRAVITY = -99999999999999999999999999999f;
    public const int PTM = 32;
    public const float ROCKET_MASS = 10000f;
    public const float TIMESTEP = 1f/240f;    

请注意,我已经更改了时间步长。我试图使用质量,但它绝对没有意义!

// Add rocket's physics
        var def = new b2BodyDef ();
        def.allowSleep = true;
        def.position = new b2Vec2(0.0f, 0.0f);
        def.type = b2BodyType.b2_dynamicBody;
        body = world.CreateBody (def);
        // body.Mass = Globals.ROCKET_MASS;
        body.SetActive(true);
        b2PolygonShape bodyBox = new b2PolygonShape();
        bodyBox.SetAsBox(0.1f , 0.5f);  // !
        b2FixtureDef bodyFixture = new b2FixtureDef();
        bodyFixture.shape = bodyBox;
        bodyFixture.density = 0.5f;
        bodyFixture.friction = 0.4f;
        body.CreateFixture(bodyFixture);    

以下是我初始化物理的方法:

void InitPhysics ()
    {
        // Init the world
        var gravity = new b2Vec2 (0.0f, Globals.GRAVITY);
        world = new b2World (gravity);
        world.SetAllowSleeping (true);
        world.SetContinuousPhysics (true);
        // Now init the ground
        new Ground(world);
    }    

这是我的渲染器代码:

private void StartScheduling()
    {
        Schedule(t =>
            {
                if(t <= Globals.TIMESTEP)
                    timeAccumulator += t;
                else
                    timeAccumulator += Globals.TIMESTEP; 
                while (timeAccumulator >= Globals.TIMESTEP)
                {
                    world.Step(Globals.TIMESTEP, 6, 2);
                    timeAccumulator -= Globals.TIMESTEP;
                    // If touched, then do apply force
                    if (touch)
                    {
                        rocket.body.ApplyLinearImpulse(new b2Vec2(0f, 10000f), b2Vec2.Zero);
                    }
                    world.DrawDebugData();
                    rocket.UpdateRocketPosition();
                }
                world.ClearForces();
            });
    }    

我太累了,请帮忙!火箭必须迅速坠落,但它没有!

尽管在 box2d 中具有巨大的重力,但移动速度太慢

感谢@iforce2d。

解决方案是在将结果渲染到屏幕时使用 PTM 调整 box2d 单位。至于我,我不明白,box2d 世界是一个非常小的东西,不依赖于设备屏幕尺寸,我应该乘以(放大)进行渲染。现在它工作得很好:

public void UpdateRocketPosition ()
    {
        // PositionX, Y are sprite's positions
        PositionX = body.Position.x * Globals.PTM;
        PositionY = body.Position.y * Globals.PTM;
    }