[SOLVED] Triangle height math problem?

Posted By: Carlos3DGS

[SOLVED] Triangle height math problem? - 11/02/11 16:54

I have a problem with some triangle math, I was recommended a couple of functions to use to solve it (vec_lerp for its height and vec_dot for its angle).

Diagram of the problem:

I have been reading both functions and am trying to come up with a clever way to implement it but I have to admit I have absolutely no idea what to do...

Here is my problem:

lets say this is a 16x16 square formed by these 4 vectors:
A=(0,15,6)
B=(15,0,7)
C=(15,15,0)
D=(0,0,-1)
the coords are (x,y,height)
How would I calculate the height of H1 and H2 if I only have their xy coordinates to work with?
H1=(3,11,?)
H2=(13,3,?)


I assume the first step would be to calculate which triangle H1 or H2 are on?
How could I calculate if H1 is inside triangle ACD or BCD?
How could I calculate if H2 is inside triangle ACD or BCD?

Posted By: Superku

Re: Triangle height math problem? - 11/02/11 17:10

There may be an easier way but that is what I can think of right now:

Quote:
How could I calculate if H1 is inside triangle ACD

if(H1 is inside the square ACBD)
{
// calculate direction vector C->D
vec_diff(temp,C.x,D.x);
// calculate normal that points outside (in direction of B)
normal.x = temp.y;
normal.y = -temp.x;
normal.z = 0;
vec_normalize(normal,1);
// the normal may be pointing towards A, depending on where C and D are
if(vec_dot(A.x,normal) - vec_dot(C.x,normal) > 0) vec_inverse(normal); // or < 0, I'm not sure right now
// now check if H1 is on the same side of the direction vector temp
if(vec_dot(H1.x,normal) - vec_dot(C.x,normal) < 0) return 1;
}
return 0;
Posted By: Carlos3DGS

Re: Triangle height math problem? - 11/02/11 17:33

While you were answering I think I got a solution for knowing what triangle it is in another way:
Code:
vec_set (temp_vec, vector(H1.x,H1.y,0));
vec_sub (temp_vec,vector(D.x,D.y,0));
vec_to_angle (temp_ang.pan,temp_vec);

if(temp_ang.pan>45)
{
   //Resolve H1.z for triangle ACD
}
else
{
   //Resolve H1.z for triangle BCD
}


Would that also work?

Now I just need to know what to do to figure out H1.z (the height) calculated on triangle ACD's surface...
Any ideas?

EDIT:
I got it working. For anyone that needs something similar, I posted the solution in this thread:
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=386444#Post386444
© 2023 lite-C Forums