Author Topic: How to Rotate Object relative to camera?  (Read 3697 times)

Offline stormp

  • byte
  • *
  • Posts: 38
    • View Profile
How to Rotate Object relative to camera?
« on: November 04, 2007, 10:25:43 pm »
Hi again,

I'm trying to figure out how to rotate an object relative to the X Y axis of the screen space.

This *seemed* at first to almost work, but when I move the camera behind or under the object, I get inverse and / or reverse rotations - so I guess I'm way off here. :-(

Any suggestions?

Thank you,

S.

Code: [Select]
public SimpleVector rotateRespect2Cam(float rotX, float rotY)
{
   SimpleVector dir = theCamera.getDirection();

   dir.rotateX(rotX);
   dir.rotateY(rotY);

   return dir.calcSub(theCamera.getDirection());
}

Code: [Select]
SimpleVector rotate = rotateRespect2Cam(delta.x * -camRotSpeed, delta.y * -camRotSpeed);

object.rotateX(rotate.x);  // rx rz ry to avoid gimbal lock.
object.rotateZ(rotate.z);
object.rotateY(rotate.y);
« Last Edit: November 05, 2007, 12:23:29 pm by stormp »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to Rotate Object relative to camera?
« Reply #1 on: November 05, 2007, 05:27:40 pm »
You can get the axes from the camera and rotate the object around those using rotateAxis(...).

Offline stormp

  • byte
  • *
  • Posts: 38
    • View Profile
Re: How to Rotate Object relative to camera?
« Reply #2 on: November 05, 2007, 07:57:48 pm »
Thanks Egon!  I really misunderstood what the rotateAxis() function was for.  Now suddenly everything is coming to light ;-)  Thanks again!!!!

I made a demo base that i am using to build on for my quest to solve the my bigger problem of natural rotations (hopefully soon).  I know these are elementary examples, but they might help someone else starting out.   


Applet demo with source:

Simple free camera movement and rotation around a simple scene using keyboard and mouse.
http://testcod07.fortunecity.com/rotate/EulerRotateCamTest/

Simple free camera movement and rotation with keyboard and object rotation and movement with mouse relative to camera view.
http://testcod07.fortunecity.com/rotate/EulerRotateObjTest/

I'll try to clean up the code and optimize with comments later :-)

Thanks so much again!

S.