lid6j86's picture

Basic 3-D First Person Camera

Took me forever to research and figure out, but I finally got something basic implemented. It is extremely straight-forward. I used XNA for the gamepad, all you need to do is change it to keyboard inputs for it to be valid. I'm sure it can be optimized but this is at least to get people started.

        Vector3 camCoord = new Vector3();
        Vector2 camAngle = new Vector2();
 
 
        XNA.GamePadState prevState;
        XNA.GamePadState gamePad;
 
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
 
            const float PIE = 3.141592654f;
 
            gamePad = XNA.GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
 
            camAngle.Y += gamePad.ThumbSticks.Right.X * (float)e.Time * 180;
            camAngle.X += gamePad.ThumbSticks.Right.Y * (float)e.Time * 180;
 
            Vector2 RadAng = new Vector2();
 
            RadAng.X = (camAngle.X / 180 * PIE);
            RadAng.Y = (camAngle.Y / 180 * PIE);
 
 
            if (gamePad.ThumbSticks.Left.Y > 0)
            {
                camCoord.X -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.Y);
                camCoord.Z += (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.Y);
                camCoord.Y += (float)(Math.Sin(RadAng.X) * gamePad.ThumbSticks.Left.Y);
            }
            if (gamePad.ThumbSticks.Left.Y < 0)
            {
                camCoord.X -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.Y);
                camCoord.Z += (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.Y);
                camCoord.Y += (float)(Math.Sin(RadAng.X) * gamePad.ThumbSticks.Left.Y);
            }
            if (gamePad.ThumbSticks.Left.X > 0)
            {
                camCoord.X -= (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.X);
                camCoord.Z -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.X);
            }
            if (gamePad.ThumbSticks.Left.X < 0)
            {
                camCoord.X -= (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.X);
                camCoord.Z -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.X);
            }
 
            //camCoord.X -= (float)(Math.Sin(RadAng.Y));
            //camCoord.Z += (float)(Math.Cos(RadAng.Y));
            //camCoord.Y += (float)(Math.Sin(RadAng.X));
 
            if (camAngle.X > 89)
            {
                camAngle.X = 89;
            }
            if (camAngle.X < -89)
            {
                camAngle.X = -89;
            }
 
            if (camAngle.Y > 360)
            {
                camAngle.Y -= 360;
            }
            else if (camAngle.Y < 0)
            {
                camAngle.Y += 360;
            }
        }
 
        private void CameraMatrix()
        {
            GL.Rotate(camAngle.X, 1, 0, 0);
            GL.Rotate(camAngle.Y, 0, 1, 0);
            GL.Translate(camCoord);
        }
 
 protected override void OnRenderFrame(FrameEventArgs e)
        {
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
            GL.LoadIdentity();
            CameraMatrix();
 
            //Render your stuffs           
 
            SwapBuffers();
        }

Phew. off to the next thing!