Author Topic: Camera rotation question  (Read 6574 times)

Offline entis

  • byte
  • *
  • Posts: 32
    • View Profile
Camera rotation question
« on: April 16, 2008, 10:58:25 am »
Hi,

I have a question about camera rotation... I would like to rotate a camera each time absolutely (i.e. rotation angle should be layed off from the world axis each time I call the rotation method) .. currently I have a problem with it. For example if I call camera.rotateY(Math.toRadians(10)) two times it will do camera rotation each time and the camera angle relative to the world Y axis will be 20 deg. not 10.

Here is what I want:
...
camera.rotateY(Math.toRadians(10)) // camera has been rotated on 10 deg. (relative to the world Y axis)
camera.rotateY(Math.toRadians(10)) // actually camera has been rotated on 10 deg. (relative to the world Y axis) but nothing changed on the screen since angle is 10 again
camera.rotateY(Math.toRadians(12)) // actually camera has been rotated on 12 deg. (relative to the world Y axis) but on the screen it looks like rotation on 2 deg. (relative to the old position)

So your sugestions are welcome about how can I realize this in jpct... may be it's already done in jpct?
Thanks in advance.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Camera rotation question
« Reply #1 on: April 16, 2008, 12:14:34 pm »
Just do something like:

Code: [Select]
camera.getBack().setIdentity();
Before doing the rotations. That should do the trick.

Offline entis

  • byte
  • *
  • Posts: 32
    • View Profile
Re: Camera rotation question
« Reply #2 on: April 16, 2008, 12:22:27 pm »
Just do something like:

Code: [Select]
camera.getBack().setIdentity();
Before doing the rotations. That should do the trick.

Thanks a lot! it really works...

Offline entis

  • byte
  • *
  • Posts: 32
    • View Profile
Re: Camera rotation question
« Reply #3 on: April 28, 2008, 04:32:55 pm »
Hi,

I have another one question about camera rotation/movement...

In my app I move the camera just like in the JPCT FPS example is shown:

Code: [Select]
   ...
   public void moveIn()
   {
   tempVector = cameraDirection.getZAxis();
   scene.checkCameraCollisionEllipsoid(tempVector, CAMERA_ELLIPSOID_RADIUS, CAMERA_MOVE_SPEED, 5);
   }

   public void moveOut()
   {
   tempVector = cameraDirection.getZAxis();
   tempVector.scalarMul(-1f);
   scene.checkCameraCollisionEllipsoid(tempVector, CAMERA_ELLIPSOID_RADIUS, CAMERA_MOVE_SPEED, 5);
   }

   public void moveUp()
   {
   tempVector = cameraDirection.getYAxis();
   tempVector.scalarMul(-1f);
   scene.checkCameraCollisionEllipsoid(tempVector, CAMERA_ELLIPSOID_RADIUS, CAMERA_MOVE_SPEED, 5);
   }
   
   public void moveDown()
   {
   tempVector = cameraDirection.getYAxis();
   scene.checkCameraCollisionEllipsoid(tempVector, CAMERA_ELLIPSOID_RADIUS, CAMERA_MOVE_SPEED, 5);
   }
   ...

and it works ok... but know I need to call once camera.lookAt() method to point my camera to some object... but after this my camera begin to move not good with the code listed above...
I understand that it's due to the fact that my cameraDirection matrix is not changed with lookAt() method, but I just can't get how to apply modifications made by lookAt() method to the cameraDirection matrix... Please help...
« Last Edit: April 28, 2008, 04:42:55 pm by entis »

Offline entis

  • byte
  • *
  • Posts: 32
    • View Profile
Re: Camera rotation question
« Reply #4 on: April 28, 2008, 05:28:32 pm »
I found some solution here http://www.jpct.net/forum2/index.php/topic,317.0.html but anyway my movement is not parallel to the floor after lookAt()...

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Camera rotation question
« Reply #5 on: April 28, 2008, 11:34:49 pm »
I am not an expert on this subject, but I believe the reason for movement not being parallel to the floor after lookAt(), is because the camera's Z-axis is changed by that method.  Instead of using the camera's getZAxis() for the moveIn() and moveOut() methods, you will probably need to calculate a vector that is in the general direction of the object, but parallel to the ground (i.e. if the ground is flat and constant altitude, then destination Y should equal camera Y)).  Use that vector in your call to checkCameraCollisionEllipsoid() instead of getZAxis().

Also, if you want the moveUp() and moveDown() methods to move perpendicular to the ground when you "lookAt()" an object with a different Y position than the camera, then instead of using the camera's getYAxis(), you will probably need to calculate a vector that is perpendicular to the ground (i.e. if the ground is flat and constant altitude, then use (0, 1, 0)).

In cases where the ground is not flat and constant altitude, you may be able to assume it is and just let the collision detection take care of the altitude adjustments.  I don't have any experience with collision detection in jPCT yet, though, so you'll have to experiment, and I am sure some other people can help you, who have more experience than I do on the topic.

One more thing, if you are keeping track of a "playerDirection" matrix separate from the camera's direction matrix (as mentioned in the thread you brought up), then what EgonOlsen wrote is what you will need help with:

... I assume that movement should stay parallel to the floor even when looking slightly up or down to the object in question. Then you have to maintain the player's rotation matrix yourself, i'm afraid ...

Unfortunately, I do not have any experience with that topic, either.

Anyway, I hope this helps you out a little  ;D
« Last Edit: April 29, 2008, 12:05:22 am by paulscode »

Offline entis

  • byte
  • *
  • Posts: 32
    • View Profile
Re: Camera rotation question
« Reply #6 on: April 29, 2008, 07:59:43 am »
I am not an expert on this subject, but I believe the reason for movement not being parallel to the floor after lookAt(), is because the camera's Z-axis is changed by that method.  Instead of using the camera's getZAxis() for the moveIn() and moveOut() methods, you will probably need to calculate a vector that is in the general direction of the object, but parallel to the ground (i.e. if the ground is flat and constant altitude, then destination Y should equal camera Y)).  Use that vector in your call to checkCameraCollisionEllipsoid() instead of getZAxis().

Also, if you want the moveUp() and moveDown() methods to move perpendicular to the ground when you "lookAt()" an object with a different Y position than the camera, then instead of using the camera's getYAxis(), you will probably need to calculate a vector that is perpendicular to the ground (i.e. if the ground is flat and constant altitude, then use (0, 1, 0)).

In cases where the ground is not flat and constant altitude, you may be able to assume it is and just let the collision detection take care of the altitude adjustments.  I don't have any experience with collision detection in jPCT yet, though, so you'll have to experiment, and I am sure some other people can help you, who have more experience than I do on the topic.

One more thing, if you are keeping track of a "playerDirection" matrix separate from the camera's direction matrix (as mentioned in the thread you brought up), then what EgonOlsen wrote is what you will need help with:

... I assume that movement should stay parallel to the floor even when looking slightly up or down to the object in question. Then you have to maintain the player's rotation matrix yourself, i'm afraid ...

Unfortunately, I do not have any experience with that topic, either.

Anyway, I hope this helps you out a little  ;D

Thanks Paulscode... the following action after lookAt() method solves all the problems with parallel movement: cameraDirection.set(2,1,0.0f);