   Triangle interpolation?
  I'm trying to write a function that when given the coords of a triangle and the x,z of a point somewhere inside the triangle, will find the y value of that point.  I've done this before using bilinear interpolation (I didn't understand how it worked though) but that was with squares whose vertices were on an evenly spaced grid, so I don't know how much of that method would apply here.  Can anyone point me in the right direction here?Also, just to make it clear: This is for terrain height that does NOT use a height map!![edited by - XCaVeDoGX on February 19, 2004 5:37:48 AM]
  Well, you *could* get the triangle''s normal, which, coupled with one of the points on the triangle, gives you the plane equation for the plane in which the triangle lies. Then substitute in your known X and Y coordinates, and it should spit out your Z.I''m pretty sure there''s a nicer way though. ;  So what you''re saying is that given the following:1) triangle {T1,T2,T3} - ordered clockwise (or counter-clockwise, depending on your implementation) uniformly with all other triangles in the system.2) point P - is contained within the 3-sided volume formed by the triangles vertices (this volume is implied to extend infinitely along the Y axis [this may cause you some trouble in the future])I''m not 100% positive, but given only the X and Z coordinates of point P, you cannot move P onto the triangle {T1,T2,T3}. ;  You can move the point into the same plane as the triangle, which is (I think) what CaveDog was after.The subspace of 3D space with x and z fixed and y left unfixed is a line parallel to the y axis. The intersection of this line and the plane in which the triangle lies will, unless the triangle is vertical, give you that point.(CaveDog: I''m guessing this is for a ''triangulated irregular network'' like some oldskool flight sims used?) ;////T2// +// |// | // |  \  v// |x P\ | t // |    \|// +-----+    ^//    T0 T1//  //>-<// s////// s = (P.x - T0.x) / (T1.x - T0.x)// t = (P.z - T0.z) / (T2.z - T0.z)// P = T0 + s * (T1 - T0) + t * (T2 - T0)// => P.y = T0.y + s * (T1.y - T0.y) + t * (T2.y - T0.y)is that what you're after? [edited by - oliii on February 19, 2004 6:39:53 AM];  The plane equation is n dot p = d, where n is the normal of the plane, p is a point on the plane and d is a constant for the plane.  From v0, v1, and v2 you can get n = (v1 - v0) cross (v2 - v0).Solve for d : n dot v0.Now, you want P.y given that P.x and P.z are known.  Back to the plane equation : (P.x * n.x) + (P.y * n.y) + (P.z * n.z) = drearrange and P.y = (d - P.x*n.x - P.z*n.z)/n.yOliii, I think your solution only works if the vertices are axis-aligned.  The explicit form of the triangle is T(s,t) = (1-s-t)*v0 + s*v1 + t*v2, and I don't think you can just force s and t axes to line up on x and z in the general case.   [edited by - ajas95 on February 19, 2004 12:33:41 PM];  Ah, thank you ajas it works perfectly :-) ;quote:Original post by ajas95  Oliii, I think your solution only works if the vertices are axis-aligned.  The explicit form of the triangle is T(s,t) = (1-s-t)*v0 + s*v1 + t*v2, and I don''t think you can just force s and t axes to line up on x and z in the general case.  Yeah, I should have put a disclaimer there  This is for heightmaps. 