m_position.y = terrain->getHeight(m_position.x, m_position.z) + (m_pModel->getRadius() * 2);
float CTerrain::getHeight(float x, float z)
{
int cellX = (x - m_pos.x) / MAP_SCALE; // Find cellX which entity point lies within
int cellZ = -(z - m_pos.z) / MAP_SCALE; // Find cellZ which entity point lies within, negative because the way terrain verts are created
int currentCell = (( cellZ * MAP_X ) + cellX ); // Find exact cell point lies within
CVector bottomLeftVert = m_vTerrain[ currentCell ] + m_pos; // Vertices around current cell
CVector bottomRightVert = m_vTerrain[ currentCell + 1 ] + m_pos;
CVector topLeftVert = m_vTerrain[ (currentCell + MAP_X) ] + m_pos;
CVector topRightVert = m_vTerrain[ (currentCell + MAP_X) + 1 ] + m_pos;
float dx = x - bottomLeftVert.x; // x distance is cell
float dz = -(z - bottomLeftVert.z); // z distance in cell
if( dx > dz )
{
CVector normal = (topLeftVert - bottomLeftVert).CrossProduct( bottomRightVert - bottomLeftVert ); // Get normal of triangle
// height = bottomLeftVert + ((dx * triNorm.x) + (dz * triNorm.z)) / -triNorm.y
float height = bottomLeftVert.y + ( normal.x * dx + normal.z * dz ) / -normal.y;
}
else // dz > dx
{
CVector normal = (topLeftVert - topRightVert).CrossProduct( bottomRightVert - topRightVert );
float height = topRightVert.y + ( normal.z * dz + normal.x * dx ) / -normal.y;
return height;
}
Any ideas how to sort this?
[Edited by - Infinite_Daremo on July 28, 2008 2:41:56 PM]