Jump to content
  • Advertisement
Sign in to follow this  
DeKaN

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.

If you intended to correct an error in the post then please contact us.

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.
screenhunter_01_may__07_21_54.jpg
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:

screenhunter_12_may__10_21_15_-_kopiya.jpg

Glub = 100:

screenhunter_13_may__10_21_15_-_kopiya.jpg

screenhunter_14_may__10_21_15_-_kopiya.jpg

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;

screenhunter_03_may__11_09_16.jpg


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?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!