• Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 vEyePt( two.getX()+(20*sin(radians)), two.getY()+5.0f-(20*tan(y)), two.getZ()+(20*cos(radians)));

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 this post


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


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?
please?

Share this post


Link to post
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 this post


Link to post
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.

I have had no luck.
Thanks iedoc

Share this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
I'm just going to start another post so its cleaner, Try this:

[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, 1.0f );
[color="#1C2837"]D3DXVECTOR3 camRight = [color="#1C2837"]D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
[color="#1C2837"]D3DXVECTOR3 camUp = [color="#1C2837"]D3DXVECTOR3( 0.0f, 1.0f, 0.0f );

[color="#1C2837"]D3DXMATRIX pitch;
[color="#1C2837"]
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();

[color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]camForward [color="#1C2837"], [color="#1C2837"]&[color="#1C2837"]camForward [color="#1C2837"], &pitch);
[color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]camRight [color="#1C2837"], [color="#1C2837"]&[color="#1C2837"]camRight [color="#1C2837"], &pitch);

// if i'm reading your code right, and "work" is the actual position of your camera;
[color="#1C2837"]camForward += [color="#1C2837"]work;
[color="#1C2837"]camRight += work;

[color="#1C2837"]//We could do the up vector two different ways
[color="#1C2837"]//Way one;
[color="#1C2837"][font="arial, sans-serif"]D3DXVec3Cross(&camUp, &camTarget, &camRight);[/font][color="#1C2837"][font="arial, sans-serif"]
[/font]
[color="#1C2837"][font="arial, sans-serif"]//Way two;[/font]
[color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]camUp [color="#1C2837"], [color="#1C2837"]&[color="#1C2837"]camUp [color="#1C2837"], &pitch);
[color="#1C2837"]camUp += work;
[color="#1c2837"]
[color="#1C2837"]At least I think that looks right

Share this post


Link to post
Share on other sites

[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]

I prefer source tags to code tags.

Share this post


Link to post
Share on other sites
Posted (edited) · Hidden
Hidden
Is there some sort of trick to using tags then? because source tags still give me the same output except its not in its own little window

"["source"]"
[source]
[color=#1C2837][size=2]// Not working...
[color=#1C2837][size=2]float luckyFloat;
[color=#1C2837][size=2][/source]
[color="#1C2837"]"["/source"]"

[color="#1C2837"]"["code"]"
[color="#1c2837"]

[color=#1C2837][size=2]// Not working...
[color=#1C2837][size=2]float luckyFloat;
[color="#1c2837"]

[color="#1c2837"]"["/code"]"

[color="#1c2837"]I guess instead of going off topic here, could someone post a quick link for me or explain why code and source tags don't work the same for me as everyone else?

[color="#1c2837"]figured it out... have to add the language in the source tag Edited by iedoc

Share this post


Link to post
Thanks iedoc that has been a big help =) and works =)

except for one tiny thing.

when ever I move the object now once I rotate that camera just goes absolutely nuts and goes which ever way it wants to.
would re arranging the order or the entire thing fix that? or do you have any ideas?
and I really need to change the name of the work vector. It was a desperation name haha


Either way thanks =)

Share this post


Link to post
Share on other sites
I was having that problem with the camera going nuts a while ago too, haha, so i know exactly what you mean. I think it ended up being the up vector

I guess I can't really say what your problem is for sure, but i wonder if its because your updating the the current camera vectors with the new rotation matrix without resetting the vectors, or something like that. For example:

[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, 1.0f ); // Declared globaly
[color="#1C2837"][color="#1C2837"]
[color="#1C2837"][color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]camForward [color="#1C2837"], [color="#1C2837"]&[color="#1C2837"]camForward [color="#1C2837"], &pitch); // If camForward is not reset back to (0, 0, 1) before you call this function, then camForward will be rotated more and more EVERY frame
[color="#1C2837"][color="#1C2837"]
[color="#1C2837"][color="#1C2837"]make sure camForward and the other camera vectors are reset back to their defaults before you rotate them. In fact, I'm not sure if this would compile or not, but this would be better if you could do this:
[color="#1C2837"][color="#1C2837"]
[color="#1C2837"][color="#1C2837"]D3DXVec3TransformCoord(&[color="#1C2837"]camForward [color="#1C2837"], [color="#1C2837"]D3DXVECTOR3( 0.0f, 0.0f, 1.0f )[color="#1C2837"][color="#1C2837"] [color="#1C2837"], &pitch);


[color="#1c2837"]It's seems really easy to explain in my head but i'm having troubles typing what i mean, sorry

[color="#1c2837"]If your still having the problem, would you mind posting what you have so far?

[color="#1c2837"]thanks yckx, I'll use source tags from now on. I found out why i get those extra tags to show up when i use source and code tags, but i don't understand why the code tag makes everything show up on one line... oh well, at least the source tag works

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement