How to Rotate Object relative to camera?

Started by stormp, November 04, 2007, 10:25:43 PM

Previous topic - Next topic

stormp

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.


public SimpleVector rotateRespect2Cam(float rotX, float rotY)
{
   SimpleVector dir = theCamera.getDirection();

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

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



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);

EgonOlsen

You can get the axes from the camera and rotate the object around those using rotateAxis(...).

stormp

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.