# Unprojected

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

## Recommended Posts

I know I was a bit of an ungrateful jerk before. I am sorry about that, I did appreciate the help. But people where say that what I was trying wasn't possible even though I knew it was. In case anyone was interested, I found a way to do what I wanted in my last question without resetting the device like everyone was saying. I will send you the code if you want. I thank you all for trying, just please be open minded. Anyway, my question: this should be easy for most of you. I have a plane that is perpendicular to the Z axis. The Z value on any point in this plane is always 0. The camera is looking straight at it. I want to get the position on this plane under the point that the user clicked on. Here is what I have so far.
D3DXVECTOR3 pOut, in, tmp1, tmp2;

tmp1.x = (float)viewport.Width / 2.0f;
tmp1.y = (float)viewport.Height / 2.0f;
tmp1.z = 0.0f; // near clipping plane

tmp2.x = (float)X;
tmp2.y = (float)Y;
tmp2.z = 1.0f; // far clipping plane

in = tmp2 - tmp1;

D3DXVec3Unproject( &pOut, &in, &viewport, &matProj, &matView, &matWorld );

tile_x = (int)(pOut.x);
tile_y = (int)(pOut.y);


However, I cannot make much sense of the results. For example, I click on a point I know is at (0,0,0) and it gives me (-847,-708,-50) which isn't even close. (-50 does happen to be my camera eye position) If anyone can look at my code and find a way I can get the correct position on the plan (X,Y,0) please let me know. I have been googling for an answer for over an hour now and this should be very straight forward. Thank you in advance for any help.

##### Share on other sites
hi,

this is what I do :

D3DXVECTOR3  start((float)mousePos.x, (float)mousePos.y, 0.0f);D3DXVECTOR3  end((float)mousePos.x, (float)mousePos.y, 1.0f);D3DXVECTOR3  dir;D3DXVec3Unproject(&start, &start, &viewPort, g_Camera.GetProjectionMatrix(), g_Camera.GetViewMatrix(), &id);D3DXVec3Unproject(&end, &end, &viewPort, g_Camera.GetProjectionMatrix(), g_Camera.GetViewMatrix(), &id);dir = end - start;

with mousePos in screen space (e.g. pixels, (0, 0) at the left-upper corner) Then, with start and dir, you can compute the intersection on any quad / triangle you want.

Don't know if it's what you were asking, I hope it'll help ^^

##### Share on other sites
Thank you, that is a step in the right direction.

However, I have a stupid question (forgive me, I am new to 3d mathematics)... what is the code needed to get the intersection of the plane at Z=0 using the direction that your code computes? I don't need to know it intersects a specific triangle, just if it intersects the plane Z=0. However if you only have code to get the intersection of a triangle (seams more complicated than what I need) I can change my code to use triangles instead.

Thank you again for your help.

##### Share on other sites
this is what i use to get an intersection point on a plane:

[SOURCE]float t = -(Plane.normal.x*raypoint.x + Plane.normal.y*raypoint.y + Plane.normal.z*raypoint.z + Plane.distance) / (Plane.normal.x*raydir.x + Plane.normal.y*raydir.y + Plane.normal.z*raydir.z);		if(t<0) return; //not intersecting		planePos = raypoint + (raydir*t);[/SOURCE]

the code should be fairly obvious you just need your plane (normal + distance), and a ray (point + direction).

you may need to negate the ray direction depending on which way round you have yours.

##### Share on other sites
Thank you. That code will be usefull, but I forgot an important point.
I need to know the actual point in which it intersects the plane. If you or anyone else can help me get the exact point, it would be appreciated.

##### Share on other sites
thats the planePos (vec3) part at the end of the code. I just riped it from so old code, and forgot to point that out :/ soz

##### Share on other sites
Oh ok. No problem at all. Thanks!
I didn't realize that was a D3DXVECTOR3 because there weren't any data types in your code.