Exemple #1
0
        private float GetVisualWorldHeight(TerrainInfo terInfo, float x, float z)
        {
            int ix = terInfo.PosToVertexX(x);
            int iz = terInfo.PosToVertexZ(z);

            if (ix >= terInfo.Width - 1) ix = (int)terInfo.Width - 2;
            if (iz >= terInfo.Height - 1) iz = (int)terInfo.Height - 2;

            Vector3 p11 = new Vector3(terInfo.VertexToPosX(ix), terInfo.VertexToPosZ(iz), terInfo.At((uint)ix, (uint)iz));
            Vector3 p21 = new Vector3(terInfo.VertexToPosX(ix + 1), terInfo.VertexToPosZ(iz), terInfo.At((uint)ix + 1, (uint)iz));
            Vector3 p22 = new Vector3(terInfo.VertexToPosX(ix + 1), terInfo.VertexToPosZ(iz + 1), terInfo.At((uint)ix + 1, (uint)iz + 1));
            Vector3 p12 = new Vector3(terInfo.VertexToPosX(ix), terInfo.VertexToPosZ(iz + 1), terInfo.At((uint)ix, (uint)iz + 1));

            float t, s, ret;

            if (x - p11.x < p12.y - z)
            {
                t = p21.z - p11.z;
                s = p12.z - p11.z;
                ret = p11.z + (x - p11.x) / terInfo.Scaling.x * t + (z - p11.y) / terInfo.Scaling.z * s;
            }
            else
            {
                t = p22.z - p12.z;
                s = p22.z - p21.z;
                ret = p22.z + (x - p22.x) / terInfo.Scaling.x * t + (z - p22.y) / terInfo.Scaling.z * s;
            }

            ret = ret * terInfo.Scaling.y + terInfo.Offset.y;
            //float ret2 = terInfo.GetHeightAt(x, z);
            if (float.IsNaN(ret)) ret = 0;
            return ret;
        }