• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jozsef.horvath

Quaternion Camera rotation

5 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
So, as you suspected, you really messed up everything there [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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.

[CODE]
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);
}
[/CODE]

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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
[left][background=rgb(250, 251, 252)]XMMatrixLookToLH( m_camPosition, m_camTarget, m_camUp) function.[/background][/left] Edited by Hornsj3
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0