I finally achieved it! (I think)

Code:
var get_height(VECTOR P, int SW, ENTITY* Terrain)
{
	//P = point that we want the height of (x,y,0)
	//SW = vertex number of terrain ent closest towards the lower left corner of the square
	//Terrain we are calculating for
	
	CONTACT* c1;
	VECTOR Triangle[3];
	VECTOR temp_vec;
	ANGLE temp_ang;
	VECTOR Director[2];
	
	VECTOR formula;
	var ind_term;
	
	//retrieve first point of containing triangle (base corner)
	c1= ent_getvertex(Terrain,NULL,SW);
	Triangle[0].x=c1.v.x;
	Triangle[0].y=c1.v.z;
	Triangle[0].z=c1.v.y;
	
	//retrieve second point of containing triangle (diagonal to base)
	c1= ent_getvertex(Terrain,NULL,SW-32);
	Triangle[1].x=c1.v.x;
	Triangle[1].y=c1.v.z;
	Triangle[1].z=c1.v.y;
	
	//determine third point of containing triangle (varying point)
	vec_set (temp_vec, P);
	vec_sub (temp_vec,Triangle[0].x);
	vec_to_angle (temp_ang.pan,temp_vec);
   
   //retrieve third point of containing triangle (varying point)
	if(temp_ang.pan>45)
	{
		c1= ent_getvertex(Terrain,NULL,SW-33);
	}
	else
	{
		c1= ent_getvertex(Terrain,NULL,SW+1);
	}
	Triangle[2].x=c1.v.x;
	Triangle[2].y=c1.v.z;
	Triangle[2].z=c1.v.y;
	
	//calculate the triangle's plane normal for the formula coeficients
	vec_set(Director[0],Triangle[1]);
	vec_sub(Director[0],Triangle[0]);
	vec_set(Director[1],Triangle[2]);
	vec_sub(Director[1],Triangle[0]);
	vec_cross(formula,Director[0],Director[1]);
	//set the independant term of the formula
	ind_term=(-1*formula.x*Triangle[0].x)+(-1*formula.y*Triangle[0].y)+(-1*formula.z*Triangle[0].z);
	//calculate our point's height plugging our values into the formula
	P.z=(-1*((formula.x*P.x)+(formula.y*P.y)+ind_term))/formula.z;
	return(P.z);
}



A short summary:
Planes can be expressed in two ways, either by three points, or by one point and a normal vector to the plane's surface.
(we have the first way of expressing a plane but need the second)

First I determine which of the two triangles (that form our square) our point is on. Then I retrieve the vertex positions that form that triangle. With those three points we have a plane, now I translate that way of expressing a plane to the point and normal version. Then I store the coeficients of the formula we are building. I multiply all the coeficients with the independant terms and store it all together to have those parts pre-calculated and solved. Now we have to use another formula for the line, and plug it into our unfinished formula or the plane to find out where the line and plane intersect eachother. I plug in to the formula the rest of our known data and multiply by the formula coeficients we calculated before. We need to isolate the variable we are searching for using basic algebra, that was automatically done re-arranging the formula in the previous step taking into account what needed to go to where and how to solve for z.

And finished! OMG I HATE calculus! Finally I can sleep!

It surprizes me how short and simple the function looks after spending most of the the afternoon and night (it's 4:30 in the morning here) learning calculus.

Untested but it should work, I am too tired to test it now and risk feeling pulled into the code for god knows how many more hours... When I program hours fly and I lose track of time...

This was a nightmare for me to figure out, hope it saves you some sleep hours in the future.
tired sleep


"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