XNA Game Development Forums
2012/05/21 20:34:20 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Camera rotation issues (z-axis drift solved!)  (Read 4272 times)
ppardee
Newbie
*
Offline Offline

Posts: 4


View Profile
« on: 2008/09/13 20:54:10 »

I am trying to get the camera to rotate as it would in a first person shooter, but if I pan, tilt and then pan again, the camera has rolled along the Z axis.  I think this is due to the fact that the camera is using its own rotation (myRotation) quaternion to make the matrix and after it tilts, the Y axis is no longer aligned with the world's and it causes the pan do to wonky things, but I don't know how to fix this.  Any suggestions would be appreciated!! (Great tutorials!!!!!!)

Thanks!

Paul
« Last Edit: 2008/09/14 22:08:49 by mikeschuld » Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #1 on: 2008/09/14 17:42:20 »

This has been a source of much confusion for a long time on these forums (one that has bugged me quite a bit actually Wink) You can modify the rotate function to accomplish this sort of action for you. I'm not positive as to how you would want to go about this, but I'll work on a version myself and let you know when I get it working Wink It shouldn't take too long.
« Last Edit: 2008/09/14 18:22:20 by mikeschuld » Logged
Chr0n1x
Global Moderator
Sr. Member
*****
Offline Offline

Posts: 307


View Profile WWW
« Reply #2 on: 2008/09/14 20:10:11 »

Source code for Riemer's book, chapter 2 part 3 is about a FPS camera. You will find in this case you DO want Gimbal Lock, so using Vectors for a FPS camera rotation is appropriate. Playing around in a FPS game, you'll find you encounter gimbal lock in most FPS games.

You create your X rotation before your Y rotation from the 2 vectors, and always transform your Up and Target vectors by the rotation. (The target is so that it retargets based on your movement, like in FPS.)
Logged

mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #3 on: 2008/09/14 21:37:50 »

I figured out you can set the z component of the quaternion to 0 and then normalize it again. It messes up the revolve functionality, but the rotate works pretty well.
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #4 on: 2008/09/14 22:08:17 »

Even better... Add these to your camera:

        public void RotateGlobal(Vector3 axis, float angle) {
            myRotation = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(axis, angle) * myRotation);

            Update();
        }

        public void TranslateGlobal(Vector3 distance) {
            myPosition += distance;

            Update();
        }

        public void RevolveGlobal(Vector3 axis, float angle) {
            Quaternion rotate = Quaternion.CreateFromAxisAngle(axis, angle);
            myPosition = Vector3.Transform(myPosition - myTarget, Matrix.CreateFromQuaternion(rotate)) + myTarget;

            RotateGlobal(axis, angle);
        }

Then just use RotateGlobal for your Y. Works for revolve too Wink I'll add these into the XNA 3.0 versions of the tutorials.
Logged
ppardee
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #5 on: 2008/09/15 17:27:18 »

Thanks for the replies!


Mike, the RotateGlobal works only when looking directly down the world's Z-axis, unfortunately.  If you turn 90 degrees and look up and down, the camera rotates on its Z-axis instead of its X-axis because it is now aligned with the world's Z axis.   Cry

But I will keep looking, you gave me some ideas to try and I will get back to you on them
Logged
ppardee
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #6 on: 2008/09/15 17:59:00 »

Here is my solution... I think it works....

I added to the HMCamera class:

private float yRotation = 0;
private float xRotation = 0;

        public void Rotate(float xAngle, float yAngle)
        {
            xRotation += xAngle;
            yRotation += yAngle;
            myRotation = Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationX(yRotation) *
                Matrix.CreateRotationY(xRotation));
        }


And then in HMDemo:

        static void mouse_OnMove(Vector2 move)
        {
            HMCameraManager.ActiveCamera.Rotate(move.X * -0.005f, move.Y * -0.005f);
            //HMCameraManager.ActiveCamera.Rotate(new Vector3(1, 0, 0), move.Y * -0.005f);
            //HMCameraManager.ActiveCamera.Rotate(new Vector3(0, 1, 0), move.X * -0.005f);
        }

