# Ray tracing math help

## Recommended Posts

asimov02    100
Hi guys, I need some help with the math for ray tracing to find the distance between a point and a surface. Say i have a point in space P(x,y,z) and a surface defined by a triangle with vertices (x1,y1,z1),(x2,y2,z2),(x3,y3,z3). I need to check if the ray from the point to the plane intersects the plans and if it does then find the distance from the point to the plane. The camera is located at C(x,y,z). Do I even need the camera location for this method? I have looked it up online but i am not clear about it. If someone can show me the math properly, i'll really appreciate. I am basically trying to do z-buffer/hidden surface using ray tracing.

##### Share on other sites
jyk    2094
Quote:
 I need to check if the ray from the point to the plane intersects the plans and if it does then find the distance from the point to the plane.
What do you mean by 'the ray from the point to the plane'?

If all you have is a point and a triangle, there is no ray. Do you also have a direction vector? Or is it really just a point?

[Edit: Is the ray formed from the points C and P, perhaps?]

##### Share on other sites
taz0010    277
Do you want to know whether the ray intersects the triangle? You do this by intersecting it with the triangle's plane and then checking whether the point is inside the triangle. There are many methods of doing so, but the fastest is probably by using barycentric coordinates.

bool Intersection::pointInTriangle(const Vec3& point,ConstTriangleRef tri){	Vec3 vec0,vec1,vec2;	float invDenom;	vec0 = *tri.v2-*tri.v0;	vec1 = *tri.v1-*tri.v0;	vec2 = point-*tri.v0;	float dot00 = vec0.dot(vec0);	float dot01 = vec0.dot(vec1);	float dot02 = vec0.dot(vec2);	float dot11 = vec1.dot(vec1);	float dot12 = vec1.dot(vec2);	invDenom = 1 / (dot00 * dot11 - dot01 * dot01);	float u = (dot11 * dot02 - dot01 * dot12) * invDenom;	float v = (dot00 * dot12 - dot01 * dot02) * invDenom;	// Check if point is in Triangle	return (u >= 0) && (v >= 0) && (u + v <= 1);}