# Ray Picking

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

## 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 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 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.

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001808
×