jMonkeyEngine相机跟随

时间:2012-02-20 17:05:26

标签: java graph 3d jmonkeyengine

我正在使用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(Box)
    • shipNode
      • shipBase
      • cameraNode

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

1 个答案:

答案 0 :(得分:4)

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

cameraNode.setControlDir(ControlDirection.CameraToSpatial);

但是,教程有:

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

在教程中降低它定义了这两个ControlDirections之间的差异。教程提供的相机跟随对象的移动,而你拥有的对象跟随相机的移动。

希望这有帮助。