Thread: Problems moving the camera

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Problems moving the camera

    To post on Game Programming forum:

    I'm starting work on my first game, and I'm doing all the programming from scratch in C# using DirectX. But I've picked up a small problem.
    I use the following code:

    Code:
    device.Transform.View = Matrix.Translation(new Vector3(Camera.posX, Camera.posY, Camera.posZ)) * Matrix.RotationYawPitchRoll(Camera.lookX, Camera.lookY, 0);
    to handle camera movement. So When I move the mouse the camera looks around, and moving the mouse up or down tilts the camera up or down, just as you would expect. But, the further I turn, the less acurrate the camera tilting becomes. It is a bit difficult to explain. Lets say I've turned left 90 degrees to the left. I now try to look up or down, but instead it rotates the camera. This doesn't happen when the camera is looking straight ahead when I start the app, but the further I turn, the worse the rotating becomes when I move the mouse up or down.

    Does anyone know what's going on here, or how I can fix it?

    BTW: Here is the code I use to get the mouse position:

    Code:
    DInput.MouseState state = mouseDevice.CurrentMouseState;
    
    Camera.lookY += ((state.Y*-1)*mouseSensitivity);
    Camera.lookX += ((state.X*-1)*mouseSensitivity);
    PS: While I was writing this post, I ran the app again, and it would seem that it is actually rotating my ground mesh instead of my camera. This would make sense because if I am looking to the right, and I drag the mouse down to tilt the camera, it is actually tilting the world or the mesh, which makes it tilt to the left. Any advice?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You are experiencing gimbal lock. It happens when one axis of rotation is mapped onto another axis of rotation. To prevent this you can perform axis angle rotations or you can use quaternions.

    As you pitch up or down and then attempt to yaw the closer you are to the 'pole' of the unit sphere of rotation, the more your yaw becomes a z type of rotation instead of a yaw or y rotation. This is a mathematical anomaly with euler angles and cannot be prevented except by altering your representation of rotations.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Thanks for the reply. I found a page on gamedev.net that shows how to rotate the camera. It involves a lot of trig. But now I have another problem. When it moves the camera, it moves it in the direction you are looking, which is good. But if I look up it moves up as well. How can I make the camera stay at a constant height while moving?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You will need two orientations to do this. One for the object that is moving and one for the camera inside the object. When the camera rotates it will not alter the direction of the object.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Quote Originally Posted by Bubba;
    710050
    You will need two orientations to do this. One for the object that is moving and one for the camera inside the object. When the camera rotates it will not alter the direction of the object.
    How would I do that? This is the first time I'm working with DirectX, so I don't exactly understand how to do that.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This has nothing to do with DirectX. Essentially you want the camera separate from the object it is attached to. The mouse control then would either move the camera (like looking left or right) or it would move the ship. You may want to move the camera if the user holds down the left or right mouse button and moves the mouse. Otherwise the mouse movement would be interpreted as a ship or object movement.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    This is the code that I am using for moving the camera:

    Code:
    public static void SetCamera(Vector3 cPosition, float h, float v)
    {
    	cameraPosition = cPosition;
            cameraTarget = new Vector3();
            cameraUpVector = new Vector3();
            hRadians = h;
            vRadians = v;
    
            RotateCamera(0, 0);
    }
    
    public static void RotateCamera(float h, float v)
    {
    	hRadians += h;
            vRadians += v;
    
            cameraTarget.Y = cameraPosition.Y + (float)(radius * Math.Sin(vRadians));
            cameraTarget.X = cameraPosition.X + (float)(radius * Math.Cos(vRadians) * Math.Cos(hRadians));
            cameraTarget.Z = cameraPosition.Z + (float)(radius * Math.Cos(vRadians) * Math.Sin(hRadians));
    
            cameraUpVector.X = cameraPosition.X - cameraTarget.X;
            cameraUpVector.Y = Math.Abs(cameraPosition.Y + (float)(radius * Math.Sin(vRadians + Math.PI / 2)));
            cameraUpVector.Z = cameraPosition.Z - cameraTarget.Z;
    }
    
    public static void SlideCamera(float h, float v)
    {
    	cameraPosition.Y += v * moveDist;
            cameraPosition.X += h * moveDist * (float)Math.Cos(hRadians + Math.PI / 2);
            cameraPosition.Z += h * moveDist * (float)Math.Sin(hRadians + Math.PI / 2);
            RotateCamera(0, 0);
    }
    
    public static void MoveCamera(float d)
    {
    	cameraPosition.Y += d * moveDist * (float)Math.Sin(vRadians);
            cameraPosition.X += d * moveDist * (float)(Math.Cos(vRadians) * Math.Cos(hRadians));
            cameraPosition.Z += d * moveDist * (float)(Math.Cos(vRadians) * Math.Sin(hRadians));
            RotateCamera(0, 0);
    }
    I got this code from gamedev.net. Is it possible to modify the code so that I can only move along a specific axis, so that I don't move up when I look up?

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I got this code from gamedev.net. Is it possible to modify the code so that I can only move along a specific axis, so that I don't move up when I look up?
    I answered this question. Use two orientations. One for the camera and one for the object.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    I found the solution. In the MoveCamera() method above I removed the movement on the Y axis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Camera rotate to function
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 11-15-2008, 12:22 PM
  2. Problems with GetAsyncKeyState
    By blurrymadness in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2007, 06:13 PM
  3. openGL space shooter problems
    By c_young in forum Game Programming
    Replies: 0
    Last Post: 01-17-2007, 04:33 PM
  4. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  5. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM