如何防止PrismaticJoint重置为其初始翻译限制?

时间:2013-01-12 21:52:56

标签: android box2d andengine

我正在开发一款让玩家围绕行星旋转的游戏。我正在使用PrismaticJoint让玩家越来越远离行星,我正在通过旋转玩家(bodyB)附着的中心点身体(关节的身体A)来绕行。我通过启用电机并禁用限制来移动播放器在Prismatic轴上。但是,只要我更新并重新启用PrismaticJoint的限制,身体就会跳回到原来的位置,并且PrismaticJoint限制将恢复到原始限制。

我已经尝试过百万种不同的方式调整它,无论我做什么很快,因为我重新启用了限制(无论新的限制应该是什么)它总是快照到原来的限制和位置。我使用SpriteBody,它是一个扩展Sprite并具有正文的类。除了应用的新限制之外的所有内容都按预期执行。 这是调用Prismatic Joint创建的方法 - (请注意currentThrustJoint是一个公共字段,总是可以访问,就像它的FixtureDef一样)

// sprite is the SpriteBody that the Player orbits
private void setOrbit(PlanetaryCenter sprite){
    // Get Player (which is a SpriteBody)
    Player player = (Player) resources.get("player");
    // Updated Sprite Body Physics / Rotation
    thrustJointDef = new PrismaticJointDef();

    // Set up the thrustJointDef Correctly

    thrustJointDef.collideConnected = false;
    thrustJointDef.enableMotor = false;
    thrustJointDef.maxMotorForce = 20f;

    thrustJointDef.lowerTranslation = sprite.getBody().getWorldCenter().dst(player.getBody().getWorldCenter());
    thrustJointDef.upperTranslation = sprite.getBody().getWorldCenter().dst(player.getBody().getWorldCenter());
    thrustJointDef.motorSpeed = 5;
    thrustJointDef.initialize(sprite.getBody(), player.getBody(), 
            sprite.getBody().getWorldCenter(), new Vector2(0, 1f));


    // Update focus to the new orbit point
    sprite.isFocus = true;

    // Change currentOrbit to the new orbit point's user data
    currentOrbit = (String) sprite.getUserData();

    currentThrustJoint = (PrismaticJoint) this.mPhysicsWorld.createJoint(thrustJointDef);
    currentThrustJoint.enableLimit(true);
 }

这是我如何创建播放器,然后是OrbitPoint。两者都是在调用setOrbit之前完成的。

// Create Player Body/Physics
Player player = new Player(400-25, 400 + 400, player_default, getVertexBufferObjectManager());
player.setRotationCenter(player.getWidth()/2, player.getHeight()/2);
player.setBody(PhysicsFactory.createBoxBody(mPhysicsWorld, player, BodyType.DynamicBody, playerFixtureDef));
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, player.getBody(), true, true));
resources.put("player", player);

....
    // create the orbitpoint for the center
    PlanetaryCenter orbitCenter = new PlanetaryCenter(center_x_old, center_y_old, planet_center, getVertexBufferObjectManager());
    orbitCenter.setRotationCenter(50, 50);
    orbitCenter.setBody(PhysicsFactory.createBoxBody(mPhysicsWorld, orbitCenter, BodyType.KinematicBody, wallFixtureDef));
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(orbitCenter, orbitCenter.getBody(), false, true));
    orbitCenter.getBody().setUserData("orbit" + z);

    mScene.registerTouchArea(orbitCenter);
    // store in hashMap
    resources.put(("orbit" + z), orbitCenter);

    // attach the initial center point in the construct to the scene
    mScene.attachChild(orbitCenter);

最后,在最重要的位置 - 我将限制关闭,然后再将其重新打开:

private void setThrust(){ // called when the user taps a thrust key
    Player thisPlayer = (Player) resources.get("player");
    PlanetaryCenter thisOrbit = (PlanetaryCenter) resources.get(currentOrbit);
    thisPlayer.throttling = true;

    currentThrustJoint.enableLimit(false);

    if(ascButton.clicked) currentThrustJoint.setMotorSpeed(5f);
    else currentThrustJoint.setMotorSpeed(-5f);

    currentThrustJoint.enableMotor(true);

}
.... called in TouchListener on Scene...
Player thisPlayer = (Player) resources.get("player");
PlanetaryCenter thisOrbit = (PlanetaryCenter) resources.get(currentOrbit);

thisPlayer.throttling = false;
currentThrustJoint.setMotorSpeed(0);
currentThrustJoint.enableMotor(false);
float newLimit = currentThrustJoint.getJointTranslation(); // intended amt, but it doesn't matter what number it is- never changes
currentThrustJoint.setLimits(newLimit, newLimit);
currentThrustJoint.enableLimit(true);
currentThrustJoint.enableMotor(false);

我知道这很多但是我真的很感激第二眼看到这一点,我已经沉溺了四天试图找出我忽略的东西。非常感谢你。

1 个答案:

答案 0 :(得分:1)

我在AndEngine论坛上问过这个问题,有人说每次想要更改限制时都会尝试销毁并重新创建关节。保存最初创建Joint的JointDef,销毁关节,再次初始化它,然后重新创建关节。奇迹般有效!它似乎不太昂贵,因为我不经常重置限制,但是如果你想创建一个可以通过改变限制来快速滑动身体的PrismaticJoint,你可能不得不尝试另一个关节。 (我会说一个DistanceJoint或者其他东西,但我还没有测试过)

仍然没有关于为什么会发生这种情况的线索,但我会说PhysicsEngine在其计算中不承认一些更新的PrismaticJoint值。认为这是一个错误。