2

我正在用 jMonkeyEngine 测试一些东西,我试图让相机跟随一个盒子空间。我在这里遵循了官方说明:

http://jmonkeyengine.org/wiki/doku.php/jme3:advanced:making_the_camera_follow_a_character

申请时,我在那里学到的东西产生了以下代码:

@Override
public void simpleInitApp() {
    flyCam.setEnabled(false);

    //world objects
    Box b = new Box(Vector3f.ZERO, 1, 1, 1);
    Geometry geom = new Geometry("Box", b);

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    geom.setMaterial(mat);

    rootNode.attachChild(geom);

    //Ship node
    shipNode = new Node();
    rootNode.attachChild(shipNode);

    //Ship
    Box shipBase = new Box(new Vector3f(0, -1f, 10f), 5, 0.2f, 5);
    Geometry shipGeom = new Geometry("Ship Base", shipBase);

    Material shipMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    shipMat.setColor("Color", ColorRGBA.Green);
    shipGeom.setMaterial(shipMat);

    shipNode.attachChild(shipGeom);

    //Camera node
    cameraNode = new CameraNode("Camera Node", cam);
    cameraNode.setControlDir(ControlDirection.CameraToSpatial);
    shipNode.attachChild(cameraNode);

    initPhysics();

    initKeys();


}

当调用以下代码时:

@Override
public void simpleUpdate(float tpf) {
    //Update ship heading
    shipHeading = shipHeading.mult(shipRotationMoment);
    shipNode.setLocalRotation(shipHeading);

    shipPos = shipPos.add(shipVelocity);
    shipNode.setLocalTranslation(shipPos);
}

盒子按预期移动,但相机保持在原位。图表应该是这样的:

  • 根节点
    • b(框)
    • 船节点
      • 船舶基地
      • 相机节点

因此相机应该已经绑定到shipNode。我错过了什么?

4

1 回答 1

4

阅读您提供的教程,您似乎可能有错字。你有:

cameraNode.setControlDir(ControlDirection.CameraToSpatial);

但是,本教程有:

//This mode means that camera copies the movements of the target:
camNode.setControlDir(ControlDirection.SpatialToCamera);

在教程的下方,它定义了这两个 ControlDirections 之间的区别。教程提供的方法是让相机跟随物体的运动,而你拥有的物体跟随相机的运动。

希望这可以帮助。

于 2012-02-20T17:26:17.203 回答