# Ray in 3D world

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

## Recommended Posts

Hi guys, Probably a n00b question for most, but my math skills are shoddy, so here goes. I have a ray origin and ray direction in my World coordinate system. How do I get the (X,Y) coordinate of this ray at Z = 0? Some sample code would be appreciated if possible ;) I would like to implement an algorithm myself and I don't have the Unproject functionality avalaible cause I'm not using Managed DX. Regards, Div.

##### Share on other sites
It seems like a ray - plane intersection problem, if the 3D line ever intersects the plane... well a dot product between some point in the line and a point in the plane... gives you the distance between the point and the intersection point
something like that.

Don't worry someone will come with a solution soon.

##### Share on other sites
Like VerMan said, you just have to find the intersection of the ray with the plane z=0, which is the plane with normal (0, 0, 1) and distance from the origin = 0. Then you just take the x, y of the intersection point. The following code gives the distance along the ray where the intersection with the plane occurs. To get the point you have to calculate ray.origin + ray.direction * t. If you use the A B C D coefficients for your planes, then (A, B, C) is the normal and D is equal to dist * length(normal)

struct Plane{		Vec3 normal;	Float32 dist;	Plane(){};	Plane(const Vec3 &a_normal, Float32 a_dist):	normal(a_normal), dist(a_dist){};};Float32 Intersection(const Ray3 &ray, const Plane &plane){	Float32 dotPnRo = DotProduct(plane.normal, ray.origin);	Float32 dotPnRd = DotProduct(plane.normal, ray.direction);	Float32 t = (plane.dist - dotPnRo) / dotPnRd;	if(t < 0)	{		return FLOAT32_MAX; // A big number.	};	return t;};

So in your case you have to do:

Plane plane(Vec3(0, 0, 1), 0);Ray3 yourRay;Vec3 xyzCoords = Intersection(yourRay, plane) * ray.direction + ray.origin;

[Edited by - D_Tr on August 3, 2007 2:40:03 AM]

##### Share on other sites
Thank you guys.

Will try it this afternoon after work ;)

Regards,
Div.

##### Share on other sites
Two quick questions:
1)

Quote:
 Vec3 xyzCoords = Intersection(yourRay, plane) * ray.direction + ray.origin;

Do the order in which these operations are executed make a difference? Or could it be:
Vec3 xyzCoords = Intersection(yourRay, plane) * (ray.direction + ray.origin);
as well?

2) Just to be sure, does ray.direction + ray.origin imply vector addition?

Regards,
Div.

##### Share on other sites
Quote:
 Do the order in which these operations are executed make a difference? Or could it be:Vec3 xyzCoords = Intersection(yourRay, plane) * (ray.direction + ray.origin);as well?

No, it should be the way i posted it.
The function "Intersection" returns a scalar (distance along the ray of the intersection, with t=0 indicating the intersection is on the ray's origin), which is multiplied by the direction of the ray, and then you add the origin of the ray to the result to get the location of the intersetion as a point rather than just distance along the ray.

Quote:
 2) Just to be sure, does ray.direction + ray.origin imply vector addition?

Yep. I used overloaded vector * scalar, as well as vector + vector operators.
t * ray.direction is a vector, and then you add ray.origin which is also a vector.

##### Share on other sites
That makes sense, once again thank you!

##### Share on other sites
D_Tr

Thank you man, it worked like a charm ;)

##### Share on other sites
Nice :)

BTW, since you have a fixed plane whose distance from the center is zero and its normal's x and y are zero too, you could simplify the intersection test by making a special case function and replacing:

DotProduct(plane.normal, ray.origin);
with
ray.origin.z;

DotProduct(plane.normal, ray.direction);
with
ray.direction.z;

and ofc plane.dist with zero.

Just in case you need the speed-up..

##### Share on other sites
Quote:
 Original post by DivinityZAI would like to implement an algorithm myself and I don't have the Unproject functionality avalaible cause I'm not using Managed DX.

I don't know if this helps but take a look at D3DXVec3Unproject. This is the unmanaged version of vector.unproject method.

1. 1
Rutin
23
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 16
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002151
×