# Mesh selection

This topic is 2778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone ... I'll describe the essence (do on Manager DirectX):
I apologize for my English. I do not speak on it. Helped google translate))

I have a plane on which the different meshes, which represent the parallepipidy.

Here's the render code:

 Matrix proj = device.Transform.Projection; Matrix view = device.Transform.View; Matrix World = Matrix.Identity; World *= Matrix.Translation(0, 0, Glub); Vector3 near = new Vector3(Coord.X, Coord.Y, 0); Vector3 far = new Vector3(Coord.X, Coord.Y, 1); near.Unproject(device.Viewport, proj, view, World); far.Unproject(device.Viewport, proj, view, World); device.Transform.World = Matrix.Identity; CustomVertex.PositionColored[] P = new CustomVertex.PositionColored[2]; P[0] = new CustomVertex.PositionColored(far, Color.Green.ToArgb()); P[1] = new CustomVertex.PositionColored(near, Color.Green.ToArgb()); device.DrawUserPrimitives(PrimitiveType.LineList, 1, P); device.Transform.World *= Matrix.Translation(0, 0, Glub); // Here I draw objects // device.Transform.World = Matrix.Identity; 

Where Coord - Mouse position,

Glub - displacement along z.

As a result, I see the following picture:

Glub = 5:

Glub = 100:

If Glub large, the vector does not coincide with the mouse. How do I correct this?

Now do it like this:

 Matrix proj = device.Transform.Projection; Matrix view = device.Transform.View; Matrix World = Matrix.Identity; device.Transform.World = World; // Vector without the coordinate transformation (Blue Line) Vector3 Near_0 = new Vector3(Coord.X, Coord.Y, 0); Vector3 Far_0 = new Vector3(Coord.X, Coord.Y, 1); Near_0.Unproject(device.Viewport, proj, view, World); Far_0.Unproject(device.Viewport, proj, view, World); // Vector with the coordinate transformation (Red Line) World *= Matrix.Translation(0, 0, Glub); Vector3 near = new Vector3(Coord.X, Coord.Y, 0); Vector3 far = new Vector3(Coord.X, Coord.Y, 1); near.Unproject(device.Viewport, proj, view, World); far.Unproject(device.Viewport, proj, view, World); // Display a vector device.Transform.World = Matrix.Identity; CustomVertex.PositionColored[] P = new CustomVertex.PositionColored[4]; P[0] = new CustomVertex.PositionColored(far, Color.Red.ToArgb()); P[1] = new CustomVertex.PositionColored(near, Color.Red.ToArgb()); P[2] = new CustomVertex.PositionColored(Near_0, Color.Blue.ToArgb()); P[3] = new CustomVertex.PositionColored(new Vector3(0, 0, Glub), Color.Blue.ToArgb()); device.DrawUserPrimitives(PrimitiveType.LineList, 2, P); device.Transform.World = Matrix.Identity; device.Transform.World *= Matrix.Translation(0, 0, Glub); /// draw obect /// device.Transform.World = Matrix.Identity; 

Clicking the mouse on the window, I need to select an object on which I clicked. Made it this way:

 Matrix proj = device.Transform.Projection; Matrix view = device.Transform.View; Matrix world = Matrix.Identity; world *= Matrix.Translation(0, 0, Glub); Vector3 near = new Vector3(e.X , e.Y, 0); Vector3 far = new Vector3(e.X , e.Y , 1); near.Unproject(device.Viewport, proj, view, world); far.Unproject(device.Viewport, proj, view, world); IntersectInformation closestIntersection; bool intersects = Mesh.Intersect(near, far, out closestIntersection); if (intersects) return true; 

As a result, will be selected object located at the end of the red line, not the blue ....

I need select an object under the mouse cursor. How do I do?

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013710
×