
FPS camera problem
Posted Monday, 27 August, 2012 - 03:23 by Richy19 inI was trying to achieve a FPS style camera following this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-a... which I have used previously in c++ and it worked flawlessly, however after converting it to c# and getting the following code:
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using OpenTK; using OpenTK.Graphics.OpenGL; using OpenTK.Input; using OpenTKGaem.GLUtilities; namespace OpenTKGaem { public class DirectionalCamera { public static DirectionalCamera dc = new DirectionalCamera (); Matrix4 View; Matrix4 Persp; Vector3 position; Vector3 up = new Vector3 (0.0f, 1.0f, 0.0f); float horizontalAngle = (float)Math.PI; float verticalAngle = 0.0f; float initialFoV = OpenTK.MathHelper.PiOver4; float speed = 3.0f; float mouseSpeed = 0.005f; public DirectionalCamera () { } public void Init () { position = new Vector3(0f ,0.0f, 10.0f); View = Matrix4.LookAt (position, Vector3.Zero, up); Persp = Matrix4.CreatePerspectiveFieldOfView (initialFoV, (float)Settings.width / Settings.height, 0.1f, 100.0f); } public void Update (float dt) { Vector2 hSS = new Vector2 (Settings.width / 2, Settings.height / 2); Vector2 mouseP = new Vector2 (((int)Game.g.Mouse.X), ((int)Game.g.Mouse.Y)); Mouse.SetPosition (Game.g.X + hSS.X ,Game.g.Y + hSS.Y); horizontalAngle += mouseSpeed * (float)dt * (hSS.X - mouseP.X); verticalAngle += mouseSpeed * (float)dt * (hSS.Y - mouseP.Y); Vector3 direction = new Vector3 ( (float)(Math.Cos (verticalAngle) * Math.Sin (horizontalAngle)), (float)(Math.Sin (verticalAngle)), (float)(Math.Cos (verticalAngle) * Math.Cos (horizontalAngle)) ); Vector3 right = new Vector3 ( (float)(Math.Sin (horizontalAngle - OpenTK.MathHelper.PiOver2)), 0.0f, (float)(Math.Cos (horizontalAngle - OpenTK.MathHelper.PiOver2)) ); up = Vector3.Cross (right, direction); Vector3 dts = new Vector3 (dt * speed); if (Keyboard.GetState ().IsKeyDown (Key.W)) { position.X += direction.X * dts.X; position.Y += direction.Y * dts.Y; position.Z += direction.Z * dts.Z; } if (Keyboard.GetState ().IsKeyDown (Key.S)) { position.X -= direction.X * dts.X; position.Y -= direction.Y * dts.Y; position.Z -= direction.Z * dts.Z; } if (Keyboard.GetState ().IsKeyDown (Key.D)) { position.X += right.X * dts.X; position.Y += right.Y * dts.Y; position.Z += right.Z * dts.Z; } if (Keyboard.GetState ().IsKeyDown (Key.A)) { position.X -= right.X * dts.X; position.Y -= right.Y * dts.Y; position.Z -= right.Z * dts.Z; } View = Matrix4.LookAt (position, position + direction, up); } public void getMVP (ref Matrix4 MVP) { MVP = MVP * View * Persp; } public void getVP (out Matrix4 VP) { VP = View * Persp; } } }
It all works but, the lookat keeps moving up
To ilustrate this I have made a video of what happens
http://www.youtube.com/watch?v=1fBiwOEOlek
Dont know if this is a floating point issue or what
It could just be that im using one of the math functions wrong as im not used to C#/OpenTK as I come from C++/OpenGL/GLM

