|
| 1 | +--- |
| 2 | +title: Selecting an Object with a Mouse |
| 3 | +description: Demonstrates how to check whether the mouse is positioned over a 3D object by creating a ray starting at the camera's near clipping plane and ending at its far clipping plane. |
| 4 | +requireMSLicense: true |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +A common requirement in game development is to select 3D objects in a scene. This example walks through the most common method for achieving this. |
| 10 | + |
| 11 | +> [!NOTE] |
| 12 | +> This example applies only to Desktop or mobile development. The [Mouse](xref:Microsoft.Xna.Framework.Input.Mouse) and [MouseState](xref:Microsoft.Xna.Framework.Input.MouseState) objects are not supported on consoles. |
| 13 | +> |
| 14 | +> On consoles however the same pattern applies, you simply need to use either a fixed point (where the camera is looking, or screen center) or a cursor controlled by a stick to get the position. |
| 15 | +
|
| 16 | +## Detecting Whether a User Clicked a 3D Object |
| 17 | + |
| 18 | +1. Get the current state of the mouse by using [GetState](xref:Microsoft.Xna.Framework.Input.Mouse). |
| 19 | + |
| 20 | + ``` csharp |
| 21 | + MouseState mouseState = Mouse.GetState(); |
| 22 | + ``` |
| 23 | + |
| 24 | +2. Get the current screen coordinates of the mouse from [X](xref:Microsoft.Xna.Framework.Input.Mouse) and [Y](xref:Microsoft.Xna.Framework.Input.Mouse). |
| 25 | + |
| 26 | + ``` csharp |
| 27 | + int mouseX = mouseState.X; |
| 28 | + int mouseY = mouseState.Y; |
| 29 | + ``` |
| 30 | + |
| 31 | + > [!NOTE] |
| 32 | + > Replace mouse position with a virtual cursor or the screen centre for consoles. |
| 33 | + |
| 34 | +3. Using [Viewport.Unproject](xref:Microsoft.Xna.Framework.Graphics.Viewport#Microsoft_Xna_Framework_Graphics_Viewport_Unproject_Microsoft_Xna_Framework_Vector3_Microsoft_Xna_Framework_Matrix_Microsoft_Xna_Framework_Matrix_Microsoft_Xna_Framework_Matrix_), determine points in world space on the near and far clipping planes. For the point on the near plane, pass a source vector with x and y set to the mouse position, and z set to 0. |
| 35 | + |
| 36 | +4. For the point on the far plane, pass a source vector with x and y set to the position, and z set to 1. |
| 37 | + |
| 38 | +5. Create a translation matrix for a point that is the origin, (0,0,0). |
| 39 | + |
| 40 | +6. For both points, pass [Viewport.Unproject](xref:Microsoft.Xna.Framework.Graphics.Viewport#Microsoft_Xna_Framework_Graphics_Viewport_Unproject_Microsoft_Xna_Framework_Vector3_Microsoft_Xna_Framework_Matrix_Microsoft_Xna_Framework_Matrix_Microsoft_Xna_Framework_Matrix_) the current projection matrix, the view matrix. |
| 41 | + |
| 42 | + ``` csharp |
| 43 | + // The closest point at which an object can interact. |
| 44 | + Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f); |
| 45 | + // The furthest point at which an object can interact. |
| 46 | + Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f); |
| 47 | + |
| 48 | + Matrix world = Matrix.CreateTranslation(0, 0, 0); |
| 49 | + |
| 50 | + // Assuming you have the current Projection and View Matrices available. |
| 51 | + Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearsource, |
| 52 | + proj, view, world); |
| 53 | + |
| 54 | + Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farsource, |
| 55 | + proj, view, world); |
| 56 | + ``` |
| 57 | + |
| 58 | +7. Create a [Ray](xref:Microsoft.Xna.Framework.Ray) whose origin is at the near point and whose direction points to the far point. |
| 59 | + |
| 60 | + ``` csharp |
| 61 | + // Create a ray from the near clip plane to the far clip plane. |
| 62 | + Vector3 direction = farPoint - nearPoint; |
| 63 | + direction.Normalize(); |
| 64 | + Ray pickRay = new Ray(nearPoint, direction); |
| 65 | + ``` |
| 66 | + |
| 67 | +8. Loop through each object in the scene using the [Intersects](xref:Microsoft.Xna.Framework.Ray) method to check whether the [Ray](xref:Microsoft.Xna.Framework.Ray) intersects each object. If it is hit, store the object and the distance at which it was intersected. |
| 68 | + |
| 69 | + ```csharp |
| 70 | + // Assuming you have an array containing a list of all "hittable" objects in your game |
| 71 | + foreach (var obj in objects) |
| 72 | + { |
| 73 | + float? distance = ray.Intersects(obj.BoundingSphere); |
| 74 | + if (distance.HasValue) |
| 75 | + { |
| 76 | + // A cachable list of all object "HIT" this frame. |
| 77 | + intersectedObjects.Add(new Tuple<MyObject, float>(obj, distance.Value)); |
| 78 | + } |
| 79 | + } |
| 80 | + ``` |
| 81 | + |
| 82 | +9. Sort the items that have been hit in order of which is closest. |
| 83 | + |
| 84 | + ```csharp |
| 85 | + // Sort the intersected objects by distance |
| 86 | + intersectedObjects.Sort((a, b) => a.Item2.CompareTo(b.Item2)); |
| 87 | + ``` |
| 88 | + |
| 89 | +10. When you completely loop through the objects, the last object stored will be the closest object underneath the area the user clicked. |
| 90 | + |
| 91 | +## See Also |
| 92 | + |
| 93 | +- [Rotating and Moving the Camera](HowTo_RotateMoveCamera.md) |
| 94 | + |
| 95 | +### Concepts |
| 96 | + |
| 97 | +- [What Is 3D Rendering?](../../whatis/graphics/WhatIs_3DRendering.md) |
| 98 | + |
| 99 | +### Reference |
| 100 | + |
| 101 | +- [Ray](xref:Microsoft.Xna.Framework.Ray) |
| 102 | +- [Ray.Intersects<BoungingBox>](xref:Microsoft.Xna.Framework.Ray#Microsoft_Xna_Framework_Ray_Intersects_Microsoft_Xna_Framework_BoundingBox_) |
| 103 | +- [Ray.Intersects<BoungingSphere>](xref:Microsoft.Xna.Framework.Ray#Microsoft_Xna_Framework_Ray_Intersects_Microsoft_Xna_Framework_BoundingSphere_) |
| 104 | +- - [Ray.Intersects<Plane>](xref:Microsoft.Xna.Framework.Ray#Microsoft_Xna_Framework_Ray_Intersects_Microsoft_Xna_Framework_Plane_) |
| 105 | +- [BoundingBox](xref:Microsoft.Xna.Framework.BoundingBox) |
| 106 | +- [BoundingSphere](xref:Microsoft.Xna.Framework.BoundingSphere) |
| 107 | +- [Plane](xref:Microsoft.Xna.Framework.Plane) |
0 commit comments