Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1.     public float GetActualHeightAt(Vector2 position)
  2.     {
  3.         // Get the grid index it exists at.
  4.         int xIndex = Mathf.FloorToInt(position.x / _unitScale.x);
  5.         int zIndex = Mathf.FloorToInt(position.y / _unitScale.z);
  6.  
  7.         // Get the bottom left and top right positions, this is because of the layout of the Tris in the quad, these are the "right angle" locations.
  8.         Vector2 bottomLeft2D = new Vector2(xIndex * _unitScale.x, zIndex * _unitScale.z);
  9.         Vector2 topRight2D = bottomLeft2D + new Vector2(_unitScale.x, _unitScale.z);
  10.        
  11.         // Delcare the three points of the triangle we want.
  12.         Vector3 p1, p2, p3;
  13.         // Which tri is closer (Compare corner of tri to position)
  14.         if(Vector2.Distance(position, bottomLeft2D) < Vector2.Distance(position, topRight2D))
  15.         {
  16.             // bottom left is, so we want tri, Bottom Left, Bottom Right and Top Left.
  17.             p1 = GetVertexAt(xIndex, zIndex);
  18.             p2 = GetVertexAt(xIndex, zIndex + 1);
  19.             p3 = GetVertexAt(xIndex + 1, zIndex);
  20.         }
  21.         else
  22.         {
  23.             // top right is, so wen want tri, Top Right, Top Left and Bottom Right
  24.             p1 = GetVertexAt(xIndex + 1, zIndex + 1);
  25.             p2 = GetVertexAt(xIndex, zIndex + 1);
  26.             p3 = GetVertexAt(xIndex + 1, zIndex);
  27.         }
  28.  
  29.         // Interlopate magic
  30.         // Do the thing with the thing and get the thing.
  31.         float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
  32.    
  33.         // use that thing with all these things woah, I think these are point weights.
  34.         float l1 = ((p2.z - p3.z) * (position.x - p3.x) + (p3.x - p2.x) * (position.y - p3.z)) / det;
  35.         float l2 = ((p3.z - p1.z) * (position.x - p3.x) + (p1.x - p3.x) * (position.y - p3.z)) / det;
  36.         float l3 = 1.0f - l1 - l2;
  37.  
  38.         // return those weights by the points heights to get the over all height.
  39.         return l1 * p1.y + l2 * p2.y + l3 * p3.y;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement