代码之家  ›  专栏  ›  技术社区  ›  Alex Gyoshev

零重力世界中的速度递减

  •  5
  • Alex Gyoshev  · 技术社区  · 14 年前

    我试图在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
    
    2 回复  |  直到 14 年前
        1
  •  6
  •   Thomas    14 年前

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

        2
  •  0
  •   Rajavanya Subramaniyan    13 年前

    试着检查线性阻尼和角度阻尼的数值,使它们为零。这也许能解决问题