# Camera Rotations

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I've worked up a simple camera on D3D lately... there is several classes that make up this function. A camera class that stores all the matrices, such as view, world and projection A vector, matrix and quaternion class for my own work. I'm currently trying to create a rotation (ie, head-turning rotation) with the camera itself .. it should be the view matrix i think ... Here are my steps: 1) I used a quaternion to generate a matrix based on the euler angles I've passed in. 2) Afterwhich I used the generated matrix to multiply with into view, ie, view = view * generated The effects: At first the rotation seems ok... but as i move the camera away from its initial point, the rotation seem to rotate AROUND the initial lookAt position... Is there something that I did wrong? Sorry, I'm still quite new into 3D programming, therefore, may not understand certain concepts and principles. Please advise. Thanks, thrawn80

##### Share on other sites
Quote:
 Original post by Thrawn80Hi all,I've worked up a simple camera on D3D lately... there is several classes that make up this function.A camera class that stores all the matrices, such as view, world and projectionA vector, matrix and quaternion class for my own work.I'm currently trying to create a rotation (ie, head-turning rotation) with the camera itself .. it should be the view matrix i think ... Here are my steps:1) I used a quaternion to generate a matrix based on the euler angles I've passed in. 2) Afterwhich I used the generated matrix to multiply with into view, ie, view = view * generatedThe effects:At first the rotation seems ok... but as i move the camera away from its initial point, the rotation seem to rotate AROUND the initial lookAt position...Is there something that I did wrong? Sorry, I'm still quite new into 3D programming, therefore, may not understand certain concepts and principles.Please advise.Thanks,thrawn80

Yeah that happened to me too when I was constructing my first cam, and I was asking the same exactly question on this forum D. As I thought about it I figured that when I tried to rotate the camera I was rotating it in its LOCAL space. I personally didnt use Euler angles for my cam, so im gonna tell you how I did it with simple matrices and vectors. What i did is I built a small model of the camera using markers, so I had 3 markers, one used for lookat vector, other 2 for up and right vectors. So they were all perpendicular with the look at sticking out from the up and right(kinda like the 3d axes). Lets say we start with the lookat pointing away from you, and you rotate around the right(pitch) by lets say 30 deg down.So the up and the look at should have tilted with the rotation while the right stayed. Now rotate about the local up vector, if you have a small model you can easily see that now the righ vecor is moved off its plane and from now on the rotations are gonna be messed up.
What you have to do is
1. Have a world_UP vector that cannot be changed, and always point {0.0f, 1.0f, 0.0f} That is going to be your world reference around which you will rotate.
2. The local up vector of the camera is still existing, and you should use it to generate the lookat vector, but do NOT use it to rotate your camera around.
3. Now lets apply the previous example, the look at is pointing away, the righ vector points right, and there should be two up vectors, one global up, which you never change, so its alwyas the up of the world, and the local up which you do rotate.
4. So now if you want to rotate the cam around right vector (lets say 60 deg down), you do, and so you
rotate the LOCAL up of the camera around right and then CrossMultiply right and local up to get the lookat.
5. Now you want to rotate around up of the camera(yaw), and you do it by using the WORLD y, and then tilt the local right and local up along with the rotation. That will take care of that roll effect that youre getting.

So the key concept is to use the world up when you want to yaw. in d3d functions it would be something like

D3DXMATRIX mx_AroundWorldUp;
D3DXVECTOR3 world_pY = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
// rad_angle_aroundUp is the angle you want to rotate by

//Now you transform the local rigth and local up by that matrix
D3DXVec3TransformCoord(&v_Right, & v_Right, &mx_AroundWorldUp);
D3DXVec3TransformCoord(&v_Up, &v_Up, &mx_AroundWorldUp);

I advise you building a small cam vector model that would really help, because its hard visualizing all the rotations around differnt axes in your head.

Hope that helped, Tim.

##### Share on other sites
Hi,

I've gotten the basic concept ...

I guess I have to rotate the up, right and direction together...

The movement feel stimulated by my previous code was rather illusionary... While it appeared to be moving in the direction I was facing, actually, it was just a screwed up rotation in the steps which led to funny results.

Please correct me if I'm wrong ...:)

As for the correct movement (fps cam), I'd just hav to make use of the current up and right vector accordingly...

Thanks all,
thrawn80

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013596
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!