Jump to content
  • Advertisement
Sign in to follow this  
honds

Unprojected

This topic is 4792 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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.

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!