Camera Positioning

Hi… I'm having a hard time setting up my camera…



import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;


public class cameraRotationTrial extends SimpleGame {
   private CameraNode box1;
   private Node pivotNode;
   
   public static void main(String args[]){
      cameraRotationTrial app = new cameraRotationTrial();
      app.setConfigShowMode(ConfigShowMode.AlwaysShow);
      app.start();
   }
   protected void simpleInitGame(){
      pivotNode = new Node();
      
            
      Box box3 = new Box("box3",new Vector3f(0,0,0),1,1,1);
      box3.setModelBound(new BoundingBox());
      box3.setRandomColors();
      box3.setLightCombineMode( Spatial.LightCombineMode.Off );
      box3.updateModelBound();
      box3.updateRenderState();
      rootNode.attachChild(box3);
      
      box1 = new CameraNode("CamNode",cam);
      box1.setLocalTranslation(new Vector3f(0,0,-10));
      box1.setModelBound(new BoundingBox());
      box1.updateModelBound();
      pivotNode.attachChild(box1);
      pivotNode.updateRenderState();
      Box box2 = new Box("box2", new Vector3f(0,0,10),1,1,1);
      box2.setModelBound(new BoundingBox());
      box2.updateModelBound();
      pivotNode.attachChild(box2);
      pivotNode.updateRenderState();
      pivotNode.setLocalRotation(new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,1,0)));
      rootNode.attachChild(pivotNode);
      rootNode.updateRenderState();
      }
}



Here's my code... what this actually does is create a Camera node positioned Vector3f(0,0,10) and an imaginary box positioned opposite to it Vector3f(0,0,-10)... and then I put them into 1 node named pivotNode to ensure that my axis where the camera would orbit is the middle of the screen or Vector3f(0,0,0).... and then at this position I have put a Box which would be the subject of my camera...

the last of the code

pivotNode.setLocalRotation(new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,1,0)));


does a rotation of the pivotNode, where the camera is included, with both 45 degrees pointing the camera to the NorthWest vertex of the box in the middle... my problem is what if I would want to rotate the pivotNode 45 degrees with regards to Vector3f.UNIT_X but 90 degrees with regards to Vector3f.UNIT_Y which would make the camera point to the Northwest side of the box in the middle

please help

please, tell me if im wrong… but i think this should do the job:





change



pivotNode.setLocalRotation(new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,1,0)));



to


Quaternion abc = new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,0,0));
abc.addLocal(new Quaternion().fromAngleAxis(90 * FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
pivotNode.setLocalRotation(abc);

well, i do not know if this is part of a larger idea, which would cause my solution not work, however, here is what I think.





If you want the camera to look at a 45 degree angle, make a node and move it to a 45 degree angle to the camera, and say



cam.LookAt(node.getLocalTranslation(), new Vector3f(0,1,0));



however, it looks like you are trying to do something more intricate. this is just a simple way to deal with quaternions.

Fellkneul said:


Quaternion abc = new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,0,0));
abc.addLocal(new Quaternion().fromAngleAxis(90 * FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
pivotNode.setLocalRotation(abc);




actually, i think if you want to chain two rotations you have to multiply the quaternions, not add  ;)


Quaternion abc = new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,0,0));
abc.multLocal(new Quaternion().fromAngleAxis(90 * FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
pivotNode.setLocalRotation(abc);

dhdd said:

Fellkneul said:


Quaternion abc = new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,0,0));
abc.addLocal(new Quaternion().fromAngleAxis(90 * FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
pivotNode.setLocalRotation(abc);




actually, i think if you want to chain two rotations you have to multiply the quaternions, not add  ;)


Quaternion abc = new Quaternion().fromAngleAxis(45 * FastMath.DEG_TO_RAD, new Vector3f(1,0,0));
abc.multLocal(new Quaternion().fromAngleAxis(90 * FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
pivotNode.setLocalRotation(abc);





Seems quite logical, same as matrix multiplication you should indeed as DHDD suggest multiply the Quaternion.

Wiz

i see… well, you're right, i'm sorry.



thank you :slight_smile: