I have a free camera in my game. My problem is the camera moves too fast when I move the mouse. Now if I slow down the camera the pitch and yaw in degrees also slow down.
I want to slow down my camera movement but in the same time I don't want the Pitch and Yaw to slow down.
If you look at the video the pitch and yaw are very slow, I can speed them up by increasing the mouse sensitivity variable but then the camera moves very fast.
I just want to make a free camera that locks at 90 and -90 degrees when I move up and down.
Sorry I’m not making a lot of sense but I don't know how to explain it.
I already made a free camera in OpenGL and it works but I don't know what I’m doing wrong in XNA.
XNA free camera problem
[media]
[/media]
OpenGL free camera locks at 90 and -90 degrees
[media]
[/media]
code FreeCamera class
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 XNA_4._0_Tutorial
{
class FreeCamera : Camera
{
public float Pitch { get; set; }
public float Yaw { get; set; }
public Vector3 Position { get; set; }
public Vector3 Target { get; set; }
private Vector3 Translation;
public FreeCamera(Vector3 Position, GraphicsDevice graphicsDevice)
: base(graphicsDevice)
{
this.Position = Position;
}
public void Lock()
{
if(Pitch > 90.0f)
Pitch = 90.0f;
if(Pitch < -90.0f)
Pitch = -90.0f;
if(Yaw > 360.0f)
Yaw -=360.0f;
if(Yaw < 0.0f)
Yaw +=360.0f;
}
public void Rotate(float YawOffset, float PitchOffset)
{
this.Yaw += MathHelper.ToDegrees(YawOffset);
this.Pitch += MathHelper.ToDegrees(PitchOffset);
}
public void Move(Vector3 Translation)
{
this.Translation += Translation;
}
public void Control(GraphicsDeviceManager graphics, GameTime gameTime, float Velocity, float MouseSensitivity)
{
MouseState mouseState = Mouse.GetState();
KeyboardState keyState = Keyboard.GetState();
int ScreenX = graphics.PreferredBackBufferWidth / 2;
int ScreenY = graphics.PreferredBackBufferHeight / 2;
Lock();
// Determine how much the camera should turn
float deltaX = MouseSensitivity * (ScreenX - (float)mouseState.X);
float deltaY = MouseSensitivity * (ScreenY - (float)mouseState.Y);
Mouse.SetPosition(ScreenX, ScreenY);
// Rotate the camera
Rotate(deltaX, deltaY);
Vector3 translation = Vector3.Zero;
// Determine in which direction to move the camera
if (keyState.IsKeyDown(Keys.W)) translation += Vector3.Forward;
if (keyState.IsKeyDown(Keys.S)) translation += Vector3.Backward;
if (keyState.IsKeyDown(Keys.A)) translation += Vector3.Left;
if (keyState.IsKeyDown(Keys.D)) translation += Vector3.Right;
// Move 10 units per millisecond, independent of frame rate
translation *= Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
// Move the camera
Move(translation);
// Update the camera
Update();
}
public override void Update()
{
// Calculate the rotation matrix
Matrix Rrotation = Matrix.CreateFromYawPitchRoll(Yaw, Pitch, 0);
// Offset the position and reset the translation
Translation = Vector3.Transform(Translation, Rrotation);
Position += Translation;
Translation = Vector3.Zero;
// Calculate the new target
Vector3 forward = Vector3.Transform(Vector3.Forward, Rrotation);
Target = Position + forward;
// Calculate the up vector
Vector3 up = Vector3.Transform(Vector3.Up, Rrotation);
// Calculate the view matrix
View = Matrix.CreateLookAt(Position, Target, up);
}
}
}