# Rotating camera around the y axis with a locked camera on a mesh

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

## Recommended Posts

Hopefully the topic made sense if not.
Basically I have a cute plane that i'm flying around 3rd person with a locked camera on the plane. Only problem is when I do a "flip" (y axis) the camera only follows 179degrees then flips out.
Once again I've searched everywhere only to find nothing really.
Any help would be much appreciated =)
i've read there is a yaw pitch roll but I haven't the slightest clue on how to implement it or if that would even help in my position
[color="#0000ff"][color="#0000ff"]

static [color="#0000ff"][color="#0000ff"]float index = 0.0f; index+=0.1f; [color="#008000"][color="#008000"]// an ever-increasing float value

[color="#0000ff"][color="#0000ff"]float radians = r / 57.29578;

[color="#008000"][color="#008000"]// draw camera, two is the plane object and mZ mY are mouse movement only when clicked otherwise unclicked locks back on. 20 is the distance away from object at all times

D3DXVECTOR3 vLookatPt( two.getX()-mZ,two.getY()+mY, two.getZ());

should I be using tan? i thought about kind of rotating everything around and using cos sin for Y and Z. but adding that in seemed to not work at all.
Any suggestions?
Any help would be much appreciated =)

##### Share on other sites
What function are you using to make your view matrix? is it D3DXMatrixLookAtLH()? If that's the case, are you making sure to rotate your "up" vector along with your target vector? If you don't update your "up" vector, which is the fourth parameter of D3DXMatrixLookAtLH(), you will get that effect where the camera does a 180 when you get to about 180 degrees looking up or down. Just a suggestion

##### Share on other sites
Yes, that's my recommendation as well: consider your up vector. When the camera's lookat vector approaches its up vector, sudden and irritating flips can occur. If you make rotations freely in three dimensions, you'll need to adjust your up vector as you go.

##### Share on other sites
The Up vector thing is exactly what I was going to suggest.

I encountered the same sort of effect when I was visualising the position of the sun in my current game project. I use the sun position as the eye of a camera and set the look at to be the origin of the world, (I forgot to properly set the up vector) and as it passed directly overhead the camera flipped around 180 degrees. This turned out to be pretty useful in my case.

Your options for setting the Up Vector are either to rotate it along with everything else or calculate it using the cross product from your look at and left or right vectors.

##### Share on other sites
well

D3DXVECTOR3 vEyePt(two.getX()+(20*sin(two.getAX())+(20*sin(two.getAZ()))), 5.0f-(20*sin(two.getAY())), two.getZ()+(20*cos(two.getAX()))+(20*sin(two.getAY())));

I know that looks like a nightmare. But it all works except the very last one one the Z axis which is supposed to move when I pitch.

It starts to mess up even before I get a 180 switch.

I did update my upvec to deal with the 180 switch

