2

I am writing mincraft-like game with voxel terrain.

For mountains, I specify a location, a height and size. There is a function to return True if the block at the current (x, y, z) coordinate is part of a mountain. If a block is far away from the centre of a mountain, True is only returned if if the z coord is below a maximum height for the distance from the mountain, ie the further from a mountain a block is, the lower the maximum height. So at the centre of a mountain, the maximum height is high, and True will be returned even if the z is high (I am using a z-up system). However, further away from the mountain, the maximum height will be lower.

However, my current function (below) returns them linearly, and real mountains do not have straight sides:

def isMountain(self, x, y, z, mountainPos, mountainSize, mountainHeight):
        if math.hypot(mountainPos[0] - x, mountainPos[1] - y) < mountainSize:
                if z < (mountainHeight - math.hypot(mountainPos[0] - x, mountainPos[1] - y)):
                    return True
                else:
                    return False

The line 3 checks if z is less than the maximum height for the position, if yes, returning True, otherwise, False.

These are the maximum heights for distances:
Distance: Max Height
0 - 10
1 - 9
2 - 8
...
9 - 1
10 - 0

How could I re-write this function to make it return more mountain-like values: not linear, rather cubic or smooth fall-off (like blender proportianal edit mode), so it would give values more like this:
0 - 10
1 - 9
2 - 9
3 - 8
4 - 7
5 - 5
6 - 3
7 - 1

kCODINGeroo
  • 443
  • 4
  • 13
  • Why not consider a block inside a mountain if is inside a radius **and** above a fixed height? In fact, how do you determine bounds of a mountain in real life? Another option could be consider the mentioned radius and some kind of slope check – alseether May 10 '18 at 09:11

3 Answers3

1

You can either break your head to find out some mathematical formula for this, or you could simulate the natural erosion process. This is usually done using a grid (matrix, cells, ...) and iterating. Basically you would start with more or less random high terrain, then erode it until mountains form, well actually mountains are what remains. That said, this is usually more costly than using a simple function, but on modern computers this would work well.

Also see: https://www.gamasutra.com/blogs/MattKlingensmith/20130811/198049/How_we_Generate_Terrain_in_DwarfCorp.php

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
1

If you were interested in going another route you could use a modified version of perlin noise to use amplitude and frequency then use smoothing transition to get what you want. You could set points to have a general height range and then let the noise algo do its thing to create variability between the points. I have done something similar for creating an inf gen world with different biomes that have different kinds of mountain heights and shapes.

1

Maybe you could use an inverse tan function like this

https://www.desmos.com/calculator/sn7tbepuxh

Where h is the max height, s is the steepness and x is the distance from the centre of the peak. The -1 at the end allows negative values to be ignored so that the base of the mountain won't extend forever.

I've used this for a mountain generator for a small game and it seems to work fine, just as long as you tweak your steepness and height values to the mountain isn't too spiky.

Philip Cox
  • 23
  • 5