/// <inheritdoc /> public void Update(TimeSpan elapsed) { //// Pan up - rotate forward and up around their cross product if (view.IsKeyDown(Keys.W)) { latitude += RotationSpeed * (float)elapsed.TotalSeconds; latitude = (float)Math.Min(latitude, Math.PI / 2f); } //// Pan down - rotate forward and up around their cross product if (view.IsKeyDown(Keys.S)) { latitude -= RotationSpeed * (float)elapsed.TotalSeconds; latitude = (float)Math.Max(latitude, -Math.PI / 2f); } //// Pan right - rotate forward around up if (view.IsKeyDown(Keys.D)) { longitude += RotationSpeed * (float)elapsed.TotalSeconds; longitude = longitude % (float)(2f * Math.PI); } //// Pan left - rotate forward around up if (view.IsKeyDown(Keys.A)) { longitude -= RotationSpeed * (float)elapsed.TotalSeconds; longitude = longitude % (float)(2f * Math.PI); } //// Zoom zoomLevel += (int)view.MouseState.ScrollDelta.Y; // Projection matrix Projection = Matrix4.CreatePerspectiveFieldOfView( FieldOfViewRadians, view.AspectRatio, NearPlaneDistance, FarPlaneDistance); // Camera matrix var longitudeRot = Quaternion.FromAxisAngle(Vector3.UnitY, longitude); var x = Vector3.Transform(Vector3.UnitX, longitudeRot); var latitudeRot = Quaternion.FromAxisAngle(Vector3.Cross(x, Vector3.UnitY), latitude); this.forward = -Vector3.Transform(x, latitudeRot); this.up = Vector3.Transform(Vector3.UnitY, latitudeRot); View = Matrix4.LookAt(Position, Vector3.Zero, up); }
/// <inheritdoc /> public void Update(TimeSpan elapsed) { //// Pan up - rotate forward and up around their cross product if (view.IsKeyDown(Keys.W)) { var t = Quaternion.FromAxisAngle(Vector3.Cross(forward, up), -RotationSpeed); forward = Vector3.Transform(forward, t); up = Vector3.Transform(up, t); } //// Pan down - rotate forward and up around their cross product if (view.IsKeyDown(Keys.S)) { var t = Quaternion.FromAxisAngle(Vector3.Cross(forward, up), RotationSpeed); forward = Vector3.Normalize(Vector3.Transform(forward, t)); up = Vector3.Normalize(Vector3.Transform(up, t)); } //// Pan right - rotate forward around up if (view.IsKeyDown(Keys.D)) { forward = Vector3.Normalize(Vector3.Transform(forward, Quaternion.FromAxisAngle(up, RotationSpeed))); } //// Pan left - rotate forward around up if (view.IsKeyDown(Keys.A)) { forward = Vector3.Normalize(Vector3.Transform(forward, Quaternion.FromAxisAngle(up, -RotationSpeed))); } //// Roll right - rotate up around forward if (view.IsKeyDown(Keys.Q)) { up = Vector3.Normalize(Vector3.Transform(up, Quaternion.FromAxisAngle(forward, -RollSpeed))); } //// Roll left - rotate up around forward if (view.IsKeyDown(Keys.E)) { up = Vector3.Normalize(Vector3.Transform(up, Quaternion.FromAxisAngle(forward, RollSpeed))); } //// Zoom zoomLevel += (int)view.MouseState.ScrollDelta.Y; // Projection matrix Projection = Matrix4.CreatePerspectiveFieldOfView( FieldOfViewRadians, view.AspectRatio, NearPlaneDistance, FarPlaneDistance); // Camera matrix View = Matrix4.LookAt(Position, Vector3.Zero, up); }
} // = 100f; /// <inheritdoc /> public void Update(TimeSpan elapsed) { if (view.LockCursor) { // Compute new orientation var xDiff = view.CenterOffset.X; xDiff = Math.Abs(xDiff) < 2 ? 0 : xDiff; horizontalAngle -= RotationSpeed * xDiff; var yDiff = view.CenterOffset.Y; yDiff = Math.Abs(yDiff) < 2 ? 0 : yDiff; verticalAngle -= RotationSpeed * yDiff; verticalAngle = Math.Max(-(float)Math.PI / 2, Math.Min(verticalAngle, (float)Math.PI / 2)); } // Direction: Spherical coordinates to Cartesian coordinates conversion var direction = new Vector3( (float)(Math.Cos(verticalAngle) * Math.Sin(horizontalAngle)), (float)Math.Sin(verticalAngle), (float)(Math.Cos(verticalAngle) * Math.Cos(horizontalAngle))); // Right vector var right = new Vector3( (float)Math.Sin(horizontalAngle - 3.14f / 2.0f), 0, (float)Math.Cos(horizontalAngle - 3.14f / 2.0f)); // Up vector var up = Vector3.Cross(right, direction); //// Move forward if (view.IsKeyDown(Keys.W)) { Position += direction * (float)elapsed.TotalSeconds * MovementSpeed; } //// Move backward if (view.IsKeyDown(Keys.S)) { Position -= direction * (float)elapsed.TotalSeconds * MovementSpeed; } //// Strafe right if (view.IsKeyDown(Keys.D)) { Position += right * (float)elapsed.TotalSeconds * MovementSpeed; } //// Strafe left if (view.IsKeyDown(Keys.A)) { Position -= right * (float)elapsed.TotalSeconds * MovementSpeed; } Projection = Matrix4.CreatePerspectiveFieldOfView( FieldOfViewRadians, view.AspectRatio, NearPlaneDistance, FarPlaneDistance); // Camera matrix View = Matrix4.LookAt( Position, // Camera is here Position + direction, // and looks here : at the same position, plus "direction" up); // Head is up (set to 0,-1,0 to look upside-down) }