Esempio n. 1
0
        // Sets up terrain, texture, ...
        private float[,] CreateTerrain(Texture2D heightMap)
        {
            float minimumHeight = 0;
            float maximumHeight = 255;

            int width = heightMap.Width;
            int height = heightMap.Height;

            boundingBox = new BoundingBox(
                new Vector3(-width / 2, maximumHeight - minimumHeight, -height / 2),
                new Vector3(width / 2, maximumHeight - minimumHeight, height / 2));

            Color[] heightMapColors = new Color[width * height];
            heightMap.GetData<Color>(heightMapColors);

            // Setup height data from heightmap
            float[,] heightData = new float[width, height];
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    heightData[x, y] = heightMapColors[x + y * width].R;
                    if (heightData[x, y] < minimumHeight)
                        minimumHeight = heightData[x, y];
                    if (heightData[x, y] > maximumHeight)
                        maximumHeight = heightData[x, y];
                }
            }

            for (int x = 0; x < width; ++x)
                for (int y = 0; y < height; ++y)
                    heightData[x, y] = (heightData[x, y] - minimumHeight) / (maximumHeight - minimumHeight) * 30;

            // Setup Physics
            heightMapInfo = new HeightMapInfo(heightData, 1);

            if (heightMapObject != null)
            {
                heightMapObject.DisableComponent();
                heightMapObject = null;
            }

            heightMapObject = new HeightMapObject(
                heightMapInfo,
                new Vector2(heightMapInfo.Width / 2, -heightMapInfo.Height / 2 + heightMapInfo.Height));

            return heightData;
        }
Esempio n. 2
0
        // Rebuilds the HeightMapObject from the given heights
        void CreatePhysicsHeightmap(float[,] heights)
        {
            // If the physics object has been created already with previous settings...
            if (physicsHeightmap != null)
            {
                // And there is a physics simulator available
                Physics p = Engine.Services.GetService<Physics>();

                if (p != null)
                {
                    // Disable old height map object and remove it from engine
                    physicsHeightmap.Body.DisableBody();
                    p.PhysicsSystem.RemoveBody(physicsHeightmap.Body);
                    physicsHeightmap.DisableComponent();
                }
            }

            // Create new HeightMapObject
            physicsHeightmap = new HeightMapObject(new HeightMapInfo(heights, 1),
                new Vector2(-1.0f, -1.0f));
        }