Sign in to follow this  
DivinityZA

Ray in 3D world

Recommended Posts

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


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


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


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


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


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


Link to post
Share on other sites
cabbar    127
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.

Share this post


Link to post
Share on other sites

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