/// <summary> /// Used to find which primitive object the mouse is currently hold over for selecting that object /// </summary> /// <param name="mousePosition">the mouses location</param> /// <param name="shape">the primitive that you wish to see if the mouse is currently hovering over</param> /// <param name="distance">the distance from the mouse to the shape</param> /// <returns>true is mouse is over that primitive; false otherwise</returns> public bool RayCalculation(Vector2 mousePosition, MeshClass mesh) { var mouseNear = new Vector3(mousePosition, 0.0f); var mouseFar = new Vector3(mousePosition, 1.0f); var mat = this.View * this.projection * DeviceManager.LocalDevice.GetTransform(TransformState.World); Vector3.Unproject(ref mouseNear, DeviceManager.LocalDevice.Viewport.X, DeviceManager.LocalDevice.Viewport.Y, DeviceManager.LocalDevice.Viewport.Width, DeviceManager.LocalDevice.Viewport.Height, 0f, 1f, ref mat, out mouseNear); Vector3.Unproject(ref mouseFar, DeviceManager.LocalDevice.Viewport.X, DeviceManager.LocalDevice.Viewport.Y, DeviceManager.LocalDevice.Viewport.Width, DeviceManager.LocalDevice.Viewport.Height, 0f, 1f, ref mat, out mouseFar); var direction = mouseFar - mouseNear; var selectionRay = new Ray(mouseNear, direction); return(mesh.ObjectMesh.Intersects(selectionRay)); }
/// <summary> /// Used to find which primitive object the mouse is currently hold over for selecting that object /// </summary> /// <param name="mousePosition">the mouses location</param> /// <param name="shape">the primitive that you wish to see if the mouse is currently hovering over</param> /// <param name="distance">the distance from the mouse to the shape</param> /// <returns>true is mouse is over that primitive; false otherwise</returns> public bool RayCalculation(Vector2 mousePosition, MeshClass mesh) { var mouseNear = new Vector3(mousePosition, 0.0f); var mouseFar = new Vector3(mousePosition, 1.0f); var mat = this.View * this.projection * DeviceManager.LocalDevice.GetTransform(TransformState.World); Vector3.Unproject(ref mouseNear, DeviceManager.LocalDevice.Viewport.X, DeviceManager.LocalDevice.Viewport.Y, DeviceManager.LocalDevice.Viewport.Width, DeviceManager.LocalDevice.Viewport.Height, 0f, 1f, ref mat, out mouseNear); Vector3.Unproject(ref mouseFar, DeviceManager.LocalDevice.Viewport.X, DeviceManager.LocalDevice.Viewport.Y, DeviceManager.LocalDevice.Viewport.Width, DeviceManager.LocalDevice.Viewport.Height, 0f, 1f, ref mat, out mouseFar); var direction = mouseFar - mouseNear; var selectionRay = new Ray(mouseNear, direction); return mesh.ObjectMesh.Intersects(selectionRay); }