CyberLord_Dan

Hi! Can anyone tell me how to unproject the mouse coordinates to create a ray in 3D space And pls some sample code of how the PICKING operation is made (With intersectinformation..) Thnx. Note that I don't want to use the sample framework provided with the DirectX SDK . Thnx again. And I need it in C#, not VB, C++ ar J# !

This is for a program named Verter, but don't search for it because it is still in development. But anyone that helps me I give him/her a free license. I promise .

### Re: Game Technologies: DirectX 101 Selecting objects in 3D space

Etienne2005

It's pretty complicated math but here is how it is done:

First you need to know that the LookAt function get you the reverse of the ViewMatrix real position in the World

So you need to inverse the result of the LookAt function resulting matrix to get the Camera position in World Coordinate

Once you have this camera position you need to understand frustum near and far plane

It's like holding a cone in front of your eye, the Near plan is in front of your eye and because of perspective

the Far plane is bigger then then near plane

as this was not enough you have a 4:3 perspective so you calculate a 2 Square plan but you need to add the 4/3 perspective to get the real value

How do you get those plan coordinate You know the Near plan distance and Far plan distance
You also know the width of your plan so let's assume it's 600 pixel, half give you 300 on each side
You ADD the Near distance vector to the side vector of 300 and you get the right and left vector

Why get all this Because you need to know where the camera is in the World (inverse of View matrix)

You need to get vector to Near and Far Plane, so you can get the Vector that come from the Screen

and goes back the the far plane this direction will be used by RayIntersectMesh

Good luck :-)

### Re: Game Technologies: DirectX 101 Selecting objects in 3D space

Lord_Dan

K, i knew this things already. What I don't is the RayIntersectMesh and the "out IntersectInformation" parameter. Here's a link with something useful I found, but I don't want to use the sample framework and I didn't understood all code.

http://www.mdxinfo.com/tutorials/picking.php

Pls if u can help......

### Re: Game Technologies: DirectX 101 Selecting objects in 3D space

The ZMan

None of the code DX framework code in that tutorial is relevant to the picking example.

The code does exactly what Etienne explained but I will try a different way.

When you draw 3d objects you set a world, view and projection matrix. These 3 matrices determine how an object (which has its own coordinate sytem - usually 0,0,0 is in the center) gets drawn on the screen.

An object starts in object space (0,0,0 in it center usually)

the GPU multiplies it by the world matrix to place it in the world at world space

the GPU multiplies this by the view matrix to position the world so that it looks like it is being viewed from a camera - view or camera space

the GPU multiplies by the projection matrix to 'flatten' the 3d world into 2d space (plus depth) so that it can be drawn on the screen - screen or clip space

Note that screen space is -1<x<1 and -1<y<1 not PIXEL coordinates

So to determine if you have clicked on an object you start with PIXEL coordinates - Convert the pixels to screen space. Remember that this is a 3d world so your 2d click represents a 3d ray going directly into the screen. So construct a vector that goes from the near plane to the far plane. YEs this means you could be clicking on more than one thing along that ray.

Divide by (or multiply by the inverse) of the projection, view matrix and then for each object in your scene divide this ray by the world matrix for that object. Now the ray is in object space and you can compare it to the actual object.

If more than one collision is found then sort byt distance from the camera since you usually want the front one.

You should get a good book on game math or graphics to explain all of these concepts - its only going to get harder from here and if you can't follow a siple example like that you will have problems.

### Re: Game Technologies: DirectX 101 Selecting objects in 3D space

Lord_Dan

K. but isn't there a Vector3.Unproject that can help me. I'm only 13 years old, and I didn't understood matrices very well..... it's not for my math level so I'm trying not to use them very frequently cuz most of the time I get stuck in a bug . Anyway..... Vector3.Unproject doesn't transform the 2D coordinates into 3D ( 2 vectors => a ray). The thing that I don't understand is the ClosestHit parameter in the rayintersectmesh mesh method...

It's the only thing I'm missing to start working seriously on a program (Verter).....

### Re: Game Technologies: DirectX 101 Selecting objects in 3D space

The ZMan

Yes UnProject does the 'division' or rather multiplies by the inverse of the matrix.

Sadly there is no way you can avoid understanding matrices if you want to write 3d games.

Vector3.Unproject does not turn 2d into 3d. It takes a vector in one set of coordinates and turns it into the same vector in a different set of coordinates.

In rayintersectmesh - closesthit returns information about shich triangel within the mesh was hit. It gives you index to the triangle and barycentric coordiantes for WHERE on the triangle the ray hit. If you only need to know if your mesh was hit or not you can ignore this and use the overload that doesn't use that parameter (http://msdn2.microsoft.com/en-us/library/ms886089.aspx).

13 is a great age to learn about matrices.... and what better reason to imporve your math skills that programming graphics.

### Re: Game Technologies: DirectX 101 Selecting objects in 3D space

CyberLord_Dan

K... That resolved my problem and I've finnaly understood the code (after almost 1 unslept night and a hard day at school ...). My final question: the viewport is automaticly created by the device initialization And if I render in a panel that has the X and Y = 10 and 30 I must increase the mouse.X and Y with 10 and 30

tnx