LookAt fails when moving camera with object

I have a strange behaviour here.



I have written a shipLookAtTarget() methode which makes my Space Ship dynamics node look at the ray casted from the mouse coordinates. When i'm pressing the right maouse button the ship will fly towards the ray's location.



   private void shipLookAtTarget() {
      Ray mouseRay = null;
      Vector2f mousePos = new Vector2f();
      mousePos.x = MouseInput.get().getXAbsolute();
      mousePos.y = MouseInput.get().getYAbsolute();
      mouseRay = display.getPickRay(mousePos, false, mouseRay);
      target = mouseRay.getOrigin().mult(zoomValue);
      target.y = 0f;
      mShip.lookAt(target, Vector3f.UNIT_Y);
   }



Everything will work fine with this:


   protected void simpleUpdate() {
      super.simpleUpdate();

      shipLookAtTarget();

      if (!released) {
         Quaternion old = new Quaternion(mShip.getLocalRotation());
         mShip.getLocalRotation().slerp(old, 0.001f);
         Vector3f rotColmn = mShip.getLocalRotation().getRotationColumn(2);
         mShip.addForce(rotColmn.mult(0.01f));
      }
       }



But when i want the camera to follow the mShip object, the shipLookAtTarget seems not to work anymore (the ship's behaviour is wrong, it doesn't follow the mouse anymore):


   protected void simpleUpdate() {
      super.simpleUpdate();

      shipLookAtTarget();

      if (!released) {
         Quaternion old = new Quaternion(mShip.getLocalRotation());
         mShip.getLocalRotation().slerp(old, 0.001f);
         Vector3f rotColmn = mShip.getLocalRotation().getRotationColumn(2);
         mShip.addForce(rotColmn.mult(0.01f));
      }
      
       Vector3f tempVec = mShip.getLocalTranslation().clone();
       tempVec.z += 0.01;
       tempVec.y = zoomValue;
       this.cam.setLocation(tempVec);
       this.cam.lookAt(mShip.getLocalTranslation(), Vector3f.UNIT_Y);
   }



Have i forgotten to update something or is there a great logic bug hiding somewhere between the lines?

Any help would be really nice ;)
- Maurice

PS: Here's the complete code:


package de.blackswamp.game.main;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.input.InputHandler;
import com.jme.input.MouseInput;
import com.jme.input.MouseInputListener;
import com.jme.math.Quaternion;
import com.jme.math.Ray;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.shape.Box;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.util.SimplePhysicsGame;

public class Test8 extends SimplePhysicsGame {

   ColorRGBA[] red = new ColorRGBA[3];

   private static final Logger logger = Logger
         .getLogger(Test8.class.getName());

   private StaticPhysicsNode staticNode;

   private boolean released;

   private Vector3f target;

   private DynamicPhysicsNode mShip;

   private float zoomValue;

   /**
    * Simple Init Game
    */
   protected void simpleInitGame() {
      zoomValue = 3f;
      input = new InputHandler();

      logger.setLevel(Level.INFO);
      logger.addHandler(new ConsoleHandler());

      released = true;

      staticNode = getPhysicsSpace().createStaticNode();
      rootNode.attachChild(staticNode);
      staticNode.getSpace().setDirectionalGravity(new Vector3f(0, 0, 0));

      mShip = getPhysicsSpace().createDynamicNode();
      mShip.createSphere("test").setLocalScale(0.03f);
      rootNode.attachChild(mShip);
      mShip.setLocalTranslation(0f, 0f, 0f);

      Box boxX = new Box("Box2", new Vector3f(0, 0, 0), 20f, 0f, 0.02f);
      Box boxY = new Box("Box1", new Vector3f(0, 0, 0), 0.02f, 0f, 20f);
      rootNode.attachChild(boxX);
      rootNode.attachChild(boxY);

      // MOUSE INPUT SYSTEM
      MouseInput.get().setCursorVisible(true);
      MouseInput.get().addListener(new MouseInputListener() {
         public void onButton(int button, boolean pressed, int x, int y) {
            if (button == 1) {
               if (pressed) {
                  released = false;
               } else {
                  released = true;
               }
            }
         }

         public void onMove(int xDelta, int yDelta, int newX, int newY) {
         }

         public void onWheel(int wheelDelta, int x, int y) {
         }
      });

      this.cam.setLocation(new Vector3f(0f, zoomValue, 0.01f));
      this.cam.setUp(Vector3f.UNIT_Y);
      this.cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
      cam.update();
      showPhysics = true;
   }

   protected void simpleUpdate() {
      super.simpleUpdate();

      shipLookAtTarget();

      if (!released) {
         Quaternion old = new Quaternion(mShip.getLocalRotation());
         mShip.getLocalRotation().slerp(old, 0.001f);
         Vector3f rotColmn = mShip.getLocalRotation().getRotationColumn(2);
         mShip.addForce(rotColmn.mult(0.01f));
      }
      
       Vector3f tempVec = mShip.getLocalTranslation().clone();
       tempVec.z += 0.01;
       tempVec.y = zoomValue;
       this.cam.setLocation(tempVec);
       this.cam.lookAt(mShip.getLocalTranslation(), Vector3f.UNIT_Y);
   }

   private void shipLookAtTarget() {
      Ray mouseRay = null;
      Vector2f mousePos = new Vector2f();
      mousePos.x = MouseInput.get().getXAbsolute();
      mousePos.y = MouseInput.get().getYAbsolute();
      mouseRay = display.getPickRay(mousePos, false, mouseRay);
      target = mouseRay.getOrigin().mult(zoomValue);
      target.y = 0f;
      mShip.lookAt(target, Vector3f.UNIT_Y);
   }

   public static void main(String[] args) {
      Test8 test = new Test8();
      test.start();
   }
}