Box2D distance在AndEngine中不能保持形状

时间:2013-01-14 02:10:01

标签: android box2d andengine

我正在使用Box2D distanceJoint和AndEngine来保持形状。但是形状不能保持碎片刚落到地板上。它应该保持圆形/椭圆形。你能让我知道我做错了什么吗?我在这里与邻居保持距离。我应该与其他每个成员保持距离吗?

    for (int i=1; i<nBodies; ++i) {
        FIXTURE_DEF = PhysicsFactory.createFixtureDef(25f, 0.0f, 1f);

        float angle = map(i, 0, nBodies, 0, 2*3.1415f);
        float x = cx + rx * (float)Math.sin(angle);
        float y = cy + ry * (float)Math.cos(angle);
        // Add plate    
        float x1 = (x + 10) * 30;
        float y1 = y * 20;
        centers[i][0] = x1;
        centers[i][1] = y1;

          Vector2 v1 = new Vector2(x1,y1);

          circle = new AnimatedSprite(x1, y1, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
          circleBody[i] = PhysicsFactory.createCircleBody(this.mPhysicsWorld, circle, BodyType.DynamicBody, FIXTURE_DEF);
          this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(circle, circleBody[i], true, true));

          this.mScene.attachChild(circle);

    }

    for(int i= 1;i < nBodies -1; i++)  {

        Vector2 v1 = new Vector2(centers[i][0]/PIXEL_TO_METER_RATIO_DEFAULT,centers[i][1]/PIXEL_TO_METER_RATIO_DEFAULT);
        Vector2 v2 = new Vector2(centers[i+1][0]/PIXEL_TO_METER_RATIO_DEFAULT,centers[i+1][1]/PIXEL_TO_METER_RATIO_DEFAULT);
        Log.d("Vals","vals x:" + centers[i][0] + "y:" + centers[i][1] + "x:" +centers[i+1][0] + "y:" + centers[i+1%19][1]);
        int j = i+1;
        Log.d("i","i" + i + "   " + j);
        distanceJoint.initialize(circleBody[i], circleBody[(i+1)], v1, v2);
        distanceJoint.collideConnected = true;
        distanceJoint.dampingRatio = 10.0f;
        distanceJoint.frequencyHz = 1.0f;
        this.mPhysicsWorld.createJoint(distanceJoint);
    }

    Vector2 v1 = new Vector2(centers[1][0]/PIXEL_TO_METER_RATIO_DEFAULT,centers[1][1]/PIXEL_TO_METER_RATIO_DEFAULT);
    Vector2 v2 = new Vector2(centers[19][0]/PIXEL_TO_METER_RATIO_DEFAULT,centers[19][1]/PIXEL_TO_METER_RATIO_DEFAULT);
    distanceJoint.initialize(circleBody[1], circleBody[19], v1, v2);
    distanceJoint.collideConnected = true;
    distanceJoint.dampingRatio = 10.0f;
    distanceJoint.frequencyHz = 1.0f;
    this.mPhysicsWorld.createJoint(distanceJoint);

我错过了什么吗?请告诉我。

1 个答案:

答案 0 :(得分:0)

我想我已经通过保持每个身体之间的距离关节来解决它。我试图在http://www.jbox2d.org/中重新创建Blob演示。这个演示使用了ConstantVolumeJoint。我已经能够通过每个身体之间的距离关节重建这个,如下所示

    for(int i= 0;i < nBodies-1; i++)  {

       for(int j=i+1; j< nBodies ; j++){
           Vector2 v1 = new Vector2(centers[i][0]/PIXEL_TO_METER_RATIO_DEFAULT,centers[i][1]/PIXEL_TO_METER_RATIO_DEFAULT);
           Vector2 v2 = new Vector2(centers[j][0]/PIXEL_TO_METER_RATIO_DEFAULT,centers[j][1]/PIXEL_TO_METER_RATIO_DEFAULT);
           Log.d("Vals","vals x:" + centers[i][0] + "y:" + centers[i][1] + "x:" +centers[j][0] + "y:" + centers[j][1]);

           Log.d("i","i:" + i + " j:" + j);
           distanceJoint.initialize(circleBody[i], circleBody[(j)], v1, v2);

           distanceJoint.collideConnected = true;
           distanceJoint.dampingRatio = 10.0f;
           distanceJoint.frequencyHz = 0.5f;
           this.mPhysicsWorld.createJoint(distanceJoint);
       }
    }

我现在需要在身体之间有一条连线。否则,斑点会从落在它上面的身体反弹。

请让我知道如何在整个形状移动时在相邻物体之间绘制连接线以保持连接?