android - 如何防止 Prismatic Joint 重置为初始交易限额?

标签 android box2d andengine

我正在开发一款让玩家围绕行星运行的游戏。我正在使用 PrismaticJoint 让玩家靠近和远离行星,我通过旋转玩家 (bodyB) 所连接的中心点主体(关节的 bodyA)来围绕玩家运行。我通过启用电机并禁用限制来在 Prismatic 轴上移动玩家。但是,一旦我更新并重新启用 PrismaticJoint 的限制, body 就会跳回其原始位置,并且 PrismaticJoint 限制会返回到其原始限制。

我已经尝试以一百万种不同的方式调整它,无论我如何尽快重新启用限制(无论新限制应该是什么),它总是会捕捉到原来的极限和位置。我使用 SpriteBody,这是一个扩展 Sprite 并具有主体的类。除了正在应用的新限制之外的所有内容都按预期执行。 这是创建 Prismatic Joint 时调用的方法——(请注意 currentThrustJoint 是一个公共(public)字段,并且始终可以访问,因为它是 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);
 }

下面是我创建 Player 的方式,然后是 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);

我知道这很多,但我真的很感激第二眼看到这一点,我已经花了四天时间试图弄清楚我忽略了什么。非常感谢。

最佳答案

我在 AndEngine 论坛上问过这个问题,有人说每次要更改限制时都要尝试破坏并重新创建关节。保存最初用于创建关节的 JointDef,销毁关节,再次初始化它,然后重新创建关节。奇迹般有效!它似乎并不太耗费性能,因为我很少重置限制,但如果您希望创建一个 PrismaticJoint,它可以通过更改限制来快速滑动 body ,您可能需要尝试另一个关节。 (我会说 DistanceJoint 之类的,但我还没有测试过)

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

关于android - 如何防止 Prismatic Joint 重置为初始交易限额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14298306/

相关文章:

java - 具有多行摘要的自定义 Android 通知 BigPicture 不适用于 API 25 和 API 26

android - SearchView Edittext 不显示光标

android - 为 Facebook 登录禁用 native 应用程序(只需使用弹出窗口)

java - AndEngine Sprite 阴影

java - libGDX - 尝试实现 2 个 Sprite 之间的碰撞

android - 何时在 Libgdx 中使用 ShapeRenderer、Mesh + SpriteBatch、Box2D 和 Scene2D?

ios - 像小翅膀一样的硬币收藏品

Javascript物理引擎和模拟无限曲线

android - andengine 中的微不足道的纹理加载

安卓(和引擎): Drawing with touch using particles