1
\$\begingroup\$

I'm trying to build an old game (Lander) where you try to land a spaceship on a flat platform.

But I have no idea how to generate a random surface. Something like in the picture would be nice.

Edit: I don't need a map generation like in Civilisation. I need a surface like hills and valleys.

lander surface

I'm using QT but I think it's more a meta question than a framework question. Thanks!

\$\endgroup\$
3

2 Answers 2

2
\$\begingroup\$

Unity code, but even so...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Terrain : MonoBehaviour
{
    //static members
    public const int HEIGHT_MAX = 10;
    public const int LENGTH = 30;

    //instance members
    int[] terrainPoints = new int[Terrain.LENGTH];
    GameObject[] terrainGOs = new GameObject[Terrain.LENGTH];
    public int scanRadius = 1;

    void Start () 
    {
        Camera.main.transform.position = new Vector3(0,0,-50); //zoom out a bit

        for (int i = 0; i < terrainPoints.Length; i++)
        {
            terrainPoints[i] = Random.Range(0, Terrain.HEIGHT_MAX);
            terrainGOs[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
            terrainGOs[i].transform.position = new Vector3(i, terrainPoints[i], 0);
        }
    }

    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            Smoothe();
    }

    void Smoothe()
    {
        for (int selfIndex = scanRadius; selfIndex < terrainPoints.Length - scanRadius; selfIndex++)
        {
            int self = terrainPoints[selfIndex];

            //scan a local subset *including* self
            int sum = 0;
            int count = 0;
            for (int neighbourIndex = selfIndex - scanRadius;
                     neighbourIndex < selfIndex + scanRadius + 1;
                     neighbourIndex++)
            {
                int neighbour = terrainPoints[neighbourIndex];

                sum += neighbour;
                count++;
            }

            self = sum / count; //get the average over the sample diameter.
            terrainPoints[selfIndex] = self;
            terrainGOs[selfIndex].transform.position = new Vector3(selfIndex, self, 0);
        }

    }
}

Save as Terrain.cs, drop on any GameObject in the scene. Spacebar Smoothe()s the noise generated in Start(), you can do this repeatedly. You can use scanRadius=1, but the larger you make it via the inspector (e.g. 2, 3 and upwards) the more rapidly it will smoothe.

\$\endgroup\$
2
\$\begingroup\$

Well, I’d say that a simple noise function on the height of land on a certain point should be sufficient.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This answer would be more useful if it contained more details - examples of noise functions, what to use as input, how to layer or scale the noise to produce shapes like the example, etc. \$\endgroup\$
    – DMGregory
    Commented Feb 27, 2018 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .