Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:23:11+00:00 2026-05-31T01:23:11+00:00

So, I’m making a 3rd person game in XNA. I’ve created a camera that

  • 0

So, I’m making a 3rd person game in XNA. I’ve created a camera that can pitch within constraints and should be able to yaw the full 360.

The class is seen below:

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 Assignment_3_Redesign
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        //Constrain where our camera can wander in the X and Z plane
        public Rectangle XZBoundingBox;

        //Angular bounds for camera.
        float minPitchIncr = -12;
        float maxPitchIncr = 38;
        float currentPitchIncr = 12;
        float currentYawAngle = 0;

        //Matrices to pass to effects.
        public Matrix view { get; protected set; }
        public Matrix projection { get; protected set; }

        //Important directional vars.
        public Vector3 pos;
        public Vector3 look;
        private Vector3 up;

        //Mouse Input 
        //X -- yaw; Y-- pitch
        private MouseState prevMouseState;
        private MouseState tempMS;
        private KeyboardState keyboardState;

        //Speed to move camera at for translates.
        float Speed = 3;

        public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up, Rectangle bounding)
            : base(game)
        {
            this.pos = pos;
            this.look = (target - pos);
            this.look.Normalize();
            this.up = up;
            this.view = Matrix.CreateLookAt(pos, target, up);

            XZBoundingBox = bounding;

            this.projection = Matrix.CreatePerspectiveFieldOfView
                (MathHelper.PiOver4,
                (float)Game.Window.ClientBounds.Width /
                (float)Game.Window.ClientBounds.Height,
                1, 500);
        }

        private void CreateLookAt()
        { view = Matrix.CreateLookAt(pos, pos + look, up); }

        //Returns current camera direction as vector.
        public Vector3 GetCameraDirection
        { get { return look; } }

        /// 
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            Mouse.SetPosition(Game.Window.ClientBounds.Width / 2,
                Game.Window.ClientBounds.Height / 2);
            prevMouseState = Mouse.GetState();

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Vector3 tempPos= new Vector3(0.0f,0.0f,0.0f);

            //Get keyboard commands
            keyboardState = Keyboard.GetState();

            /*
            //Select object
            if (keyboard.IsKeyDown(Keys.D1))
                objSelected = 0;
            if (keyboard.IsKeyDown(Keys.D2))
                objSelected = 1;*/
            //Translation
            if (keyboardState.IsKeyDown(Keys.A)) //strafe +X (rel)
            {
                tempPos -= Vector3.Cross(up, look) * Speed;
                tempPos.Y = 0.0f;
                if (XZBoundingBox.Contains((int)(pos - Vector3.Normalize(tempPos) * Speed).X, 
                    (int)(pos - Vector3.Normalize(tempPos) * Speed).Z))
                    pos -= Vector3.Normalize(tempPos) * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.D)) //strafe -X (rel)
            {
                tempPos -= Vector3.Cross(up, look) * Speed;
                tempPos.Y = 0.0f;
                if (XZBoundingBox.Contains((int)(pos - Vector3.Normalize(tempPos) * Speed).X,
                    (int)(pos - Vector3.Normalize(tempPos) * Speed).Z))
                    pos += Vector3.Normalize(tempPos) * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.W)) //+Z (rel)
            {
                tempPos += look * Speed;
                tempPos.Y = 0.0f;
                if (XZBoundingBox.Contains((int)(pos - Vector3.Normalize(tempPos) * Speed).X,
                    (int)(pos - Vector3.Normalize(tempPos) * Speed).Z))
                    pos += Vector3.Normalize(tempPos) * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.S)) //-Z (rel)
            {
                tempPos -= look;
                tempPos.Y = 0.0f;
                if (XZBoundingBox.Contains((int)(pos - Vector3.Normalize(tempPos) * Speed).X,
                    (int)(pos - Vector3.Normalize(tempPos) * Speed).Z))
                    pos -= Vector3.Normalize(tempPos) * Speed;
            }

            //Get mouse commands to control camera direction;
            //Yaw rot.
            tempMS = Mouse.GetState();
            if (tempMS != prevMouseState)
            {
                currentYawAngle += (tempMS.X - prevMouseState.X) * (-MathHelper.PiOver4 / 150);
                currentYawAngle = MathHelper.WrapAngle(currentYawAngle);

                if (currentPitchIncr + (tempMS.Y - prevMouseState.Y) < maxPitchIncr &&
                    currentPitchIncr + (tempMS.Y - prevMouseState.Y) > minPitchIncr)
                    currentPitchIncr += tempMS.Y - prevMouseState.Y;
                if (currentPitchIncr > maxPitchIncr) currentPitchIncr = maxPitchIncr;
                else if (currentPitchIncr < minPitchIncr) currentPitchIncr = minPitchIncr;

                look = Vector3.Transform(Vector3.Forward,
                    Matrix.CreateFromYawPitchRoll(currentYawAngle,
                    currentPitchIncr * (-MathHelper.PiOver4 / 150), 0.0f));
            }
            //reset prevMouseState
            prevMouseState = Mouse.GetState();

            base.Update(gameTime);

            CreateLookAt();
        }

    }
}

Now this does what I want … almost. The problem is that when I’m rotating, it won’t go past Pi/-Pi in the yaw. In other words, you can’t do a full 360 spin.

Am I making a mistake in some conditional? (I wouldn’t doubt it… I’ve been staring at it for far too long, playing with it and breaking things.)

Here’s an example declaration:

   camera = new Camera(this, new Vector3(0, 20, 40), Vector3.Zero, Vector3.Up,
                   new Rectangle(-800,-800,1600,1600));

Also any thoughts on the camera class as a whole?

Thanks for the help!

EDIT 1
This is technically for a #classproject. Wanted to mention that, for clarity’s sake. The assignment is open ended and I’m trying to put more time into it because I want to make a solid framework to make WinPhone games…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:23:12+00:00Added an answer on May 31, 2026 at 1:23 am

    You can always ‘reset’ the mouse cursor back to the middle of your screen. This way you can have unlimited motion

    When using this technique, make sure you hide the original mousepointer, since it looks very bad. If you still need a mousepointer to be visible, just blit your own mousepointer on the mouse coordinates you track yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.