   height of the terrain
  I need a function for my terrain class that gets the height of the terrain at the specified vetexes eg GetHeight(double x,double z);I tried doing it by finding what triangle the vertexes are over then finding where the vertex will intersect it.  I tried this and it didn''t work.  I should have and i spent ages trying to get it to work.  Can someone please show me another way to do it that works and would be quicker.  My vertex data is stored like this double vertexData[MAPX*MAPZ][3];MAPX and MAPZ are just the number of vertexes on that axis.  Thanks in adavance. 
  Keep the heightmap itself in memory.  When you need to find out what height a specific point is at, map your position coordinates to the corresponding position on the heightmap.  For points in between vertices, simply interpolate.  Here's some untested code: double Interpolate (double fA, double fB, double fX){return (fA * (1 - fX) + fB * fX);}double GetHeight (double fx, double fy){if (fx < 0 || fx > WIDTH)return 0.0; // ERROR!if (fy < 0 || fy > HEIGHT)return 0.0; // ERROR!int x = (int) fx;int y = (int) fy;int x1 = x + 1;int y1 = y + 1;double dX = fx - (double) x;double dY = fy - (double) y;double fHeight1 = m_afHeightmap[x + y * WIDTH];double fHeight2 = m_afHeightmap[x1 + y * WIDTH];double fHeight3 = m_afHeightmap[x + y * WIDTH];double fHeight4 = m_afHeightmap[x1 + y1 * WIDTH];return Interpolate (Interpolate (fHeight1, fHeight2, dX),Interpolate (fHeight3, fHeight4, dX),dY);} [edited by - Ostsol on February 5, 2004 11:29:47 PM];  What are the variables dx and dy?  also what do you mean by keep the heightmap itself in memory.  do you mean the y part of it? ;quote:Original post by hello_there  What are the variables dx and dy?dx and dy hold the 2D distance that the point is from the point on the heightmap.quote:also what do you mean by keep the heightmap itself in memory.  do you mean the y part of it?   The heightmap is an array of values holding the height of each vertex, so yes. ;  I don''t follow can you show me an example please of dx and dy.  how would you work them out? Thanks ;quote:int x = (int) dx;int y = (int) dy;int x1 = x + 1;int y1 = y + 1;double dX = fx - (double) x;double dY = fy - (double) y;Would that even work? You're using DX and DY before you even make them a variable...//int x = (int) dx;//int y = (int) dy;//int x1 = x + 1;//int y1 = y + 1;double dX = fx - (int) fx;double dY = fy - (int) fy;int x = (int) dxint y = (int) dyint x1 = (int)x + 1int y1 = (int)y + 1 -UltimaX-Ariel Productions"You wished for a white christmas... Now go shovel your wishes!"   [edited by - UltimaX on February 5, 2004 11:07:24 PM];quote:Original post by hello_thereI don't follow can you show me an example please of dx and dy.  how would you work them out? Thanks   Here's an example:Say you have:X = 1.5DX = X - (int)XDX would now equal 0.5X = 50.611448DX = X - (int)XDX would now equal 0.611448It's used to determin how much to interpolate between the height values for the current face. X/YX1/Yo--------------o0.0____I1___1.0|  \    0.0|    \DX/DY   | | 0.5    | | (I3)  I2 |\  | |  1.0o   X1/Y1Interpolate(Interpolate(fHeight1, fHeight2, dX), Interpolate (fHeight3, fHeight4, dX), dY);I1 = Interpolate(fHeight1, fHeight2, dX)I2 = Interpolate(fHeight3, fHeight4, dX)I3 = Interpolate(I1, I2, DY)Correct me if I'm wrong on this...EDIT   Took out the spaces  -UltimaX-Ariel Productions"You wished for a white christmas... Now go shovel your wishes!"   [edited by - UltimaX on February 5, 2004 11:14:41 PM];quote:Original post by UltimaX  Would that even work? You''re using DX and DY before you even make them a variable...Doh!  Typo!  Thanks for pointing that out!  (Like I said: "untested code".) :DYou''re right on what it does, though:Since one isn''t always right over a vertex it becomes necessary to find out the height at an arbitrary point in between vertices.  This is done simply through interpolating the value based on the points on the heightmap representing the heights of the four surrounding vertices. ;  ok i tried both and i keep getting an exception when i run it.  can you please right out the full thing that works.  thanks.  also what in their might cause an exception being thrown. ;  He''s talking about bilinear interpolation of grid nodes.  Unfortunately it won''t give accurate values for the height of your trimesh, e.g., if you had a value of 10 at opposite corners of a grid cell and 0 at the other two corners, bilinear interpolation would give 5 at the center, whereas the trimesh would be either 0 or 10 at the center depending which way you did your triangle subdivision.  To do it right, you first figure out the grid cell, then which of the two triangles, and then intersect the plane of the appropriate triangle. 