我正在試圖創建一個動態的物體,它圍繞Box2D中的一個靜態物體進行軌道運動。 我有一個零重力世界,和一個連接兩個身體的DistanceJoint
。我已經消除了身體和關節的所有摩擦和阻尼,並且正在向動體施加初始線速度。其結果是人體開始繞軌道運行,但隨着時間的推移,其速度逐漸減小 - 這在沒有摩擦的零重力環境中我並不期待。零重力box2d世界中的遞減速度
我做錯了什麼?線性速度應該在每一步重新創建,還是我可以將這項工作委託給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
謝謝你的解釋!這真的很有道理,當我們從計算機科學角度思考它時,所有的舍入誤差......我想我只是期待純物理:) – 2010-08-12 18:09:17