2012-02-20 57 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之間的差異。教程提供的一個功能是讓相機跟隨物體的運動,而您擁有的物體跟隨相機的運動。

希望這會有所幫助。