Jump to content
  • Advertisement
Sign in to follow this  
ole_92

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

This topic is 2295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}
}
}

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!