0
\$\begingroup\$

I have a FPP quaternion Camera and Plane with known normal vector.

I want to find the orientation of this Plane so I can replace it to the Camera orientation to put the camera forward vector to a plane's normal vector.

  • First method I tried, I don't understand RotationAxis and RotationAngle. I built a rotation matrix and applied it to the camera but it didn't behave as I wanted.

  • The second method I tried is with method called "from_one_vector_to_another". I don't understand what the "from" vector is in my example; is it the camera's forward vector?

Here is my pseudo code:

Vector3 vector_from = camera->getForwardVector();
Vector3 vector_to = triangle->faceNormal;

Quat cameraQuaternion = camera->getOrientation();

Quat q = vector_from.getRotationTo(vector_to);//The method `getRotationTo` is from Ogre.
camera->setOrientation(cameraQuaternion * q);

Camera does not change their orientation as I expected; it changes its orientation just a little bit.

EDIT:

Maybe I have something wrong with my code:

//Quat xrot = axisToQuat(Vector3(1, 0, 0), vertical);  // disabled for now
Quat yrot = axisToQuat(Vector3(0, 1, 0), horizontal);
rot = rot * yrot;

position += velocity;

Matrix lookAt = Matrix::createLookAt
        (
            Vector3(position.x, position.y, position.z),
            Vector3(position.x, position.y, position.z-1),
            Vector3(0,1,0)
        );

view = rot.toMatrix() * lookAt;

I am only rotating the quaternion. Should I rotate lookAt Matrix too?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

So, just to be clear; you want to orient the camera such that it is looking in the direction given by the plane's normal?

The problem with calculating a quaternion for this is that it doesn't usually represent an absolute rotation but rather a rotation through which the initial forward direction of the camera is to be rotated. So what you are looking for is a quaternion that will rotate the initial forward vector of the camera to the normal of the plane. This link will give you that answer:

https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another

However it is common for a camera API to have a function that will take a position and a look at and orient the camera for you. In your case this will be much more simple:

position = whatever you want it to be

look at position = position + normal

Hope I am understanding the question correctly.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you for you answer. So, just to be clear; you want to orient the camera such that it is looking in the direction given by the plane's normal? - correct. Actually I am doing it right in this linked answer you posted. I have edited my question, would you like to look at it? As you posted I should change the position of my lookAT matrix, but what with the quaternion then? \$\endgroup\$
    – Tom
    Commented Feb 18, 2014 at 10:29
  • 1
    \$\begingroup\$ I am slightly confused by the code in your edit, I think that is partly because, regardless of whether the code actually does what it wants you are mixing euler angles, quaternions, and matricies. My first step would be to simplify this. However I would go back to what your camera API provides. Does it provide a function where you can pass a world space camera position and look at position? If so you can do everything with simple vector geometry as described previously (sorry that I wasn't clear that this was vector geom). \$\endgroup\$
    – Claude
    Commented Feb 20, 2014 at 3:08
  • 1
    \$\begingroup\$ If the API only accepts a world space position and a quaternion, then you should calculate the quaternion as described in the linked article. No matrices needed. \$\endgroup\$
    – Claude
    Commented Feb 20, 2014 at 3:09
  • \$\begingroup\$ For now my camera API accepts only a world space position and a quaternion, but I will take your advice and make a function that takes a world space position and a look at position to simplify the problem. Actually I'm changing my camera code to use only one quaternion for rotations. For now Thanks for your help. \$\endgroup\$
    – Tom
    Commented Feb 20, 2014 at 12:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .