我正在用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

shipBase
cameraNode




因此,摄像头应该已经绑定到shipNode。我想念什么?

最佳答案

通读您提供的教程,看来您可能有错字。你有:

cameraNode.setControlDir(ControlDirection.CameraToSpatial);


但是,该教程具有:

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


在本教程的下部,它定义了这两个ControlDirection之间的区别。本教程提供的一种是使照相机跟随对象的运动,而您拥有的对象随照相机的运动。

希望这可以帮助。

10-05 23:56