Quaternion Camera rotation

Started by
4 comments, last by Hornsj3 11 years, 9 months ago
Greetings,

My whole day ran off while I tried to implement a quaternion based camera system with the following requirements: With LeftmouseDown allow the camera to rotate around it's focus on a sphere trackball. I'm okay with the mathematics part, I have problems with the implementation. Currently I've achieved the following:

I have the necessary handling functions:
[source lang="cpp"]XMFLOAT2 Previous_MousePosition;

void OnMouseDown(WPARAM KeyState, int x, int y)
{
if (KeyState & VM_LBUTTON)
Previous_MousePosition = XMFLOAT2(x, y);
}

void OnMouseMove(WPARAM KeyState, int x, int y)
{
if(KeyState & VM_LBUTTON)
{
XMFLOAT2 NewPosition(x, y);

XMFLOAT2 SphereLocation2D(x / (m_Width >> 1), y / (m_Height >> 1));
SphereLocation2D.x -= 1;
SphereLocation2D.y = 1 - SphereLocation2D.y;

double z = 1 - x * x - y * y;
XMFLOAT3 SphereLocation3D(SphereLocation2D.x, SphereLocation2D.y, z > 0 ? z : 0);
// no idea where to go from here...
}
}[/source]
And I have my camera currently defined like:
[source lang="java"]class Camera
{
XMFLOAT3 m_Position;
// If I'm not mistaken no need for rotation since my camera will always look at the same point.

XMFLOAT4X4 m_ViewMatrix;
XMFLOAT4X4 m_ProjectionMatrix;
public:
Camera(void) : m_Position(XMVectorZero())
{
m_ViewMatrix = XMMatrixIdentity();
m_ProjectionMatrix = XMMatrixIdentity();
}
~Camera(void) { }

void Update(XMFLOAT3 NewPosition, float Dt = 1.0f)
{
// Calc Axis between m_Position & NewPosition
// FLOAT Angle(m_Position, NewPosition);
// No idea how to continue from here
// m_Position = NewPosition;
}

void BuildViewMatrix(void)
{
XMVECTOR Eye = XMVectorSet(m_Position.x, m_Position.y, m_Position.z, 0.0f);
XMVECTOR At = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

XMMATRIX ViewMatrix = XMMatrixLookAtLH(Eye, At, Up);
XMStoreFloat4x4(&m_ViewMatrix, ViewMatrix);
}
}
[/source]

So I really think I already messed up everything in here.
Advertisement
So, as you suspected, you really messed up everything there smile.png

If you are going to make a quaternion based camera, use some quaternions. hehe

XNAMath (or, if using win8 DirectXMath) has some helper functions for you.

See below for an example from my own quaternion based camera.

I don't profess to be a quaternion expert but this one has been tested and does work.


void D3D11CameraFlight::setRoll( float angle )
{

XMVECTOR rotationQuaternion = XMQuaternionRotationAxis(m_camTarget, angle);

m_tempQuaternion = XMQuaternionMultiply(m_camUp, rotationQuaternion);
rotationQuaternion = XMQuaternionConjugate(rotationQuaternion);
m_camUp = XMQuaternionMultiply(rotationQuaternion, m_camUp);
m_camRight = XMVector3Cross(m_camUp, m_camTarget);
}


Edit- This code is obviously not polished, m_tempQuaternion doesn't really need to be there. It's calculating a rotation quaternion, multiplying the up vector by it, then multiplying that result by the conjugate of the rotation quaternion. That will give you the new up vector post roll. Then take the cross product of that and the target vector to get the right vector and you have a full 3D orthogonal basis.

You can then use the target and up vectors, along with your position, to calculate your view matrix. XMMatrixLookToLH( m_camPosition, m_camTarget, m_camUp);

Same deal for pitch and yaw, really.
Okay, so I've cleared everything and a fresh start. So the main idea if I get right would be this: (In a very simple case)
We have a position for the camera (Eye)
We have a focus point where the camera looks (LookAt)
And finally we have the World's Up vector (Up XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f))

From this we create the View Matrix which is a 4x4 matrix:
XMStoreFloat4x4(&m_ViewMatrix, XMMatrixLookAtLH(m_Position, m_Target, XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f));

Now to have a quaternion based camera we need the actual direction (orientation) of the camera.
m_Direction = XMQuaternionNormalize(XMQuaternionRotationMatrix(XMLoadFloat4x4(&m_ViewMatrix)));

Aaand here I get lost. As I've watched over tutorials I found that to rebuild the viewMatrix they to a MatrixRotationQuaternion like:
XMStoreFloat4x4(&m_ViewMatrix, XMMatrixRotationQuaternion(m_Direction));

Zero movement or rotation for the camera yet, but if I do this nothing is drawn on my screen. On a second note to actually move or rotate the camera I thought something like this:
void UpdatePosition(const XMVECTOR Direction, float dt = 1.0f); -> Update the position (Eye) of the camera based on deltaTime.
void Move(float dx, float dy, float dz);
Calculate the direction XMVectorSet(dx, dy, dz, 0.0f) - m_Position. Call UpdatePosition. (I may be wrong tho)
void Rotate(float Pitch, float Yaw, float Roll); -y No idea yet but I'll eventually find the answer :D
A quaternion represents a rotation of some scalar amount around a specified axis. (e.g. {30.0, 0.0, 0.0, 1.0} could represent 30 degrees around the z axis)

What you are doing above seems to be creating a view matrix and using that to generate a rotation quaternion. If you use the XMMatrixRotationQuaternion method I think you need to pass it a rotation matrix (not the view matrix).

Either way, once you have the rotation quaternion you apply it to the up axis to rotate the up direction with respect to the amount of roll you specified.

I think you should search around for a simplified explanation of quaternions. I am terrible at explaining this.
Actually you helped me quite a lot. Now the only one question that remains and that none of the sites I've read explains is the Orientation of the Camera. What exactly is the camera orientation? O.o
The orientation is determined by where the camera is looking, i.e. the target vector, and the up direction.

If your camera is rolling you create the quaternion for rotation about the target vector. You can then multiply the current up vector by that quaternion, and then that result by the conjugate of the quaternion (same as the inverse of a quaternion if the quaternion's vector component is uniform). This will give you a new up vector which will be in a direction which corresponds to the rotation about the target axis. Follow the example above for roll.

The view matrix can then be created using the

[background=rgb(250, 251, 252)]XMMatrixLookToLH( m_camPosition, m_camTarget, m_camUp) function.[/background]

This topic is closed to new replies.

Advertisement