Jump to content
  • Advertisement
Sign in to follow this  
bobsaget

Xna first person camera problem?

This topic is 3647 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

I was following the tutorial at http://matt.insidegamer.org/documents/XNA%20Development-4.pdf after looking at xna creators club forum and it seems like a really good tutorial. I decided to use what I knew and combine it with the tutorial to try to create a first person camera that works with the mouse(move the mouse up the camera goes up, left looks left,ect). I added mouse input and updated the rotatecamera method based on some other advice but it seems that I am not achieving the desired results.

            cameraRotationX = Matrix.CreateRotationY(turnh);
            cameraRotationY = Matrix.CreateRotationX(turnv);
            transRef = Vector3.Transform(cameraRef, cameraRotationX +     cameraRotationY);
            lookAt = transRef + position;
            view = Matrix.CreateLookAt(position, lookAt, Vector3.Up);



Where both camerarotations equal matrix.identity(Its minor deviations from the authors original work. What is happening is when I rotate the camera in a 360 degree radius the camera looks down and then resets itself around the 180 degree point. It works fine for rotating around x and y separate but combined i get this effect. Any ideas? Here is the full source for the class ,
/***********************************
 * Author: Matt Christian
 * 
 * Description: Uses useful functions to move a game camera
 * 
 * Notes: For use with XNA Development Tutorials
 * 
 * If you use this for a project, please give credit!
 * http://matt.insidegamer.org/xnatutorials.aspx
 * 
 * ***********************************/

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace WindowsGame1
{
    class GameCamera
    {
        //Camera movement and rotation speeds
        public float rotationSpeed = 0.03f;
        private float forwardSpeed = 0.05f;

        //Amount to turn as a float
        public float turnAmt;
        public float turnv = 0;
        public float turnh = 0;
            //Position and reference vectors
        private Vector3 position;
        private Vector3 transRef;
        private Vector3 cameraRef;
        private Vector3 lookAt;

        //Screen ratio
        private float aspectRatio = 0.0f;

        //Any camera rotations
        private Matrix cameraRotationX;
        private Matrix cameraRotationY;
        //Projection and view matrix
        private Matrix projection;
        private Matrix view;

        /// <summary>
        /// GameCamera Constructor
        /// </summary>
        /// <param name="graphics"></param>
        public GameCamera(GraphicsDeviceManager graphics)
        {
            //Start aiming forward (no turn)
            turnAmt = 0;

            //Look off into the distance, set a reference vector
            lookAt = transRef = new Vector3(0.0f, 0.0f, 1.0f);

            //Starting position of the camera
            position = new Vector3(0.0f, 0.0f, 0.0f);

            //Direction camera points without rotations applied
            cameraRef = new Vector3(0.0f, 0.0f, 1.0f);

            //Aspect ratio of screen
            aspectRatio = graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height;

            //Initialize our camera rotation to identity
            cameraRotationX = Matrix.Identity;
            cameraRotationY = Matrix.Identity;
            //Create a general view matrix from start position and original lookat
            view = Matrix.CreateLookAt(position, lookAt, Vector3.Up);

            //Create general projection matrix for the screen
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
                    aspectRatio, 0.01f, 10000.0f);
        }

        /// <summary>
        /// Update Camera Position
        /// </summary>
        /// <param name="newPos"></param>
        private void UpdatePosition(Vector3 newPos)
        {
            //Create a new rotation matrix about the Y-Axis
            Matrix yRotation = Matrix.CreateRotationY(turnh);

            Vector3 currPos = Vector3.Transform(newPos, yRotation);

            //Set position of our camera to the v vector's new values
            position.X += currPos.X;
            position.Y += currPos.Y;
            position.Z += currPos.Z;

            //Update our lookAt Matrix
            lookAt = position + transRef;
            view = Matrix.CreateLookAt(position, lookAt, Vector3.Up);
        }

        /// <summary>
        /// Move camera along the Z-Axis using forwardSpeed
        /// </summary>
        public void MoveForward()
        {
            //Create a new vector to calculate speeds in certain directions
            Vector3 v = new Vector3(0.0f, 0.0f, forwardSpeed);

            UpdatePosition(v);
        }

        /// <summary>
        /// Move camera along the Z-Axis using -forwardSpeed
        /// </summary>
        public void MoveBack()
        {
            //Create a new vector to calculate speeds in certain directions
            Vector3 v = new Vector3(0.0f, 0.0f, -forwardSpeed);

            UpdatePosition(v);
        }

        /// <summary>
        /// Move camera along the X-Axis using forwardSpeed
        /// </summary>
        public void StrafeLeft()
        {
            //Create a new vector to calculate speeds in certain directions
            Vector3 v = new Vector3(forwardSpeed, 0.0f, 0.0f);

            UpdatePosition(v);
        }

        /// <summary>
        /// Move camera along the X-Axis using -forwardSpeed
        /// </summary>
        public void StrafeRight()
        {
            //Create a new vector to calculate speeds in certain directions
            Vector3 v = new Vector3(-forwardSpeed, 0.0f, 0.0f);

            UpdatePosition(v);
        }

        /// <summary>
        /// Returns Camera Projection Matrix
        /// </summary>
        /// <returns></returns>
        public Matrix GetProjectionMatrix()
        {
            return projection;
        }

        /// <summary>
        /// Returns Camera View Matrix
        /// </summary>
        /// <returns></returns>
        public Matrix GetViewMatrix()
        {
            //Get the newest view
            view = Matrix.CreateLookAt(position, lookAt, Vector3.Up);

            return view;
        }

        /// <summary>
        /// Rotates Camera Around the Y-Axis
        /// </summary>
        public void RotateCamera()
        {
            cameraRotationX = Matrix.CreateRotationY(turnh);
            cameraRotationY = Matrix.CreateRotationX(turnv);
            transRef = Vector3.Transform(cameraRef, cameraRotationX + cameraRotationY);
            lookAt = transRef + position;
            view = Matrix.CreateLookAt(position, lookAt, Vector3.Up);

            
        }
        
        /// <summary>
        /// Returns camera rotation
        /// </summary>
        /// <returns></returns>
        public float GetRotation()
        {
            return turnh;
        }


    }
}



I am giving credit to matt at http://matt.insidegamer.org/xnatutorials.aspx should I ask a mod to move it to for beginners because it is more basic than some of the other questions? [Edited by - bobsaget on June 29, 2008 11:11:11 AM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!