Hight of point on 3d Triangle

Hi all,

for some days i try to get this done - and i am ashamed that i cant figure it out on my own…
i am doind a simple ground-hight detection for my game.
The ground is a mesh of triangles.
I allready can test in with triangle i am, but when i coms to calculating the hight of the position on the triangle i fail.
As my point ( player ) is floting abouve the triangle i only need to work with X and Z and than find the propper Y.

here is my function with should find the propper Y. it only works points close to the triangle corners - and i know why
but i cant find a way to improve it:

GLfloat getYbalanced( GLfloat x , GLfloat y, GLfloat z, GLfloat * dreiArray )
{
        // x, y ,z = Point to test
        // dreiArray = array holding points of triangle - some offset in it !

    GLfloat result = 0.0f;
    // distance point 1  x + z 
    GLfloat w1 = sqrt( powit( ( dreiArray[0] -  x ) )  +  powit( ( dreiArray[2] - z ) ) );
    // distance point 2  x + z 
    GLfloat w2 = sqrt( powit( ( dreiArray[8] - x ) )   +  powit( ( dreiArray[10] - z ) ) );
    // distance point 3  x + z 
    GLfloat w3 = sqrt( powit( ( dreiArray[16] - x ) )  +  powit( ( dreiArray[18] - z ) ) );

    // total distance
    GLfloat w_all = absu( w1 ) + absu( w2 ) + absu( w3 );


    // get points relativ distance -> as a factor to multiply later
    GLfloat f1 = ( 1.0f / ( w1 / w_all ) ); 
    GLfloat f2 = ( 1.0f / ( w2 / w_all ) ); 
    GLfloat f3 = ( 1.0f / ( w3 / w_all ) ); 


    result += f1 * dreiArray[1];  // factor for point - multiply with hight of point
    result += f2 * dreiArray[9];  // factor for point - multiply with hight of point
    result += f3 * dreiArray[17]; // factor for point - multiply with hight of point


    return ( result / ( f1 + f2 + f3 ) ); // result needs to be devided by total factor sum
}

Here a screen, at the corners i am almost fine but in the middel of the triangle the hight is not correct …
i now what is wrong with the function and why i get the result - but i cant figure a way to improve it…
OPEN IMG LINK: [ATTACH=CONFIG]361[/ATTACH]

thanx for any improvment of my function … i guess its easy …

cu
uwi2k2

I do not understand what you mean with ‘height on the triangle’. Do you mean the distance of the point projected onto the triangle from the nearest edge or do you mean the distance to the plane the triangle is in?

hi,

sorry if i was not clear enought.
i like to find the Y of the point on the plane that the triangle is in according to X and Z.
So if the triangle corners are:
A ( 0,0,0 )
B (1,1,0)
C ( 0,1,1 )
how do i find the Y for the point e.g.
P( 0.6, 0.4 ) ( only x and z )

thanx for any help on thisone … i guess its so easy that i just dont see the answer…

uwi

You need to formulate the plane-equatation for the plane.
This can be done using
ab=B-A
ac=C-A
Which leads to the plane equotation
P= a + Sab + Tac, for S and T in R
and solve that for P=(X, Y, Z) where X and Z are known.
S = (X - T*ac0 - a0) / ab0 // from the first line of the system
which can be substitutes into the 3rd line of the system to get an equotation for T without any free variables.
And that can be used to compute the 2nd line of the system.
I’m too lazy now to do this myself.

hi,

thanx thats the way … i will do some calculation …

uwi

sorry,

i can´t get it… whats wrong:

// line 1
X = a0 + Sab0 + Tac0
S = (X - T*ac0 - a0) / ab0 // from the first line of the system

// line 2 and 3
Y = a1 + Sab1 + Tac1
Z = a2 + Sab2 + Tac2

// 3 line to T
Z = a2 + Sab2 + Tac2 | - Sab2
Z - S
ab2 = a2 + Sab2 + Tac2 | -a2
Z - Sab2 - a2 = Tac2 | 1/ac2
T = ( Z - S*ab2 - a2 ) / ac2 | S put in

T = ( Z - ((X - Tac0 - a0) / ab0)ab2 - a2 ) / ac2
T = ( Z - ((X
ab2 - T
ac0ab2 - a0ab2) / ab0) - a2 ) / ac2 | * ac2
Tac2 = Z - ((Xab2 - Tac0ab2 - a0*ab2) / ab0) - a2 | ???

// use S and 3 line to find T
Z = a2 + ((X - Tac0 - a0) / ab0 )ab2 + Tac2
Z = a2 + ( X
ab2 - Tacab2 - a0ab2 ) / ab0 + Tac2 | -a2
Z - a2 = ( Xab2 - Tacab2 - a0ab2 ) / ab0 + Tac2 | ab0
Z
ab0 - a2
ab0 = Xab2 - Tacab2 - a0ab2 + Tac2abo | -Xab2
Z
ab0 - a2ab0 - Xab2 = - Tacab2 - a0ab2 + Tac2abo | +a0ab2
Zab0 - a2ab0 - Xab2 + a0ab2 = - Tacab2 + Tac2abo | * ( 1 /acab2 )
( Z
ab0 - a2ab0 - Xab2 + a0ab2 ) / ( acab2 ) = - T + Tac2abo | ???

i loos my T