Sign in to follow this  

help with camera rotation

Recommended Posts

alright im trying to make a camera similar to a first person shooter i have got the movement down but i cant figure out how to do the rotation im using directx and using the mouse for input on the rotations i only need to do rotations around the x and y axis... i know there are some functions already included in directx i looked through tons of pages about quaternions and such but im having trouble figuring out the values to put into the calculations and how to apply it once its gone through the calculations what would be the best method for me to use? if its using quaternions... what are the steps needed to do it... thanks

Share this post

Link to post
Share on other sites
Three values you put in: roll, pitch and yaw, all as Euler angles.
These will define your rotation about the axes.

First, you have to create a new quaternion for each axis using the following equations:

Qroll = { cos(roll/2), [sin(roll/2)]i + 0j + 0k}
Qpitch = { cos(pitch/2), 0i + [sin(pitch/2]j + 0k}
Qyaw = { cos(yaw/2), 0i + 0k + [sin(yaw/2)k}

Lastly, all is needed it to multiply the three new quaternions together. This will give you a final rotation Quaternion.

Along with this quaternion, you also need the conjugate (whch is simply the inverse vector, so -x, -y and -z. The scalar value should remain the same).

You should already have a quaternion defining your view direction. In which case, to define the new view quaternion, use the following equation.

NewView = ((Rotation * View) * ConjugateRotation)

My camera specifically has an Up vector and a Right vector. In order for me to display my new view, I need to reset my Right vector to the new data.

I do this by adding the vector of the NewView to my Position vector.
(So in my case Right = NewView.GetVector() + Position)

Hope this helps :)

Share this post

Link to post
Share on other sites
I personally wouldn't suggest to use quaternions for a FPS camera. They are a good choice of course but they introduce difficulties that aren't really needed. They are better for iterative cameras used in flight simulators or such.
My choice would be to use givens rotations in the sequence YXZ to create a rotation matrix. I won't go in details on that but the sequence matters for the rotations are dependent, YXZ is the widely used in FPSs.
I will just explain how you make a 3x3 rotation matrix out of 3 angles. If you need to know how to wrap it on Direct3D and stuff, ask or take a look on msdn.

#include <math.h>

struct Matrix3f // an example struct for a 3x3 matrix
float _[3][3];

Matrix3f RotationYXZ(const float angleX, const float angleY, const float angleZ)
Matrix3f result;
float sx = sinf(angleX);
float cx = cosf(angleX);
float sy = sinf(angleY);
float cy = cosf(angleY);
float sz = sinf(angleZ);
float cz = cosf(angleZ);
result._[0][0] = cy * cz + sy * sx * sz;
result._[0][1] = -cy * sz + sy * sx * cz;
result._[0][2] = sy * cx;
result._[1][0] = cx * sz;
result._[1][1] = cx * cz;
result._[1][2] = -sx;
result._[2][0] = -sy * cz + cy * sx * sz;
result._[2][1] = sy * sz + cy * sx * cz;
result._[2][2] = cy * cx;
return result;

About the parameters: angleY controls the look-left/look-right, angleX the look-down/look-up and angleZ can be used to simulate the head moving while you run (it twists on the center of the screen).

I suggest you look into some theory when you had enough fun with it, bests.

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