[游戏随机生成地形] Meteorite Algorithm

[游戏随机生成地形] Meteorite Algorithm懒得不想复习:今天在尝试生成随机地形的时候,想到了一个简单又比较实用的方法,不知道是否有前辈已经用过,我暂且将它命名MeteoriteAlgorithm(陨石算法)从名字就能猜出来,这是个什么算法了hahahaha.最终效果:具体思路:1.生成能存储高度的2维网格2.初始化高度(这一步可以影响最终地形效果)3.在比网格稍大的区域内,随机扔下随机大小的陨石(不_游戏地图生成算法

懒得不想复习:

今天在尝试生成随机地形的时候, 想到了一个简单又比较实用的方法, 不知道是否有前辈已经用过, 我暂且将它命名
Meteorite Algorithm (陨石算法)
从名字就能猜出来, 这是个什么算法了hahahaha.

最终效果:
Random Island

具体思路:

1. 生成能存储高度的2维网格

2. 初始化高度 (这一步可以影响最终地形效果)

3. 在比网格稍大的区域内,随机扔下随机大小的陨石(不用扔真的陨石)(比网格稍大的区域: 原网格区域在四个方向 + 最大陨石半径), 陨石会在他的半径所及的圆形区域内削减地面高度, 陨坑中心的地面高度削减最多.

4. 更新地图画面 (可以最后更新, 但是放到这里更刺激)

5. 回到第三步,继续扔陨石

注意, 如果第二步的初始高度全部为一个常量, 则最终生成的地形较为零散. 所以可以考虑在初始化时将他中心升高, 最终会生成一个岛屿, 如上图.

代码:

暂且将我写的代码贴在下面, 代码丑陋之处还请指正.

// Meteorite Algorithm
// Jamesika 2017/6/14
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class MeteoriteAlgorithm : MonoBehaviour {

    //陨石坠落生成随机地形
    //陨石数量,陨石大小规模
    //地图范围
    //地图初始高度
    //海平面高度

    public GameObject landPrefab;
    public Sprite land;
    public Sprite water;

    [Header("是否生成主岛屿")]
    public bool mainIsland = true;
    [Header("主岛屿地形增加高度")]
    public float addHeight = 100f;
    [Header("陨石数目")]
    public int meteoNumber = 20;
    [Header("陨石大小")]
    public Vector2 meteoSize = new Vector2(3,30);
    [Header("地图大小")]
    public Vector2 mapSize = new Vector2 (50, 50);
    [Header("地面初始高度")]
    public float landHeight = 50f;
    [Header("海平面高度")]
    public float seaHeight = 0f;

    public float[][] map;//记录网格的高度
    private SpriteRenderer[][] sprites;//修改网格贴图

    void Start () 
    {
        StartCoroutine(GenerateLand ());
    }
    //由两个协程组成,先初始化数据,再模拟陨石
    IEnumerator GenerateLand()
    {
        yield return StartCoroutine(InitialLand ());
        yield return StartCoroutine(MeteoriteCrash ());
    }
    //初始化数据
    IEnumerator InitialLand()
    {
        map = new float[(int)mapSize.x][];
        sprites = new SpriteRenderer[(int)mapSize.x][];
        for (int i = 0; i < mapSize.x; i++) 
        {
            map [i] = new float[(int)mapSize.y];
            sprites [i] = new SpriteRenderer[(int)mapSize.y];
        }

        float halfDiagLine = Mathf.Sqrt (Mathf.Pow (mapSize.x / 2, 2) + Mathf.Pow (mapSize.y / 2, 2));

        for (int i = 0; i < mapSize.x; i++)
        {   for (int j = 0; j < mapSize.y; j++) 
            {
                map [i] [j] = landHeight;

                //越靠近中间的地方越高
                if (mainIsland == true)
                    map [i] [j] += addHeight*(1-Mathf.Sqrt (Mathf.Pow (mapSize.x / 2 - i, 2) + Mathf.Pow (mapSize.y / 2 - j, 2)) / halfDiagLine);

                sprites [i] [j] = (Instantiate (landPrefab, new Vector2 (i, j), Quaternion.identity) as GameObject).GetComponent<SpriteRenderer> ();
                //如果计算超过 5000 次,就等待
                if ((i+1)*(j+1) % 5000 == 0)
                    yield return null;
            }   
        }

        yield return FlashMap (true);

        yield return StartCoroutine(MeteoriteCrash ());
    }
    //模拟陨石撞击
    IEnumerator MeteoriteCrash()
    {
        //越大的陨石出现的几率越低
        //使用定义域为[0.5,10]的反比例函数来进行简单的概率分布
        Vector2 len = new Vector2(0.5f,10f);
        for (int count = 0; count < meteoNumber; count++) 
        {
            float valueRatio = (meteoSize.y - meteoSize.x)/(len.y-len.x);
            float randNum = Random.Range (len.x, len.y);
            float randRadius = (randNum - len.x) * valueRatio + meteoSize.x;

            float randX = Random.value * (mapSize.x+2*meteoSize.y) - meteoSize.y;
            float randY = Random.value * (mapSize.y+2*meteoSize.y)-meteoSize.y;

            for(int i=-(int)randRadius;i<=(int)randRadius;i++)
                for(int j= -(int)randRadius;j<=(int)randRadius;j++)
                {
                    float x = i + randX;
                    float y = j + randY;
                    if (x < 0 || x >= mapSize.x || y < 0 || y >= mapSize.y || i*i+j*j>=Mathf.Pow(randRadius,2)) 
                        continue;
                    map[(int)x][(int)y] -= Mathf.Sqrt(Mathf.Pow(randRadius,2) - i*i - j*j);
                }

            yield return StartCoroutine (FlashMap (false,new Vector2 (randX - randRadius, randY + randRadius), new Vector2 (randX + randRadius, randY - randRadius)));
        }

    }
    //根据地形高度,改变贴图,flashAll指全部刷新,否则刷新两个坐标中间的网格贴图
    IEnumerator FlashMap(bool flashAll,Vector2 leftTop =  new Vector2(),Vector2 rightBottom = new Vector2())
    {
        //全部刷新
        if (flashAll) 
        {
            leftTop = new Vector2 (0, mapSize.y-1);
            rightBottom = new Vector2 (mapSize.x-1, 0);
        }
        //这里是小于等于,传入值的时候注意范围
        for (int i = (int)leftTop.x; i <= (int)rightBottom.x; i++) 
        {
            for (int j = (int)rightBottom.y; j <= (int)leftTop.y; j++) 
            {
                if (i < 0 || j < 0 || i >= mapSize.x || j >= mapSize.y)
                    continue;
                if (map [i] [j] <= seaHeight)
                    sprites [i] [j].sprite = water;
                else
                    sprites [i] [j].sprite = land;
                //加深颜色,只改变了前两个维度,即只加深了蓝绿色(更像地球)
                float blackValue = Mathf.Clamp((map [i] [j] - seaHeight) / 255+0.8f,0,1);
                sprites [i] [j].color = new Color(blackValue,blackValue,1,1);
                if ((i+1)*(j+1) % 5000 == 0)
                    yield return null;
            }
        }
        yield return null;
    }
}

更加厉害的生成地形的方法

Amit 的多边形游戏地图生成:

https://www.indienova.com/indie-game-development/polygonal-map-generation-for-games-1/

其他可能用到的方法:

分形
元胞自动机
柏林噪音
...

如果大家有什么好的方法生成地形请一起分享吧~

减小陨石半径,增加陨石数量

减小陨石半径,增大陨石数量

进行适当参数调整(减小高度差),生成3D地形

这里写图片描述

今天的文章[游戏随机生成地形] Meteorite Algorithm分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/40140.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注