2 Replies - 357 Views - Last Post: 04 March 2013 - 12:59 PM Rate Topic: -----

#1 Quin  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 381
  • Joined: 16-October 08

Moving a First Person Camera - Land Based

Posted 04 March 2013 - 03:03 AM

Hi guys

This probably sounds quite a newbie question, but TBH I am a newbie with C# and doing this type of thing :-)

I've created a first person camera for my final year project at Uni, but it looks more like a flight simulator than a FPS camera. My idea was to lock the Y axis by setting the Direction to 0 when the player moves forward/backward, but this also affects the mouse movement when looking around.
The camera is the only thing that has given me so many problems, and I've only a couple of days to get it sorted.

Are there any suggestions?

What I'm using right now:
//  Move forward/backward
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                cameraDirection.Y = 0;
                cameraPosition += cameraDirection * speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                cameraDirection.Y = 0;
                cameraPosition -= cameraDirection * speed;
            }


Is This A Good Question/Topic? 0
  • +

Replies To: Moving a First Person Camera - Land Based

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,652
  • Joined: 05-May 12

Re: Moving a First Person Camera - Land Based

Posted 04 March 2013 - 06:49 AM

There is not enough information in your question. No hint at all in the types you use for the variables in the code you posted. Although I can probably guess that cameraDirection is some vector class, I'm not willing to bet on it. Also you make no mention of how your camera works. Is it based on a unit vector to indicate direction, or some other system? And lastly, you mention that it affects the mouse movement, but don't show us how you handle mouse movement.

An then my pet peeve: Why do you assume that WASD works for directions on a keyboard? I'm using a Dvorak keyboard layout, and my letters W, A, S, and D are not laid out in an inverted T like you are assuming. Also imagine other people in other countries who may have different keyboard layouts.

This post has been edited by Skydiver: 04 March 2013 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Quin  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 381
  • Joined: 16-October 08

Re: Moving a First Person Camera - Land Based

Posted 04 March 2013 - 12:59 PM

Sorry, I just assumed that people would just get it straight away.

The controls are WASD, just for the debugging (I'm going to have those changeable at some point); so that's forwards, side to side, and backwards. Mouse movement is 'look'.

Here's the entire code
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;

namespace _3D_Game
{
    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        //  Camera matricies
        public Matrix view { get; protected set; }
        public Matrix projection { get; protected set; }

        //  Camera vectors
        public Vector3 cameraPosition { get; protected set; }
        Vector3 cameraDirection;
        Vector3 cameraUp;

        MouseState prevMouseState;

        float speed = 3;

        //  Max yaw/pitch variable
        float totalYaw = MathHelper.PiOver4 / 2;
        float currentYaw = 0;
        float totalPitch = MathHelper.PiOver4 / 2;
        float currentPitch = 0;

        public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
            : base(game)
        {
            //  view = Matrix.CreateLookAt(pos, target, up);
            //  Build camera view matrix
            cameraPosition = pos;
            cameraDirection = target - pos;
            cameraDirection.Normalize();
            cameraUp = up;
            CreateLookAt();

            projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                (float)Game.window.ClientBounds.Width /
                (float)Game.window.ClientBounds.Height,
                1, 3000);
        }

        public override void Initialize()
        {
            //  Set mouse position and do intial get state
            Mouse.SetPosition(Game.window.ClientBounds.Width / 2,
                Game.window.ClientBounds.Height / 2);
            prevMouseState = Mouse.GetState();
                
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            //  Move forward/backward
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
//                cameraDirection.Y = 0;
                cameraPosition += cameraDirection * speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
//                cameraDirection.Y = 0;
                cameraPosition -= cameraDirection * speed;
            }

            //  Move side to side
            if (Keyboard.GetState().IsKeyDown(Keys.A))
                cameraPosition += Vector3.Cross(cameraUp, cameraDirection) * speed;
            if (Keyboard.GetState().IsKeyDown(Keys.D))
                cameraPosition -= Vector3.Cross(cameraUp, cameraDirection) * speed;

            //  Yaw rotation
            float yawAngle = (-MathHelper.PiOver4 / 150) *
                (Mouse.GetState().X - prevMouseState.X);

            if (Math.Abs(currentYaw + yawAngle) < totalYaw)
            {
                cameraDirection = Vector3.Transform(cameraDirection,
                    Matrix.CreateFromAxisAngle(cameraUp, yawAngle));
                currentYaw += yawAngle;
            }

            //  Pitch rotation
            float pitchAngle = (MathHelper.PiOver4 / 150) *
                (Mouse.GetState().Y - prevMouseState.Y);

            if (Math.Abs(currentPitch + pitchAngle) < totalPitch)
            {
                cameraDirection = Vector3.Transform(cameraDirection,
                    Matrix.CreateFromAxisAngle(
                        Vector3.Cross(cameraUp, cameraDirection), pitchAngle));

                currentPitch += pitchAngle;
            }

            //  Reset prevMouseState
            prevMouseState = Mouse.GetState();

            //  Recreate the camera view matrix
            CreateLookAt();

            base.Update(gameTime);
        }

        private void CreateLookAt()
        {
            view = Matrix.CreateLookAt(cameraPosition, cameraPosition + cameraDirection, cameraUp);
        }
    }
}




Edit: Very easy to do actually. I was on the right lines, but I needed to call another method with the Y axis set at 0, and then use that for movement rather than the camera :-D

This post has been edited by Quin: 06 March 2013 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1