【问题标题】:Diminishing speed in zero gravity box2d world零重力box2d世界中的递减速度
【发布时间】:2010-07-30 22:04:52
【问题描述】:

我正在尝试在 Box2D 中创建一个围绕静态物体运行的动态物体。 我有一个零重力世界和一个连接两个物体的DistanceJoint。我已经消除了身体和关节的所有摩擦和阻尼,并且正在对动态身体应用初始线速度。结果是物体开始绕轨道运行,但它的速度随着时间的推移而减小——我不希望在没有摩擦的零重力环境中出现这种情况。

我做错了吗?是否应该在每一步重新创建线速度,还是我可以将这项工作委托给 Box2D?

以下是相关代码:

// positions of both bodies

Vector2 planetPosition = new Vector2(x1 / Physics.RATIO, y1 / Physics.RATIO);
Vector2 satellitePosition = new Vector2(x2 / Physics.RATIO, y2 / Physics.RATIO);


// creating static body

BodyDef planetBodyDef = new BodyDef();
planetBodyDef.type = BodyType.StaticBody;
planetBodyDef.position.set(planetPosition);
planetBodyDef.angularDamping = 0;
planetBodyDef.linearDamping = 0;

planetBody = _world.createBody(planetBodyDef);

CircleShape planetShapeDef = new CircleShape();
planetShapeDef.setRadius(40);

FixtureDef planetFixtureDef = new FixtureDef();
planetFixtureDef.shape = planetShapeDef;
planetFixtureDef.density = 0.7f;
planetFixtureDef.friction = 0;

planetBody.createFixture(planetFixtureDef);

// creating dynamic body

BodyDef satelliteBodyDef = new BodyDef();
satelliteBodyDef.type = BodyType.DynamicBody;
satelliteBodyDef.position.set(satellitePosition);
satelliteBodyDef.linearDamping = 0;
satelliteBodyDef.angularDamping = 0;

satelliteBody = _world.createBody(satelliteBodyDef);

CircleShape satelliteShapeDef = new CircleShape();
satelliteShapeDef.setRadius(10);

FixtureDef satelliteFixtureDef = new FixtureDef();
satelliteFixtureDef.shape = satelliteShapeDef;
satelliteFixtureDef.density = 0.7f;
satelliteFixtureDef.friction = 0;

satelliteBody.createFixture(satelliteFixtureDef);

// create DistanceJoint between bodies

DistanceJointDef jointDef = new DistanceJointDef();        
jointDef.initialize(satelliteBody, planetBody, satellitePosition, planetPosition);
jointDef.collideConnected = false;
jointDef.dampingRatio = 0;

_world.createJoint(jointDef);

// set initial velocity

satelliteBody.setLinearVelocity(new Vector2(0, 30.0f)); // orthogonal to the joint

【问题讨论】:

    标签: android physics box2d rokon


    【解决方案1】:

    从物理上讲,你是对的。能量守恒应确保身体的速度保持恒定。

    但是,Box2D 不能完美地代表物理学。每一帧都会有一个小错误,这些错误加起来。我不知道 Box2D 是如何处理关节的,但是如果它将对象的位置投影到一个圆上,这将导致在一帧期间行进的距离比没有关节时的距离略小。

    底线:期望速度与您开始时完全相同是不合理的,您需要进行补偿。根据您的需要,您可以在每帧手动设置速度,也可以使用固定在行星上的旋转接头和电机。

    【讨论】:

    • 感谢您的解释!这真的很有意义,当人们从计算机科学的角度考虑它时,所有的舍入错误......我想我只是期待纯物理:)
    【解决方案2】:

    尝试检查 linearDamping 和 angularDamping 数字并将它们设为零。这可能会解决问题

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多