What's wrong with my camera? It rotates down instead of up, when I'm facing an opposite direction

Started by
1 comment, last by iedoc 12 years ago
Hi there, my camera is acting really weird.
It pitches differently when I face a different direction (down instead of up).

Also I couldn't find any decent first-person camera tutorials on the web.
I just need a simple camera...
It's really frustrating for the beginner.

Any help will be appreciated!!!

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;
namespace MyGame {

public class Camera : Microsoft.Xna.Framework.GameComponent {

public Matrix view { get; protected set; }
public Matrix projection { get; protected set; }
public Vector3 pos;
public Vector3 dir;
Vector3 up;
float speed = 1.5f;
float gravity = 0;
float jump = 0;
public Model model;
private float pitch;
MouseState prevMouseState;

// -------------------------------------

public Camera(Game game, Vector3 iPos, Vector3 target, Vector3 iUp)
: base(game) {

pos = iPos;
dir = target - iPos;
dir.Normalize();
up = iUp;
createLookAt();
pitch = 0;
float aspectRatio = (float)Game.Window.ClientBounds.Width /
(float)Game.Window.ClientBounds.Height;

// 45 degrees
float fieldOfView = MathHelper.PiOver4;
projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, 0.1f, 3000);
model = Game.Content.Load<Model>("Models/bublik");
}

// -------------------------------------

public override void Initialize() {
Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
prevMouseState = Mouse.GetState();
base.Initialize();
}

// -------------------------------------

public override void Update(GameTime gameTime) {
KeyboardState ks = Keyboard.GetState();
MouseState ms = Mouse.GetState();

// moving
if (ks.IsKeyDown(Keys.W) && ((Game1)Game).canMove) {
// making sure the player only walks forward (restricting Y direction)
float y = dir.Y;
dir.Y = 0;
pos += dir * speed;
dir.Y = y;
}

if (ks.IsKeyDown(Keys.S)) {
float y = dir.Y;
dir.Y = 0;
pos -= dir * speed;
dir.Y = y;
}

if (ks.IsKeyDown(Keys.A))
pos += Vector3.Cross(up, dir) * speed;

if (ks.IsKeyDown(Keys.D))
pos -= Vector3.Cross(up, dir) * speed;

// jump
if (ks.IsKeyDown(Keys.Space) && pos.Y <= 10.1f)
jump = 1.2f;

// yaw and pitch
dir = Vector3.Transform(dir, Matrix.CreateFromYawPitchRoll (
(-MathHelper.PiOver4 / 400) * (Mouse.GetState().X - prevMouseState.X),
(MathHelper.PiOver4 / 400) * (Mouse.GetState().Y - prevMouseState.Y),
0) ) ;

dir.Normalize();

Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
prevMouseState = Mouse.GetState();

createLookAt();

// basic physics
pos.Y -= gravity;

if (pos.Y > 10)
gravity += 0.02f;
else if (pos.Y == 10)
gravity = 0;
else if (pos.Y < 10) {
pos.Y = 10;
gravity = 0;
jump = 0;
}

if (jump > 0) {
pos.Y += jump;
jump -= 0.03f;
}

((Game1)Game).player.position = pos + dir * 10;

base.Update(gameTime);
}

// -------------------------------------

private void createLookAt() {
view = Matrix.CreateLookAt(pos, pos + dir, up);
}
}
}
Advertisement
You have to calculate the up vector accordantly. But if you want to avoid that you can use quaternions. Moreover, the quaternions solve the pole problem (when the rotation matrix is undefined).

Good luck!!

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>
you should keep the cameras forward and right vectors. the forward is basically the direction your camera moves when you press the up arrow key (or w), and the right vector is the way the camera moves when you press the right arrow key (or d)

then just cross the forward and right vectors to get the up vector.

here's one on how to make a first person camera:
http://www.braynzarsoft.net/index.php?p=D3D11FPC

This topic is closed to new replies.

Advertisement