PDA

View Full Version : Hight of point on 3d Triangle



uwi2k2
01-30-2013, 12:02 AM
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: 962

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

cu
uwi2k2

hlewin
01-30-2013, 03:40 PM
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?

uwi2k2
01-30-2013, 11:36 PM
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

hlewin
01-31-2013, 03:06 AM
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 + S*ab + T*ac, 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.

uwi2k2
01-31-2013, 10:54 AM
hi,

thanx thats the way .. i will do some calculation ..

uwi

uwi2k2
01-31-2013, 12:40 PM
sorry,

i canīt get it.. whats wrong:


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

// line 2 and 3
Y = a1 + S*ab1 + T*ac1
Z = a2 + S*ab2 + T*ac2

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

T = ( Z - ((X - T*ac0 - a0) / ab0)*ab2 - a2 ) / ac2
T = ( Z - ((X*ab2 - T*ac0*ab2 - a0*ab2) / ab0) - a2 ) / ac2 | * ac2
T*ac2 = Z - ((X*ab2 - T*ac0*ab2 - a0*ab2) / ab0) - a2 | ???


// use S and 3 line to find T
Z = a2 + ((X - T*ac0 - a0) / ab0 )*ab2 + T*ac2
Z = a2 + ( X*ab2 - T*ac*ab2 - a0*ab2 ) / ab0 + T*ac2 | -a2
Z - a2 = ( X*ab2 - T*ac*ab2 - a0*ab2 ) / ab0 + T*ac2 | *ab0
Z*ab0 - a2*ab0 = X*ab2 - T*ac*ab2 - a0*ab2 + T*ac2*abo | -X*ab2
Z*ab0 - a2*ab0 - X*ab2 = - T*ac*ab2 - a0*ab2 + T*ac2*abo | +a0*ab2
Z*ab0 - a2*ab0 - X*ab2 + a0*ab2 = - T*ac*ab2 + T*ac2*abo | * ( 1 /ac*ab2 )
( Z*ab0 - a2*ab0 - X*ab2 + a0*ab2 ) / ( ac*ab2 ) = - T + T*ac2*abo | ????


i loos my T