Advertisement
Guest User

nih

a guest
Jan 3rd, 2008
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. // In QuadTreeComponent.QuadTree:
  2.  
  3.     public float GetTerrainDisplacement(Vector3 position)
  4.         {
  5.             //Debug.WriteLine("GetTerrainDisplacement1");
  6.             return m_root.GetTerrainDisplacement(position.X, position.Z);
  7.         }
  8.  
  9.  
  10. // In QuadTreeComponent.QuadTreeNode:
  11.  
  12.         public float GetTerrainDisplacement(float x, float y)
  13.         {
  14.             if (x < m_startX || x >= m_endX)
  15.                 return OutOfRange;
  16.             if (y < m_startY || y >= m_endY)
  17.                 return OutOfRange;
  18.            
  19.             if (m_heightData == null)
  20.             {
  21.                 for (int i = 0; i < 4; i++)
  22.                 {
  23.                     float ret;
  24.                     if (child[i] != null)
  25.                     {
  26.                         ret = child[i].GetTerrainDisplacement(x, y);
  27.                         if (ret != OutOfRange)
  28.                             return ret;
  29.                     }
  30.                 }
  31.                 return OutOfRange;
  32.             }
  33.  
  34.             float thisX = x - m_startX;
  35.             float thisY = y - m_startY;
  36.             int mapX = (int)Math.Floor(thisX / m_cellSize);
  37.             int mapY = (int)Math.Floor(thisY / m_cellSize);
  38.            
  39.             if (mapX < 0 || mapX >= m_width)
  40.                 throw new Exception("QuadtreeNode:GetTerrainDisplacement(x,y) x out of range.");
  41.             if (mapY < 0 || mapY >= m_height)
  42.                 throw new Exception("QuadtreeNode:GetTerrainDisplacement(x,y) y out of range.");
  43.  
  44.             // Create a vector for each corner of the cell we're in
  45.             Vector3 v1 = new Vector3((mapX + 0) * m_cellSize, m_heightData[(mapY + 0), (mapX + 0)], (mapY + 0) * m_cellSize);
  46.             Vector3 v2 = new Vector3((mapX + 1) * m_cellSize, m_heightData[(mapY + 0), (mapX + 1)], (mapY + 0) * m_cellSize);
  47.             Vector3 v3 = new Vector3((mapX + 0) * m_cellSize, m_heightData[(mapY + 1), (mapX + 0)], (mapY + 1) * m_cellSize);
  48.             Vector3 v4 = new Vector3((mapX + 1) * m_cellSize, m_heightData[(mapY + 1), (mapX + 1)], (mapY + 1) * m_cellSize);
  49.  
  50.             // Get the relative position of where we are in the cell
  51.             Vector3 rel = new Vector3(thisX, 0f, thisY) - v1;
  52.             rel /= m_cellSize;
  53.  
  54.             // Linear interpolation time!
  55.             // First, do the top edge (X axis)
  56.             Vector3 L1 = Vector3.Lerp(v1, v2, rel.X);
  57.             // Then the parallel bottom edge (Also x axis)
  58.             Vector3 L2 = Vector3.Lerp(v3, v4, rel.X);
  59.             // Then the Z axis which joins the two
  60.             Vector3 L3 = Vector3.Lerp(L1, L2, rel.Z);
  61.  
  62.             // We have our height!
  63.             float height = L3.Y;
  64.             return height;
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement