Nothing big, just a function that gives you the point of intersection between a line and a plane.
Usefull to use instead of c_trace in many situations.

1. The lines and planes are infinite in every direction, no need to worry about how far something is. If you trace from (0,0,0) to (1,0,0) it automatically extends your line infinitely! These example "start" and "end" points of the line would actually trace from (-inf,0,0) to (+inf,0,0). The same goes for the plane, it automatically extends the plane's surface infinitely in all directions!
2. The distance of the trace to the intersection has no effect on the function's speed. Searching for an intersection on the other side of your level/world? No problem! Just as fast as doing an intersection 1 cm in front of you.
3. The number of objects in your level has no effect on the function's speed. If you just want the point of intersection with the surface of a wall/floor/whatever that you know the coordinates of you don't have to worry about telling it what objects to ignore in it's path and which not to (it ignores all objects in your way to the "plane of intersection").
4. If you want to find an intersection with irregular objects or any objects in it's path like a traditional c_trace would... it is easy to use in combination with c_trace. Tired of giving large values to c_trace just to make sure you go past any distant object? I sometimes use this to optimize a normal c_trace's "start" and/or "end" coordinates to make sure the c_trace is as short as possible while making sure it will actually be able to hit what I am looking for. You decide when it is optimal to use c_trace, this function, or both (c_trace with this function's results as parameters).
5. Since the function dosn't use engine objects, the object/surface dosn't even have to "phisically" exist! Pass it the values of any imaginary plane you wish to get the intersection of!

Quote:
BOOL Line_Plane_Intersection(VECTOR* RESULT, VECTOR* line_p0, VECTOR* line_p1, VECTOR* plane_p0, VECTOR* plane_N);


PARAMETERS:
-RESULT: vector where the intersection's coordinates will be stored
-line_p0: any point on your line (dosn't matter what points are given as long as they are on your line, the line will extend infinately in both directions)
-line_p1: any point on your line (must be different from "line_p0")
-plane_p0: any point on your plane to intersect with (dosn't matter what point it is as long as it is on your plane, the plane will extend infinitely)
-plane_N: normal of your plane (does not matter which side it is facing, the plane's surface will be "double sidded") both front and back sides of the plane can be hit. Cannot be a nullvector (0,0,0).

RETURNS:
0 if the line is parallel to your plane (if it is parallel there are 2 options, either there is no intersection, or if the line is contained in the plane there are an infinite number of intersection points). Either way the "RESULT" vector will not be modified. 0 is also returned if any of the inputs were not valid.
1 if the line and the plane are not parallel (there is a unique intersection point). The "RESULT" vector will be set to the intersection point.

MODIFIES:
-RESULT: This vector will contain the point of intersection if the function returned 1 (not parallel)

CODE:
Code:
BOOL Line_Plane_Intersection(VECTOR* RESULT, VECTOR* line_p0, VECTOR* line_p1, VECTOR* plane_p0, VECTOR* plane_N)
{
	if(vec_dist(line_p0,line_p1)==0||vec_dist(nullvector,plane_N)==0)
	{
		return(0);
	}
	VECTOR line_indeps;
	VECTOR line_coefs;
	vec_set(line_indeps,line_p0);
	vec_set(line_coefs,line_p1);
	vec_sub(line_coefs,line_p0);
	if((plane_N.x*line_coefs.x)+(plane_N.y*line_coefs.y)+(plane_N.z*line_coefs.z)==0)
	{
		return(0);
	}
	var plane_indep;
	VECTOR plane_coefs;
	plane_indep=-(plane_N.x*plane_p0.x)-(plane_N.y*plane_p0.y)-(plane_N.z*plane_p0.z);
	vec_set(plane_coefs,plane_N);
	vec_mul(line_indeps,plane_coefs);
	vec_mul(line_coefs,plane_coefs);
	var T;
	T=(-plane_indep-(line_indeps.x+line_indeps.y+line_indeps.z))/(line_coefs.x+line_coefs.y+line_coefs.z);
	RESULT.x=line_p0.x+T*(line_p1.x-line_p0.x);
	RESULT.y=line_p0.y+T*(line_p1.y-line_p0.y);
	RESULT.z=line_p0.z+T*(line_p1.z-line_p0.z);
	return(1);
}



LINK TO THE CONFERENCE I USED TO LEARN THE MATH BEHIND THIS:
www.youtube.com/watch?v=Q0gnrV699GQ

Heavily commented version of the code to understand everything it does:
Click to reveal..
Code:
BOOL Line_Plane_Intersection(VECTOR* RESULT, VECTOR* line_p0, VECTOR* line_p1, VECTOR* plane_p0, VECTOR* plane_N)
{
   //CHECK THAT THE "SENSITIVE" INPUTS ARE VALID
   //the two points on the line must be different
   //the plane's normal cannot be null
   if(vec_dist(line_p0,line_p1)==0||vec_dist(nullvector,plane_N)==0)
	{
		return(0);
	}
	//GET PARAMETRIC EQUATIONS OF THE LINE
	//F(t)= (x0,y0,z0) + t(x1-x0,y1-y0,z1-z0)
	//resolving the above formula for each component, the resulting equations would be
	//X=x0+t(x1-x0)
	//Y=y0+t(y1-y0)
	//Z=z0+t(z1-z0)
	//programming-wise we cant store the formulas with "t" since we still don't know it's value
	//so we will temporarily store the independant terms and t's coeficients separately
	//and for the time being we ignore the "t"s
	VECTOR line_indeps;//independant terms (origin point)
	VECTOR line_coefs;//t's coeficients (direction vector)
	vec_set(line_indeps,line_p0);
	vec_set(line_coefs,line_p1);
	vec_sub(line_coefs,line_p0);
	
	//CHECK IF THE LINE IS PARALLEL TO THE PLANE
	//if the line is parallel we stop the function because we won't be able to return a point
	//when it is paralel either there is no point from the line matching any point on the plane
	//or if the line is parallel and on the plane there are an infinite amount of matching points
	//calculate if dot product (n1·v)==0?
	//n1 is the plane's normal and v is the line's direction vector (t's coeficients above)
	if((plane_N.x*line_coefs.x)+(plane_N.y*line_coefs.y)+(plane_N.z*line_coefs.z)==0)
	{
		return(0);
	}
	
	//GET PARAMETRIC EQUATION OF THE PLANE (Ax+By+Cz+D=0)
	//we use the plane's normal vector, a known point from the plane, and a second unknown point on the plane
	//pN=(A,B,C)
	//p0=(x0,y0,z0)
	//p1=(X,Y,Z)
	//get the vector formed by the two points on the plane
	//(X-x0,Y-y0,Z-z0)
	//dot product this vector and the plane's normal
	//(X-x0,Y-y0,Z-z0)·(A,B,C) = (AX-Ax0+BY-By0+CZ-Cz0)
	//group the known terms (-Ax0-By0-Cz0) as a number (D)
	//D=-Ax0-By0-Cz0
	//equate everything to zero and you have the plane's equation
	//Ax+By+Cz+D=0
	var plane_indep;//independant terms grouped as "D"
	VECTOR plane_coefs;//coeficients A,B,C
	plane_indep=-(plane_N.x*plane_p0.x)-(plane_N.y*plane_p0.y)-(plane_N.z*plane_p0.z);//D=-(Ax0)-(By0)-(Cz0)
	vec_set(plane_coefs,plane_N);
	
	//PLUG ECUATIONS OF OUR LINE INTO EQUATION OF THE PLANE
	//Ax+By+Cz+D=0 ->
	//A(x0+t(x1-x0))+B(y0+t(y1-y0))+C(z0+t(z1-z0))+D=0 ->
	//Ax0+At(x1-x0)+By0+Bt(y1-y0)+Cz0+Ct(z1-z0))+D=0
	vec_mul(line_indeps,plane_coefs);
	vec_mul(line_coefs,plane_coefs);
	
	//SOLVE FOR "t" IN OUR "LINE PLUGGED" VERSION OF THE EQUATION OF THE PLANE
	//Ax0+At(x1-x0)+By0+Bt(y1-y0)+Cz0+Ct(z1-z0))+D=0 ->
	//At(x1-x0)+Bt(y1-y0)+Ct(z1-z0) +(Ax0+By0+Cz0) +D = 0 ->
	//t(A(x1-x0)+B(y1-y0)+C(z1-z0)) +(Ax0+By0+Cz0) +D = 0 ->
	//t(A(x1-x0)+B(y1-y0)+C(z1-z0)) +(Ax0+By0+Cz0) = -D ->
	//t(A(x1-x0)+B(y1-y0)+C(z1-z0)) = -D-(Ax0+By0+Cz0) ->
	//t = (-D-(Ax0+By0+Cz0)) / (A(x1-x0)+B(y1-y0)+C(z1-z0)) ->
	var T;
	T=(-plane_indep-(line_indeps.x+line_indeps.y+line_indeps.z))/(line_coefs.x+line_coefs.y+line_coefs.z);
	
	//PLUG "t" BACK INTO EACH EQUATION OF OUR LINE TO GET THE INTERSECTION POINT'S COORDINATES
        //F(t)= (x0,y0,z0) + t(x1-x0,y1-y0,z1-z0)
	//resolving the above formula for each component, the resulting equations would be
	//X=x0+t(x1-x0)
	//Y=y0+t(y1-y0)
	//Z=z0+t(z1-z0)
	RESULT.x=line_p0.x+T*(line_p1.x-line_p0.x);
	RESULT.y=line_p0.y+T*(line_p1.y-line_p0.y);
	RESULT.z=line_p0.z+T*(line_p1.z-line_p0.z);
	//indicate that the line was not parallel to the plane and that
	//we found a unique intersection point that we stored in "RESULT"
	return(1);
}


I hope this was usefull for some of you!
Happy new year!


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1