Converting from Vector3f to Quaternion


How do I get the Camera.getDirection() as a Quaternion?



… my first bet would be to use a CameraNode to drive my cam and use it’s getLocalRotation() to get the quaternion



Hmmz, yes well that’s kind of the problem… I’m stuck using the standard camera due to the camera system I’m using.

Ok, lets see

This is some code from abstract camera:

 public void setAxes(Quaternion axes) {
        left = axes.getRotationColumn(0, left);
        up = axes.getRotationColumn(1, up);
        direction = axes.getRotationColumn(2, direction);

as i understand it this builds the left, up and direction Vec3f's from a Quaternion.
I haven't a chance to test it right now but i'd give it a try and revert this to build a Quaternion from the respective dir, up and left vectors in Camera by using:

public void fromAxes (Vector3f[] axis) {
        Matrix3f tempMat = new Matrix3f();

        tempMat.m00 = axis[0].x;
        tempMat.m10 = axis[0].y;
        tempMat.m20 = axis[0].z;

        tempMat.m01 = axis[1].x;
        tempMat.m11 = axis[1].y;
        tempMat.m21 = axis[1].z;

        tempMat.m02 = axis[2].x;
        tempMat.m12 = axis[2].y;
        tempMat.m22 = axis[2].z;


from Quaternion

But i'm not in the least sure this will work.

Matrix3f newCamera=new Matrix3f();
        newCamera.setColumn(0, left);
        newCamera.setColumn(1, up);
        newCamera.setColumn(2, direction);

CameraNode cn;

This will make your camera look at direction "dir" with up vector "up" and left vector "left". Is this what you want? To get the "direction" a camera is facing, just get the world rotation and look at Column 2 for the direction. You have to make sure you use the correct up and left vectors. You can get those with cross products. For the exact formulas, search for forums for a post I made about something similar some time ago.

He’s looking to go the opposite way, he has direction and wants the Quat… Winkman was taking you the right direction. Basically you can pass the three axis, left, up and direction into the fromAxes method of a new Quat. Obviously that method needs a bit of documentation. heh.

Ok, thanks for the replies.

This is still a bit confusing to me. How do I get the camera axes?

I tried:

Vector3f[] axis = new Vector3f[] {
   new Vector3f(cameraDirection.x, 0, 0),
   new Vector3f(0, cameraDirection.y, 0),
   new Vector3f(0, 0, cameraDirection.z)


but with no luck.

direction, up and left are the three camera axes. Each axis is a vector.


I have no idea how I managed not to see the getters for those in the Camera doc.

Well, I think it’s working the way I want it now :slight_smile: Thanks all!


I know this is an old post, but I'm having the exact problem and I'm not sure how to fix it…could you post the code segment or explain to me how you solved this?  I'm trying to make the camera's rotation (turning left and right) update the rotation of my character.