2012-02-20 54 views
2

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

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

+0

为什么相机应该移动,如果它连接到船只,但箱子是移动的对象? – 2012-02-20 17:11:59

+0

如果我看到错误是正确的,但是shipNode移动因此它是child,shipBase(框)随之移动。然而,同时也是该船的孩子的cameraNode仍然是静止的。 – Vampnik 2012-02-20 17:19:21

+0

对不起,我错过了船也在移动的事实。 – 2012-02-20 17:21:28

回答

4

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

cameraNode.setControlDir(ControlDirection.CameraToSpatial); 

然而,本教程有:

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

教程低了下去它定义了这2个ControlDirections之间的差异。教程提供的一个功能是让相机跟随物体的运动,而您拥有的物体跟随相机的运动。

希望这会有所帮助。