Let me know if there are any bugs (or if you know why this works because I stole it from Riemers' XNA Tutorial)

// Edit

Or even more flexible... HMCamera

        private float xRotation = 0;
        private float yRotation = 0;
        private float zRotation = 0;

        public void Rotate(Vector3 axis, float angle)
        {
            xRotation += MathHelper.Clamp((float)axis.X, 0.0f, 1.0f) * angle;
            yRotation += MathHelper.Clamp((float)axis.Y, 0.0f, 1.0f) * angle;
            zRotation += MathHelper.Clamp((float)axis.Z, 0.0f, 1.0f) * angle;

            myRotation = Quaternion.CreateFromRotationMatrix(
                Matrix.CreateRotationX(xRotation) *
                Matrix.CreateRotationY(yRotation) *
                Matrix.CreateRotationZ(zRotation));

            Update();
        }

Everything else stays the same as it was in the tutorial...
« Last Edit: 2008/09/15 19:20:26 by ppardee » Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #7 on: 2008/09/16 09:39:42 »

I am not sure you are understanding the rotateglobal meaning. If you turn the camera so it's local Z is on the world X and rotate world X... it is supposed to be rotating on the camera's Z. It rotates on the global X. to get the first person type of camera you are talking about with my functions you need to do:

            HMCameraManager.ActiveCamera.Rotate(new Vector3(1, 0, 0), move.Y * 0.01f);
            HMCameraManager.ActiveCamera.RotateGlobal(new Vector3(0, 1, 0), move.X * 0.01f);

That is working for me just fine Smiley Try adding the skybox and playing around with it.
Logged
ppardee
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #8 on: 2008/09/16 16:25:55 »

Not sure where my typo was yesterday, but it works today and that is all I care about Smiley
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #9 on: 2008/09/17 08:16:40 »

Sorry for all the hassle this one has caused everyone. This solution should be the real final true actual one (disclaimer: some statements may be exaggerated) that will go into the final camera class used in the engine.

It at least is going to be in the XNA 3.0 tutorials (already is in fact) and will be released as soon as I finish the updates.
Logged
inbreed
Newbie
*
Offline Offline

Posts: 23


Beer! more beer!


View Profile
« Reply #10 on: 2008/11/19 11:22:49 »

i still could point to my quakelike fix http://www.thehazymind.com/smf/index.php?topic=270.0 if i wanted, but i got no time :/

Tongue
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Having issues with tutorial 3 Hazy Mind XNA Engine siferion 4 2377 Last post 2006/11/26 14:04:03
by mikeschuld
Not good - issues in the first tutorial. Hazy Mind XNA Engine « 1 2 » Jeddak 20 5375 Last post 2007/03/26 19:31:08
by mikeschuld
Question about getting rotation angles from Quaternions... General Discussion DaphydTheBard 7 3134 Last post 2007/03/20 07:27:20
by DaphydTheBard
Camera Issues Hazy Mind XNA Engine minich21 6 3047 Last post 2007/08/14 21:27:30
by mikeschuld
J.I Styles skin shader implementation issues Hazy Mind 3D Engine DX 1 2117 Last post 2007/03/30 07:53:25
by Nemo Krad
Camera Issues Hazy Mind XNA Engine Trano 3 2811 Last post 2008/09/17 08:27:25
by mikeschuld
Camera Tracking Hazy Mind XNA Engine muchrejoicing 4 2007 Last post 2007/05/29 19:50:23
by daenris
Camera problem Hazy Mind XNA Engine Montynis 2 1686 Last post 2007/08/02 09:54:14
by Montynis
GameTime issues General Discussion Mike2 2 2021 Last post 2007/09/14 15:22:07
by Mike2
[Solved] Help with skybox Hazy Mind XNA Engine Ren 0 1116 Last post 2009/03/02 19:18:15
by Ren
extending the camera classes/cameramanager Hazy Mind XNA Engine Mikeske 7 2079 Last post 2009/12/16 01:46:28
by Mikeske
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.09 seconds with 18 queries.