Author Topic: Order for applying rotations to camera correctly  (Read 1684 times)

Offline paulados

  • byte
  • *
  • Posts: 4
    • View Profile
Order for applying rotations to camera correctly
« on: March 30, 2014, 03:20:30 am »
Hi !!!

I've been trying and trying for a week, I have read in the forum some similar questions, and I'm not sure how to apply rotations to the camera for getting a normal behavior in the scene. The movement that I want to make is to simulate that I have a mobile phone, and rotate it to the left (y axis) and then rotate up and down (x axis).

First of all, I try this code:

Camera camera = world.getCamera();
camera.setPosition( new SimpleVector(0.0f, 0.0f, -20.0f) );  // Set camera position
camera.lookAt( new SimpleVector(0.0f, 0.0f, 1.0f) );  // Set direction to see. In positive Z axis
camera.getBack().setIdentity();  // clears rotation matrix
camera.rotateCameraX(-PI / 12.0f);  // rotates in X axis
camera.rotateCameraY(PI / 6.0f);  // Rotates in Y axis

And I get an strange rotation in the scene when moving the camera up and down (varying the angle in rotateCameraY). Then, I realize that when I was rotating the camera in Y axis, it was Y axis relative to the camera after X rotation. Thus, I try this:

Camera camera = world.getCamera();
camera.setPosition( new SimpleVector(0.0f, -2.0f, -20.0f) );
camera.lookAt( new SimpleVector(0.0f, 0.0f, 1.0f) );
camera.getBack().setIdentity();
SimpleVector yaxis = camera.getYAxis();
camera.rotateCameraX(-PI / 12.0f);
camera.rotateCameraAxis( yaxis, PI / 6.0f);

In this way, I though that Y axis should be the absolute Y axis of the camera before rotating in X axis, but behavior was the same that before.

Finally, I try this:

Camera camera = world.getCamera();
camera.setPosition( new SimpleVector(0.0f, -2.0f, -20.0f) );
camera.lookAt( new SimpleVector(0.0f, 0.0f, 1.0f) );
camera.getBack().setIdentity();
camera.rotateCameraY(PI / 6.0f);
camera.rotateCameraX(-PI / 12.0f);

This order for rotations seems to be ok, but, I'm not very sure why this order works, and no my last solution, and which is really the normal rotation order if I just want to move the camera in JPCT as if I where moving a camera in real world (rotating Right/Left, Up/down). I know this is a really basic question, but I've been stucked with it for this week and I'm getting crazy!!!!  :-[

Thanks in advance. If I have not explain correctly my doubt, please tell me and I'll explain again and I'll put some images showing the weird scene rotations.






Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Order for applying rotations to camera correctly
« Reply #1 on: March 30, 2014, 09:16:57 pm »
This part of your code

Code: [Select]
camera.lookAt( new SimpleVector(0.0f, 0.0f, 1.0f) );
camera.getBack().setIdentity();

makes no sense, because the call to setIdentity() will revert the lookAt-results. Anyway, i'm not 100% sure if i got the problem here. The actual issue with rotations around the xyz-axes in object space, is that a rotation doesn't change the reference system of the following ones, so different orders will produce different outcomes.
If you want to rotate the camera like it would rotate in the real world (i.e. relative to it's own coordinate system), always do something like

Code: [Select]
rotate?Axis(cam.get?Axis(), ...);
I'm not sure what your code is supposed to do, because it always clears the rotation and then applies some rotations using static values. I don't see how this relates to any phone movement... ???