我正在尝试创建一个围绕 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