2

I'm testing something with jMonkeyEngine and I'm attempting to have the camera follow a box spatial. I followed official instructions here:

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

When applying, what I learnt there I produced the following code:

@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();


}

When the following code is called:

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

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

The box moves as is predicted but the camera stays where it is. The graph should be something like this:

  • rootNode
    • b (Box)
    • shipNode
      • shipBase
      • cameraNode

Therefore the camera should be already bound to shipNode. What am I missing?

Tõnis Ojandu
  • 3,486
  • 4
  • 20
  • 28
  • Why should the camera move if it's attached to the ship but the box is the object that's moving? – Dan W Feb 20 '12 at 17:11
  • If I'm seeing something wrong correct be, but the shipNode moves therefore it's child, shipBase (the box) moves along with it. Yet the cameraNode which is also child of the shipNode remains still. – Tõnis Ojandu Feb 20 '12 at 17:19
  • I'm sorry, I missed the fact that the ship was also moving. – Dan W Feb 20 '12 at 17:21

1 Answers1

4

Reading through the tutorial you provided, it seems you might have a typo. You have:

cameraNode.setControlDir(ControlDirection.CameraToSpatial);

However, the tutorial has:

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

Lower down in the tutorial it defines the difference between these 2 ControlDirections. The one the tutorial provides has the camera follow the movement of the object, whereas what you have the object follows the movement of the camera.

Hope this helps.

Dan W
  • 5,718
  • 4
  • 33
  • 44