D3DXVECTOR3 vUpVec( 0.0f, cos(two.getAY()/2, 0.0f );

but for the moment i'm stumped

##### Share on other sites
Ok so I got it to work kind of using this

[color="#0000ff"][color="#0000ff"]

float a = two.getX()+(20*sin(two.getAX()))+(20*sin(two.getAZ()));

[color="#0000ff"][color="#0000ff"]float b = two.getY()-(20*sin(two.getAY()));

[color="#0000ff"][color="#0000ff"]float c = two.getZ()+(20*cos(two.getAX()+two.getAY()));

D3DXVECTOR3 vEyePt(a, b, c);

now the only problem is when I rotate just a little bit and flip the z and the X flip out and don't move how they should

Anyone have any clue or idea how to help me?

##### Share on other sites
Just wondering, is there any reason you're not just using the d3d matrix rotation functions, such as D3DMatrixRotationX() or D3DXMatrixRotationYawPitchRoll()? It sort of seems like your just recreating the wheel by doing these rotations yourself. When it comes to transforming vectors, these matrx transformation vectors are really useful. You don't even need to exactly know much math to use them. Just create a D3DXMATRIX object, set it with the matrix funciton you want, such as D3DXMatrixRotationYawPitchRoll(), then use D3DXVec3TransformCoord() to transform the vector.

Yaw is like looking left and right, the rotation on the y axis
Pitch is like looking up and down, the rotation on the x axis
Roll is like a barrel roll or whatever, the rotation on the z axis

When you say "locked camera" in the title, do you actually mean "camera locked"? because saying locked camera made me think that your camera wasn't moving or something, but that gave me an idea, I guess if i was working with this problem, I would try keeping the camera in a single position while i worked out the problem with it actually rotating and staying locked on the plane or cube, if you know what i mean. maybe your already doing it, or maybe it wouldn't help at all, just thought i would suggest it.

##### Share on other sites
I've tried doing what you said with no luck really. This is frustrating really.
could you show me how to implement the D3DXMatrixRotationYawPitchRoll() and D3DXVec3TransformCoord()

this is what I tried
D3DXMatrixRotationYawPitchRoll(&look, two.getAX(), two.getAY(), two.getAZ());

look *= 20.0f;

D3DXVECTOR3 work = look;

work+=two.Position();

this just pretty much sets it to the left of the object and when I yaw it looks at the left of it and the camera follows it but when I pitch the camera just doesn't even move.
and the *20 is for the distance I was hoping to keep it away from the camera.

Thanks iedoc

##### Share on other sites
Ok i've figured it out!

I needed to do this

D3DXMATRIX pitch;

D3DXMatrixRotationYawPitchRoll(&pitch, two.getAX(), two.getAY(), two.getAZ()); [color="#008000"][color="#008000"]// Rotate based off ships rotation

D3DXVECTOR3 work = D3DXVECTOR3(0.0f, 5.0f, 20.0f); [color="#008000"][color="#008000"]// set how far up we want the camera and back

D3DXVec3TransformCoord(&work, &work, &pitch); [color="#008000"][color="#008000"]// transform the rotation matrix and the up and back vector into the vector

work+=two.Position();

and now it works perfectly.

But going back to the problem you guys started mentioning in the beginning. The upVector. It seems to not be cooperating with me at all and I'd like to slap it.

the vector is (0.0f, cos(two.getAY()), 0.0f) and at 90 degrees it flips the camera which is 90 degrees to soon. So I simply divided the angle by two cos(two.getAY()/2) and for some strange reason it still flips at 90 degrees...
Any ideas why?

##### Share on other sites
Let me try to help you out. You will want a "Forward" and "Right" vectors, where the forward is always pointing down the +z axis, and the Right is always pointing down the +x axis. These vectors will be used to update your cameras forward vector "target" and the cameras right vector. Then when updating the camera, you will use the yah pitch roll function with the amount of rotation on each axis you want, and update the forward and right vectors. You will then add the position of the camera to these vectors, and set them as the cameras forward and right vectors. Then to get the up vector, you will cross the cameras forward and right vector using [font="arial, sans-serif"]D3DXVec3Cross(). So i'll try to show you this in code, its code straight from my head so its probably not perfect and might not even work without some tweaks, but i hope it helps anyway[/font]

[font="arial, sans-serif"][color="#1C2837"][font="arial, verdana, tahoma, sans-serif"]D3DXVECTOR3 worldForward = [/font][/font][color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
[color="#1C2837"]D3DXVECTOR3 worldRight = [color="#1C2837"]D3DXVECTOR3( 1.0f, 0.0f, 0.0f );

[font="arial, sans-serif"][color="#1C2837"][font="arial, verdana, tahoma, sans-serif"]D3DXVECTOR3 camForward = [/font][/font][color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
[color="#1C2837"]D3DXVECTOR3 camRight = [color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
[color="#1C2837"]D3DXVECTOR3 camUp = [color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 0.0f );

[color="#1C2837"]D3DXMATRIX camRotation;

[color="#1C2837"]D3DXMatrixRotationYawPitchRoll( &[color="#1C2837"]camRotation[color="#1C2837"], two.getAX(), two.getAY(), two.getAZ() );

[color="#1C2837"]//To make sure your angles don't get too big or small, do this for each of the angles; [color="#1C2837"]two.getAX(), two.getAY(), two.getAZ()
[color="#1c2837"]//You'll probably want to do this wherever the angles are actually updated
[color="#1C2837"]if([color="#1C2837"] two.getAX() >= 6.28f )
[color="#1C2837"]two.getAX() = 0.0f;
[color="#1C2837"]if([color="#1C2837"] two.getAX() <= -6.28 )
[color="#1C2837"]two.getAX() = 0.0f;

[color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]worldForward[color="#1C2837"], &[color="#1C2837"]worldForward[color="#1C2837"], &[color="#1C2837"]camRotation[color="#1C2837"]);
[color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]worldRight [color="#1C2837"],&[color="#1C2837"]worldRight[color="#1C2837"], &[color="#1C2837"]camRotation[color="#1C2837"]);

[color="#1C2837"]camTarget = worldForward + camPosition;
[color="#1C2837"]camRight = worldRight + camPosition;

[color="#1C2837"]//Define the up vector
[font="arial, sans-serif"]D3DXVec3Cross(&camUp, &camTarget, &camRight);[/font]
[font="arial, sans-serif"] [/font]
I just realized this would work for a first person camera. But it seems like you have all the rotation stuff figured out, and just having problems with the up vector still. Updating the up vector is still the same as a first person camera though, which is:

[font="arial, sans-serif"]D3DXVec3Cross(&camUp, &camTarget, &camRight);[/font]

[font="arial, sans-serif"] [/font][font="arial, sans-serif"]btw, what is up with code tag? I have NEVER been able to get it to work right...[/font]
[font="arial, sans-serif"][/font][font="arial, sans-serif"][color="#1C2837"][font="arial, verdana, tahoma, sans-serif"]D3DXVECTOR3 worldForward = [/font][/font][color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 1.0f ); [color="#1C2837"]D3DXVECTOR3 worldRight = [color="#1C2837"]D3DXVECTOR3( 1.0f, 0.0f, 0.0f ); [color="#1C2837"] [font="arial, sans-serif"][color="#1C2837"][font="arial, verdana, tahoma, sans-serif"]D3DXVECTOR3 camForward = [/font][/font][color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); [color="#1C2837"]D3DXVECTOR3 camRight = [color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); [color="#1C2837"]D3DXVECTOR3 camUp = [color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); [color="#1C2837"]D3DXMATRIX camRotation; [color="#1C2837"] [color="#1C2837"]D3DXMatrixRotationYawPitchRoll( &[color="#1C2837"]camRotation[color="#1C2837"], two.getAX(), two.getAY(), two.getAZ() ); [color="#1C2837"] [color="#1C2837"]//To make sure your angles don't get too big or small, do this for each of the angles; [color="#1C2837"]two.getAX(), two.getAY(), two.getAZ() [color="#1C2837"]if([color="#1C2837"] two.getAX() >= 6.28f ) [color="#1C2837"] [color="#1C2837"]two.getAX() = 0.0f; [color="#1C2837"]if([color="#1C2837"] two.getAX() <= -6.28 ) [color="#1C2837"] [color="#1C2837"]two.getAX() = 0.0f; [color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]worldForward[color="#1C2837"], &[color="#1C2837"]worldForward[color="#1C2837"], &[color="#1C2837"]camRotation[color="#1C2837"]);[color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]worldRight[color="#1C2837"] [color="#1C2837"],&[color="#1C2837"]worldRight[color="#1C2837"], &[color="#1C2837"]camRotation[color="#1C2837"]); [color="#1C2837"] [color="#1C2837"]camTarget = worldForward + camPosition; [color="#1C2837"]camRight = worldRight + camPosition; [color="#1C2837"] [color="#1C2837"]//Define the up vector [font="arial, sans-serif"]D3DXVec3Cross(&camUp, &camTarget, &camRight);[/font] [font="arial, sans-serif"][/font]

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632167
• Total Posts
3004527

×