**0**

###
#1
Members - Reputation: **110**

Posted 02 July 2012 - 02:57 PM

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.

###
#2
Members - Reputation: **195**

Posted 02 July 2012 - 05:53 PM

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.

**Edited by Hornsj3, 02 July 2012 - 06:01 PM.**

###
#3
Members - Reputation: **110**

Posted 03 July 2012 - 08:07 AM

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

###
#4
Members - Reputation: **195**

Posted 03 July 2012 - 10:18 AM

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.

###
#6
Members - Reputation: **195**

Posted 04 July 2012 - 06:41 AM

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

XMMatrixLookToLH( m_camPosition, m_camTarget, m_camUp) function.

**Edited by Hornsj3, 04 July 2012 - 06:43 AM.**