Ray in 3D world

Started by
8 comments, last by cabbar 16 years, 8 months ago
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.
Advertisement
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.
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]
Thank you guys.

Will try it this afternoon after work ;)

Regards,
Div.
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.
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.
That makes sense, once again thank you!

D_Tr

Thank you man, it worked like a charm ;)
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..
Quote:Original post by DivinityZA
I 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.

This topic is closed to new replies.

Advertisement