5 Replies - 1081 Views - Last Post: 16 September 2011 - 08:02 AM

#1 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Matrix Math -- How to rotate first person view up and down

Posted 16 September 2011 - 01:40 AM

I'm learning about 3D programming using XNA, and I've been trying to create a first person view. I'm rendering a cube with multicoloured walls around the camera to identify if the views are working. Here's my code:
namespace FPS.GameComponents
{
    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        Game1 game;

        public Matrix view;
        public Matrix projection;

        Vector3 to;
        Vector3 position;

        int choice = 0;

        public Camera(Game game)
            : base(game)
        {
            this.game = game as Game1;

            position = new Vector3(0, 0, 0);
            to = new Vector3(0, 10, 0);

            view = Matrix.CreateLookAt(position, to, new Vector3(0, 0, 1));
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, game.GraphicsDevice.Viewport.AspectRatio, 0.2f, 500);
        }
        public override void Update(GameTime gameTime)
        {
            if (Input.State.IsKeyDown(Keys.W))
            {
                to = Vector3.Transform(to, Matrix.CreateRotationX(MathHelper.ToRadians(1)));
            }
            if (Input.State.IsKeyDown(Keys.S))
            {
                to = Vector3.Transform(to, Matrix.CreateRotationX(MathHelper.ToRadians(-1)));
            }
            if (Input.State.IsKeyDown(Keys.A))
            {
                to = Vector3.Transform(to, Matrix.CreateRotationZ(MathHelper.ToRadians(1)));
            }
            if (Input.State.IsKeyDown(Keys.D))
            {
                to = Vector3.Transform(to, Matrix.CreateRotationZ(MathHelper.ToRadians(-1)));
            }
            view = Matrix.CreateLookAt(position, to, new Vector3(0, 0, 1));
            base.Update(gameTime);
        }
    }
}
I discovered by experiment that by creating a Z rotation I could rotate my view left or right. I also discovered that applying a Y rotation to my cube rotated it up or down. However, trying to apply the Y rotation to my look vector fails miserably. It does nothing at all until I rotate my view left and right, and once I do it seems to be rotating the view in a circle.

I semi-fixed the problem by changing it to X rotation. This works to move the view up and down, but once I move it left or right it still has the problem where the camera's to vector just rotates in a circle on the screen:
Posted Image

This post has been edited by PlasticineGuy: 16 September 2011 - 02:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Matrix Math -- How to rotate first person view up and down

#2 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Matrix Math -- How to rotate first person view up and down

Posted 16 September 2011 - 03:01 AM

I think you've got the rotations mixed up, the X Axis rotation (pitch) should go Up and Down, Y axis rotation (yaw) should be left to right and Z axis (roll) should be twisting the screen in a circle.

3D Axis Rotations

This post has been edited by ShadowsEdge19: 16 September 2011 - 03:01 AM

Was This Post Helpful? 1
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Matrix Math -- How to rotate first person view up and down

Posted 16 September 2011 - 04:31 AM

Thanks for the link, it helped heaps.

I ultimately solved all my problems by realising that somewhere in my code how I envisioned the y axis turned out to be the z axis. I planned to have x left/right, y forward/back and z up/down, but it turns out y and z had to be swapped. Not sure what that's about.
namespace FPS.GameComponents
{
    public class Camera : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game1 game;

        public Matrix view;
        public Matrix projection;

        Vector3 to;
        Vector3 position;

        float xzRot = 0;
        float yRot = 0;
        const float rotSpeed = 0.01f;
        const float moveSpeed = 3f;


        public Camera(Game game)
            : base(game)
        {
            this.game = game as Game1;

            // position camera at middle of level
            Level level = this.game.level;
            position = new Vector3(level.Width * Level.tileSize / 2, 32, level.Height * Level.tileSize / 2);
            
            // look straight forward
            to = position + new Vector3(0, 0, 10);
            yRot = MathHelper.PiOver2;

            view = Matrix.CreateLookAt(position, to, new Vector3(0, 0, 1));
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, game.GraphicsDevice.Viewport.AspectRatio, 0.2f, 1000);
        }

        public override void Initialize()
        {
            base.Initialize();
        }            

        public override void Update(GameTime gameTime)
        {
            Vector3 move = new Vector3(0, 0, 0);
            if (Input.State.IsKeyDown(Keys.W))
            {
                move += new Vector3(0, 0, 1);
            }
            if (Input.State.IsKeyDown(Keys.S))
            {
                move += new Vector3(0, 0, -1);
            }
            if (Input.State.IsKeyDown(Keys.A))
            {
                move += new Vector3(1, 0, 0);
            }
            if (Input.State.IsKeyDown(Keys.D))
            {
                move += new Vector3(-1, 0, 0);
            }

            if (Input.State.IsKeyDown(Keys.Escape))
            {
                game.Exit();
            }

            xzRot -= rotSpeed * (Input.MouseState.X - Input.MouseStart.X);
            yRot += rotSpeed * (Input.MouseState.Y - Input.MouseStart.Y);
            if (yRot > MathHelper.Pi)
            {
                yRot = MathHelper.Pi;
            }
            if (yRot < 0)
            {
                yRot = 0;
            }

            position += moveSpeed * Vector3.Transform(move, Matrix.CreateRotationY(xzRot));

            Matrix rot = Matrix.CreateRotationX(yRot) * Matrix.CreateRotationY(xzRot);
            to = position + Vector3.Transform(new Vector3(0, 10, 0), rot);
            Vector3 up = Vector3.Transform(new Vector3(0, 0, -1), rot);

            view = Matrix.CreateLookAt(position, to, up);
            base.Update(gameTime);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Matrix Math -- How to rotate first person view up and down

Posted 16 September 2011 - 05:42 AM

I know you want to use axies to perform in a different manner but what will be gained from it? You can't change how a X rotation will act, calling Vector3(0, -1, 0) will still always be a reversed Y-axis and Vector3(1, 0, 0) will always be a normal X-Axis.

So your input should look like this:
            if (Input.State.IsKeyDown(Keys.W))
            {
                move += new Vector3(1, 0, 0);
            }
            if (Input.State.IsKeyDown(Keys.S))
            {
                move += new Vector3(-1, 0, 0);
            }
            if (Input.State.IsKeyDown(Keys.A))
            {
                move += new Vector3(0, 1, 0);
            }
            if (Input.State.IsKeyDown(Keys.D))
            {
                move += new Vector3(0, -1, 0);
            }


Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Matrix Math -- How to rotate first person view up and down

Posted 16 September 2011 - 05:47 AM

Doing that simply causes the W and S keys to move left and right, and A and D to move up and down.

I have previous experience in a platform where +x was right, +y was backward and +z was upwards.

This post has been edited by PlasticineGuy: 16 September 2011 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Matrix Math -- How to rotate first person view up and down

Posted 16 September 2011 - 08:02 AM

Try checking this out:

Mouse Camera

I think this is what you're talking about...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1