public Trackball() { _transform = new Transform3DGroup(); _transform.Children.Add(_scale); _transform.Children.Add(new RotateTransform3D(_rotation)); _currentOrientation = Quaternion.Identity; _persistentOrientation = Quaternion.Identity; IoC.Get<IPropertyGrid>().SelectedObject = _rotation; }
private void Track(Point currentPosition) { Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); IoC.Get<IOutput>().Append("New trackball position: " + currentPosition3D); // Because our sphere is centered at the origin, we may interpret our points as vectors. Doing so it is trivial // to find the axis and angle of rotation using the cross product dot product respectively. Vector3D axis = Vector3D.Cross(_initialPosition3D, currentPosition3D); float angle = Vector3D.AngleBetween(_initialPosition3D, currentPosition3D); // We negate the angle because we are rotating the camera (if we were rotating the scene, we wouldn't do this). Quaternion delta = new Quaternion(axis, -angle); // Get the initial orientation. Quaternion q = _persistentOrientation; // Compose the delta with the previous orientation q *= delta; // Store the new orientation. _rotation.Axis = q.Axis; _rotation.Angle = q.Angle; _currentOrientation = q; //_previousPosition3D = currentPosition3D; }
public static bool IsNan(Quaternion q) { return float.IsNaN(q.X) || float.IsNaN(q.Y) || float.IsNaN(q.Z) || float.IsNaN(q.W); }
private void OnMouseDown(object sender, MouseEventArgs e) { IoC.Get<IOutput>().Append("Mouse down"); Mouse.Capture(EventSource, CaptureMode.Element); _previousPosition2D = e.GetPosition(EventSource); _initialPosition3D = ProjectToTrackball( EventSource.ActualWidth, EventSource.ActualHeight, _previousPosition2D); _currentOrientation = _persistentOrientation; _mouseDown = true; }
public static Quaternion CreateFromRotationMatrix(Matrix3D matrix) { float num8 = (matrix.M11 + matrix.M22) + matrix.M33; Quaternion quaternion = new Quaternion(); if (num8 > 0f) { float num = (float)Math.Sqrt((double)(num8 + 1f)); quaternion.W = num * 0.5f; num = 0.5f / num; quaternion.X = (matrix.M23 - matrix.M32) * num; quaternion.Y = (matrix.M31 - matrix.M13) * num; quaternion.Z = (matrix.M12 - matrix.M21) * num; return quaternion; } if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33)) { float num7 = (float)Math.Sqrt((double)(((1f + matrix.M11) - matrix.M22) - matrix.M33)); float num4 = 0.5f / num7; quaternion.X = 0.5f * num7; quaternion.Y = (matrix.M12 + matrix.M21) * num4; quaternion.Z = (matrix.M13 + matrix.M31) * num4; quaternion.W = (matrix.M23 - matrix.M32) * num4; return quaternion; } if (matrix.M22 > matrix.M33) { float num6 = (float)Math.Sqrt((double)(((1f + matrix.M22) - matrix.M11) - matrix.M33)); float num3 = 0.5f / num6; quaternion.X = (matrix.M21 + matrix.M12) * num3; quaternion.Y = 0.5f * num6; quaternion.Z = (matrix.M32 + matrix.M23) * num3; quaternion.W = (matrix.M31 - matrix.M13) * num3; return quaternion; } float num5 = (float)Math.Sqrt((double)(((1f + matrix.M33) - matrix.M11) - matrix.M22)); float num2 = 0.5f / num5; quaternion.X = (matrix.M31 + matrix.M13) * num2; quaternion.Y = (matrix.M32 + matrix.M23) * num2; quaternion.Z = 0.5f * num5; quaternion.W = (matrix.M12 - matrix.M21) * num2; return quaternion; }