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

• byte
• Posts: 4
##### 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.

#### EgonOlsen

• Posts: 11884
##### Re: Order for applying rotations to camera correctly
« Reply #1 on: March 30, 2014, 09:16:57 pm »
`camera.lookAt( new SimpleVector(0.0f, 0.0f, 1.0f) );camera.getBack().setIdentity();`
`rotate?Axis(cam.get?Axis(), ...); `