Your welcome! I'm glad it has already helped someone soo soon after posting!
If you want more speed you can remove the first "if". You just have to be carefull yourself with two of the parameters line_p1 and plane_N (line_p1 is never the same line_p0) and (plane_N is never (0,0,0)).
If your calls are correct this should never happen anyway (that first "if" was just a "newbie safeguard" I placed in there before posting that I dont actually have in my code, just in case someone tried to use this withought understanding what the function is meant for).
Nobody that knows what they are doing would ever do this anyway, the idea of a plane with no normal is silly at best, and defining a line with only one point is just plain stupid...
BOOL Line_Plane_Intersection(VECTOR* RESULT, VECTOR* line_p0, VECTOR* line_p1, VECTOR* plane_p0, VECTOR* plane_N)
{
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);
}
NOTE: You could also remove the second "if", the last "return(1)", and set the initialization to void, for even more speed as it is not necessary if you always make sure you never pass it a line parallel to the plane (but I don't recommend it as it is usually un-predictable and it would be harder to make sure yourself that they are never parallel).
There are only very specific cases where I would feel comfortable removing that second "if" myself.
For example if I was only using this to get an intersection from the camera to the floor and my camera was always slanted looking at it (a view that could never be at floor level with no tilt). Another safe case would be if it was only for straight angle walls (0,90,180,270,360) and the camera's pan could never be at a straight angle.
Happy new year present for you!
