xna camera movement

another botched XNA tutorial on my part

Page 1 of 1

1 Replies - 3896 Views - Last Post: 22 July 2009 - 01:51 PM Rate Topic: -----

#1 chris772  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

xna camera movement

Post icon  Posted 20 July 2009 - 08:02 PM

I converted some tutorial code to fit a game I made. Everything was working just fine until I started trying to move the camera. The mouse is effectively bound to the center of the window, but the camera won't move or rotate. Any help?

game1.cs
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CameraTesting
{
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;

		Vector3 cameraPos;
		Vector3 cameraTarget;
		Vector3 cameraRef;
		float aspectRatio;
		float nearPlane;
		float farPlane;
		float fieldOfView;
		Matrix projection;
		float cameraYaw;
		float cameraPitch;

		MouseState mState;
		MouseState mStatePrev;
		KeyboardState kbState;
		float deltaTime;
		GameTime gTime = new GameTime();
		Vector3 moveVector = Vector3.Zero;

		gameObject landscape = new gameObject();

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
		}

		protected override void Initialize()
		{
			base.Initialize();

			nearPlane = 1.0f;
			farPlane = 1000.0f;
			fieldOfView = 45.0f;
			cameraPos = new Vector3(0.0f, 0.0f, 0.0f);
			cameraRef = new Vector3(0.0f, 0.0f, 1.0f);
			cameraTarget = cameraRef + cameraPos;
			cameraYaw = 0.0f;
			cameraPitch = 0.0f;
			aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;

			landscape.position = new Vector3(0.0f, -5.0f, 5.0f);

			projection = Matrix.CreatePerspectiveFieldOfView(
				MathHelper.ToRadians(fieldOfView),
				aspectRatio,
				nearPlane,
				farPlane);

			Mouse.SetPosition(
				graphics.GraphicsDevice.Viewport.Width / 2,
				graphics.GraphicsDevice.Viewport.Height / 2);

			mState = Mouse.GetState();
		}

		protected override void LoadContent()
		{
			spriteBatch = new SpriteBatch(GraphicsDevice);

			landscape.model = Content.Load<Model>("landscape1");
		}

		protected override void UnloadContent()
		{
		}

		protected override void Update(GameTime gameTime)
		{
			if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
				this.Exit();

			moveCheck();

			base.Update(gameTime);
		}

		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.CornflowerBlue);

			Matrix[] transforms = new Matrix[landscape.model.Bones.Count];
			landscape.model.CopyAbsoluteBoneTransformsTo(transforms);

			drawGameObject(landscape);

			base.Draw(gameTime);
		}

		//move drawGameObject() to drawGameObject.cs
		public void drawGameObject(gameObject gameObject)
		{
			foreach (ModelMesh mesh in gameObject.model.Meshes)
			{
				foreach (BasicEffect effect in mesh.Effects)
				{
					effect.EnableDefaultLighting();
					effect.PreferPerPixelLighting = true;

					effect.World = Matrix.CreateFromYawPitchRoll(
						gameObject.rotation.Y,
						gameObject.rotation.X,
						gameObject.rotation.Z) *
						Matrix.CreateScale(gameObject.scale) *
						Matrix.CreateTranslation(gameObject.position);

					effect.Projection = projection;
					//effect.View = null;
				}
				mesh.Draw();
			}
		}

		void moveCheck()
		{
			kbState = Keyboard.GetState();
			mStatePrev = mState;
			mState = Mouse.GetState();
			deltaTime = (float)gTime.ElapsedGameTime.TotalSeconds;

			if (kbState.IsKeyDown(Keys.Up))
			{
				moveVector.Z += 3.0f * deltaTime;
			}

			if (kbState.IsKeyDown(Keys.Down))
			{
				moveVector.Z -= 3.0f * deltaTime;
			}

			if (kbState.IsKeyDown(Keys.Left))
			{
				moveVector.X += 3.0f * deltaTime;
			}

			if (kbState.IsKeyDown(Keys.Right))
			{
				moveVector.X -= 3.0f * deltaTime;
			}

			float mouseX = mState.X - mStatePrev.X;
			float mouseY = mState.Y - mStatePrev.Y;
			cameraPitch += (mouseY * 0.4f) * deltaTime;
			cameraYaw -= (mouseX * 0.4f) * deltaTime;
			cameraPitch = MathHelper.Clamp(
				cameraPitch,
				MathHelper.ToRadians(-89.9f),
				MathHelper.ToRadians(89.9f));
			Mouse.SetPosition(graphics.GraphicsDevice.Viewport.Width / 2,
				graphics.GraphicsDevice.Viewport.Height / 2);

			Matrix cameraViewRotationMatrix = Matrix.CreateRotationX(cameraPitch) *
				Matrix.CreateRotationY(cameraYaw);
			Matrix cameraMoveRotationMatrix = Matrix.CreateRotationY(cameraYaw);

			Vector3 transformedCameraReference = Vector3.Transform(
				cameraRef,
				cameraViewRotationMatrix);
			cameraPos += Vector3.Transform(
				moveVector,
				cameraMoveRotationMatrix);
			cameraTarget = transformedCameraReference + cameraPos;
		}
	}
}



gameObject.cs
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CameraTesting
{
	public class gameObject
	{
		public Model model = null;
		public Vector3 position = Vector3.Zero;
		public Vector3 rotation = Vector3.Zero;
		public float scale = 1.0f;
		public Vector3 velocity = Vector3.Zero;
		public bool alive = false;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: xna camera movement

#2 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: xna camera movement

Posted 22 July 2009 - 01:51 PM

When you step through with a debugger, do you have values in cameraPitch and CameraYaw before you start the matrice rotations?


Aet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1