Sign in to follow this  

Ray Picking

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

Basically I am trying to cast a ray and determine where it intersects with the ground at y = 0. I have been looking around at all the samples that I can find but I feel like I am getting nowhere with this. I would post the code that I have written, but I really feel like its not working at all. Does anyone know how I would go about doing this?

Share this post


Link to post
Share on other sites
You use D3DXVec3Unproject with two input points (MouseX,MouseY, and 0 resp. 1). This will give you two points in 3d space forming a line.

Now you build a plane lying on your y-plane (you wanted y=0). D3DXPlaneFromPoints will do that, just choose three points in the correct order.

Then use D3DXPlaneIntersectLine to calculate the intersection point of the line with that plane.
Done.

Share this post


Link to post
Share on other sites
Well assumming the ground is flat, you can imagine that it is a plane and use the equation of a plane to help get the distance between it and any given ray. The plane equation is: Ax + By + Cz = D, where D is the distance from the origin, <A,B,C> is the plane's normal, and <x,y,z> is any point on the plane.

After that, the first thing is to find the angle between your ray and the ground by using the dot product. If the angle is zero, that means your ray is parallel to the plane; otherwise you the subtract the dot product of the ray's origin and the plane's normal from the plane's distance (D). Your final distance will be that new value divided by the angle.


angle = Vec3Dot(&vRayVector, &vPlaneNormal);
if(angle==0)
return PARALLEL;

newD = -D - Vec3Dot(&vRayOrigin, &vPlaneNormal);

return (newD/angle);


So this will give you the distance between the ray and the plane which you can then multiply by the ray's origin to get the intersection point at zero.

Share this post


Link to post
Share on other sites

This topic is 4664 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this