A little help with the camera

I'm trying to get myself started with JME. I'm new-ish to Java, but good with OOP. I'm finding it mostly fairly easy to get around in the code, though I think I've embarked on a project slightly more difficult than a first glance can attempt.



Basically I have a few maps in obj format, loading into an ocean with a skymap, courtesy of the test projects.

I also have a plane that I've loaded, and positioned via .setLocalTranslation.



I've attempted to attach a camera to this jet, fixed to a certain position relative to the jet, but failed.



I have written code that sets velocity, direction, and calculates the angles of rotation for the jet, all working properly.



What I haven't managed to do is mount a camera to the jet. Can anyone help me? To clarify, I need to retain my current working model of rotation for the jet, and I want the camera to be slightly behind and above the jet…



Can someone post some example code, with 'jet' as the node to which the camera would be welded?


Quaternion qRot = new Quaternion();
       qRot = qRot.fromAngleAxis(jetHeading, localRotation.getRotationColumn(1));
       jet.setLocalRotation(qRot);
       jet.setLocalTranslation(new Vector3f(setJetWorldX, setJetWorldY, setJetWorldZ));    
       qRot = qRot.fromAngleAxis((float)Math.toRadians(jetHeading), localRotation.getRotationColumn(1));
        cam.setAxes(qRot);
        Vector3f jetLoc = jet.getLocalTranslation();
        cam.setLocation(new Vector3f(jetLoc.x, jetLoc.y+7.5f, jetLoc.z+20f));
        cam.update();



I've butchered the code a bit since the last time I tested it, but this is basically what I'm working with for the rotation. Any help would be greatly appreciated!!

well, you may use the CameraNode of jme.



i've hacked a little test for you, but it's based on jme1. I dunno if its working on jme2, but anyhow the approach should be the same.

What you have to take a closer look onto is the steering-behavior within the updateCycle.


import com.jme.app.SimpleGame;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.CameraNode;
import com.jme.scene.shape.Box;

public class TestCameraNode extends SimpleGame   {

   /**
    * @param args
    */
   public static void main(String[] args) {
      TestCameraNode app = new TestCameraNode();
      app.start();

   }

   private CameraNode camNode;
   private Vector3f camDir;

   @Override
   protected void simpleInitGame() {
      setupKeyBindings();
      setupFloor();
      
      cam.setFrustumFar(Float.MAX_VALUE);
      cam.setDirection(Vector3f.UNIT_Z);
      
      
      camNode = new CameraNode("camNode1",cam);
      camNode.setLocalTranslation(0, 5, 0);
      Box jet = new Box("jet",Vector3f.ZERO,0.2f, 0.2f ,1);
      camNode.attachChild(jet);
      jet.setLocalTranslation(new Vector3f(0,0,5));
      rootNode.attachChild(camNode);
      
      
   }

   private void setupFloor() {
      //just setup some boxes as a gridlike floor
      for(int i=0; i<20; i++){
         for(int j=0; j<20; j++){
            Box floor = new Box("floor",Vector3f.ZERO,390,0.1f,390);
            floor.setLocalTranslation(i*800, 0, j*800);
            rootNode.attachChild(floor);
         }
      }
   }

   private void setupKeyBindings() {
      //define keys & commands for steering
      KeyBindingManager.getKeyBindingManager().set("forward", KeyInput.KEY_W);
      KeyBindingManager.getKeyBindingManager().set("backward", KeyInput.KEY_S);
      KeyBindingManager.getKeyBindingManager().set("rollLeft", KeyInput.KEY_A);
      KeyBindingManager.getKeyBindingManager().set("rollRight", KeyInput.KEY_D);
      KeyBindingManager.getKeyBindingManager().set("noseUp", KeyInput.KEY_UP);
      KeyBindingManager.getKeyBindingManager().set("noseDown", KeyInput.KEY_DOWN);
   }

   @Override
   protected void simpleUpdate() {
      super.simpleUpdate();
      
      //update the jet's position and Rotation based on inputs
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("forward")){
         camDir = cam.getDirection().normalize().multLocal(tpf*200);
         camNode.getLocalTranslation().addLocal(camDir);
      }
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("backward")){
         camDir = cam.getDirection().normalize().multLocal(tpf*200);
         camNode.getLocalTranslation().subtractLocal(camDir);
      }
      //just hacked..no solution for a real jet-steering solution
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("rollLeft")){
         camNode.setLocalRotation(camNode.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(FastMath.PI/250, Vector3f.UNIT_Z)));
      }
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("rollRight")){
         camNode.setLocalRotation(camNode.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(-FastMath.PI/250, Vector3f.UNIT_Z)));
      }
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("noseUp")){
         camNode.setLocalRotation(camNode.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(-FastMath.PI/250, Vector3f.UNIT_X)));
      }
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("noseDown")){
         camNode.setLocalRotation(camNode.getLocalRotation().multLocal(new Quaternion().fromAngleAxis(FastMath.PI/250, Vector3f.UNIT_X)));
      }
   }

   
}



You may have a look at the ThirdPersonController as well.

Wow, thanks for the prompt support. I'll give your code some thought and try again. I really appreciate the extra pair of eyes, thanks.

no, definately not use jme1 … it's kinda dead  :expressionless:

I just havn't switched to jme2 beacause i'm using jme1 for work, so it would be a pretty load of work to upgrade the project to jme2.

So i haven't taken a look on jme2, yet.



try to break the code down, to only the necessary things, and see if you can figure out why?

I dunno if there is a known bug or anny issue with the cameraNode or it's usage in jme2?!

Maybe double check your bounding boxes…