private void OnCameraRotateStarted(object sender, EventArgs e) { var mousePosition = Mouse.GetPosition(ViewportBorder); Point3D rotatedPosition; // Hit position or position on horizontal plane var hitTestResult = VisualTreeHelper.HitTest(MainViewport, mousePosition) as RayMeshGeometry3DHitTestResult; if (hitTestResult != null) { rotatedPosition = hitTestResult.PointHit; } else { // Get intersection of ray created from mouse position and the horizontal plane (position: 0,0,0; normal: 0,1,0) bool hasIntersection = Camera1.GetMousePositionOnPlane(mousePosition, Constants.ZeroPoint3D, Constants.YAxis, out rotatedPosition); if (!hasIntersection) { return; } } PositionCross.Position = rotatedPosition; PositionCross.IsVisible = true; ShowCameraRotationHelpers(); }
private void UpdateSpherePosition(MouseEventArgs e) { Point mousePosition = e.GetPosition(MainViewport); MousePositionValueTextBlock.Text = string.Format("{0:0}", mousePosition); Point3D intersectionPoint; // Get intersection of ray created from mouse position and the current plane bool hasIntersection = Camera1.GetMousePositionOnPlane(mousePosition, _pointOnPlane, _planeNormal, out intersectionPoint); // The GetMousePositionOnPlane uses the CreateMouseRay3D that creates a ray from a current camera and mouse position. // You can also use that method file the following code: //Point3D rayOrigin; //Vector3D rayDirection; //// Calculate the 3D ray that goes from the mouse position into the 3D scene //bool success = Camera1.CreateMouseRay3D(mousePosition, out rayOrigin, out rayDirection); if (hasIntersection) { double planeLimits = PlaneVisual.Size.Width / 2; // We limit the area where we can position the sphere to the area defined by PlaneVisual if (Math.Abs(intersectionPoint.Z) > planeLimits || Math.Abs(intersectionPoint.X) > planeLimits) { PlanePositionValueTextBlock.Text = "(out of bounds)"; Sphere1.IsVisible = false; } else { // Position the sphere // NOTE: // We are using transform to position the sphere // This is much more efficient than changing the Sphere's CenterPosition - this would recreate the whole geometry SphereTransform.OffsetX = intersectionPoint.X; SphereTransform.OffsetY = intersectionPoint.Y; SphereTransform.OffsetZ = intersectionPoint.Z; Sphere1.IsVisible = true; PlanePositionValueTextBlock.Text = string.Format("{0:0}", intersectionPoint); } } else { Sphere1.IsVisible = false; PlanePositionValueTextBlock.Text = "(no intersection)"; } }