2010-07-30 25 views
5

我正在试图创建一个动态的物体,它围绕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 

回答

6

身体,你是正确的。节约能源应该确保身体的速度保持不变。

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

底线:期望速度与您开始时的速度保持完全一致是不合理的,您需要进行补偿。根据您的需要,您可以手动设置每个框架的速度,也可以使用电机固定在行星上的旋转关节。

+0

谢谢你的解释!这真的很有道理,当我们从计算机科学角度思考它时,所有的舍入误差......我想我只是期待纯物理:) – 2010-08-12 18:09:17

0

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