public void ControlCamera(float dt, InputManager input, Viewport vp) { if(input.Keyboard.IsKeyJustPressed(Keys.LeftControl) || input.Keyboard.IsKeyJustPressed(Keys.RightControl)) { if(input.Mouse.IsBound) input.Mouse.Unbind(); else input.Mouse.Bind(vp.Width / 2, vp.Height / 2); return; } if(!input.Mouse.IsBound) return; Point p = input.Mouse.Displacement; Vector3I moves = Vector3I.Zero; moves.X += input.Keyboard.Current.IsKeyDown(Keys.D) ? 1 : 0; moves.X -= input.Keyboard.Current.IsKeyDown(Keys.A) ? 1 : 0; moves.Z += input.Keyboard.Current.IsKeyDown(Keys.W) ? 1 : 0; moves.Z -= input.Keyboard.Current.IsKeyDown(Keys.S) ? 1 : 0; moves.Y += input.Keyboard.Current.IsKeyDown(Keys.Space) ? 1 : 0; moves.Y -= input.Keyboard.Current.IsKeyDown(Keys.LeftShift) ? 1 : 0; Yaw += -p.X * YAW_SPEED * dt; Pitch += -p.Y * PITCH_SPEED * dt; mWorld = Matrix.CreateFromYawPitchRoll(Yaw, Pitch, 0) * Matrix.CreateTranslation( moves.X * mWorld.Right + moves.Y * mWorld.Up + moves.Z * mWorld.Forward + mWorld.Translation ); }
public void ControlCamera(float dt, InputManager input, Viewport vp) { Point p = input.Mouse.Displacement; if(input.Keyboard.Current.IsKeyDown(Keys.LeftControl)) { if(input.Mouse.Current.MiddleButton == ButtonState.Pressed) { // Zoom By FOV if(p.Y != 0) FOV *= (float)Math.Pow(FOV_ZOOM_SPEED, dt * p.Y); } } else if(input.Keyboard.Current.IsKeyDown(Keys.LeftShift)) { if(input.Mouse.Current.MiddleButton == ButtonState.Pressed) { // Pan Vector3 eye = Eye; Ray r1 = new Ray(eye, vp.Unproject( new Vector3(input.Mouse.Previous.X, input.Mouse.Previous.Y, 1), Projection, View, Matrix.Identity )); Ray r2 = new Ray(eye, vp.Unproject( new Vector3(input.Mouse.Current.X, input.Mouse.Current.Y, 1), Projection, View, Matrix.Identity )); Matrix mVI = Matrix.Invert(mView); Plane hit = new Plane(Center, Center + mVI.Up, Center + mVI.Right); //new Plane(Vector3.Normalize(eye - Center), 0f); r1.Direction -= r1.Position; r1.Direction.Normalize(); float d1 = r1.Intersects(hit).Value; Vector3 p1 = r1.Position + d1 * r1.Direction; r2.Direction -= r2.Position; r2.Direction.Normalize(); float d2 = r2.Intersects(hit).Value; Vector3 p2 = r2.Position + d2 * r2.Direction; Center += p1 - p2; } } else if(input.Mouse.Current.MiddleButton == ButtonState.Pressed) { // Yaw if(p.X != 0) Yaw -= YAW_SPEED * (p.X * dt); // Pitch if(p.Y != 0) Pitch += PITCH_SPEED * (p.Y * dt); } // Zoom By Distance if(input.Mouse.ScrollDisplacement != 0) { float amt = (input.Mouse.ScrollDisplacement / 60) * dt; Distance = MathHelper.Clamp(Distance * (float)Math.Pow(DIST_ZOOM_SPEED, amt), 0.1f, 1000f); } if(input.Keyboard.IsKeyJustPressed(Keys.R)) { // Reset The Camera Center = Vector3.Zero; Distance = 4f; Pitch = MathHelper.PiOver4; Yaw = 0; FOV = MathHelper.PiOver4; } }