Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // In QuadTreeComponent.QuadTree:
- public float GetTerrainDisplacement(Vector3 position)
- {
- //Debug.WriteLine("GetTerrainDisplacement1");
- return m_root.GetTerrainDisplacement(position.X, position.Z);
- }
- // In QuadTreeComponent.QuadTreeNode:
- public float GetTerrainDisplacement(float x, float y)
- {
- if (x < m_startX || x >= m_endX)
- return OutOfRange;
- if (y < m_startY || y >= m_endY)
- return OutOfRange;
- if (m_heightData == null)
- {
- for (int i = 0; i < 4; i++)
- {
- float ret;
- if (child[i] != null)
- {
- ret = child[i].GetTerrainDisplacement(x, y);
- if (ret != OutOfRange)
- return ret;
- }
- }
- return OutOfRange;
- }
- float thisX = x - m_startX;
- float thisY = y - m_startY;
- int mapX = (int)Math.Floor(thisX / m_cellSize);
- int mapY = (int)Math.Floor(thisY / m_cellSize);
- if (mapX < 0 || mapX >= m_width)
- throw new Exception("QuadtreeNode:GetTerrainDisplacement(x,y) x out of range.");
- if (mapY < 0 || mapY >= m_height)
- throw new Exception("QuadtreeNode:GetTerrainDisplacement(x,y) y out of range.");
- // Create a vector for each corner of the cell we're in
- Vector3 v1 = new Vector3((mapX + 0) * m_cellSize, m_heightData[(mapY + 0), (mapX + 0)], (mapY + 0) * m_cellSize);
- Vector3 v2 = new Vector3((mapX + 1) * m_cellSize, m_heightData[(mapY + 0), (mapX + 1)], (mapY + 0) * m_cellSize);
- Vector3 v3 = new Vector3((mapX + 0) * m_cellSize, m_heightData[(mapY + 1), (mapX + 0)], (mapY + 1) * m_cellSize);
- Vector3 v4 = new Vector3((mapX + 1) * m_cellSize, m_heightData[(mapY + 1), (mapX + 1)], (mapY + 1) * m_cellSize);
- // Get the relative position of where we are in the cell
- Vector3 rel = new Vector3(thisX, 0f, thisY) - v1;
- rel /= m_cellSize;
- // Linear interpolation time!
- // First, do the top edge (X axis)
- Vector3 L1 = Vector3.Lerp(v1, v2, rel.X);
- // Then the parallel bottom edge (Also x axis)
- Vector3 L2 = Vector3.Lerp(v3, v4, rel.X);
- // Then the Z axis which joins the two
- Vector3 L3 = Vector3.Lerp(L1, L2, rel.Z);
- // We have our height!
- float height = L3.Y;
- return height;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement