Anonymous
Anonymous

Reputation: 131

am I using OpenSimplex wrong

So I tried to use OpenSimplex noise to generate 2D terrain but it seems like I'm missing something as the terrain looks mostly random. What am I doing wrong? Here's the code:

import pygame
from opensimplex import OpenSimplex
tmp = OpenSimplex()

pygame.init()
display_width = 800
display_height = 600
black = (40,40,40)
gameDisplay = pygame.display.set_mode((display_width,display_height))
gameDisplay.fill(black)
gameDisplay.convert()
clock = pygame.time.Clock()

dimensions = [100,100]
size = 40

def mapping(x):
    y = (x + 1) * 10 + 40
    return y

class GroundCell:
    def __init__(self,x,y,dim):
        self.x = x
        self.y = y
        self.dim = dim

tempcells = []
allCells = []

for a in range(0,dimensions[0]):
    tempcells = []
    for b in range(0,dimensions[1]):
        tempcells.append(GroundCell(a*size,b*size,mapping(tmp.noise2d(a,b))))
    allCells.append(tempcells)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                quit()


    for a in allCells:
        for b in a:
            pygame.draw.rect(gameDisplay,(b.dim,b.dim,b.dim),(b.x,b.y,size,size))

    pygame.display.update()
    clock.tick(120)
    gameDisplay.fill(black)

And here's the image that was generatednoise

Upvotes: 1

Views: 239

Answers (1)

digitcrusher
digitcrusher

Reputation: 96

You have to get values between whole numbers, this "smoothes" the result otherwise you will get plain noise. So you will have to change this line:

tempcells.append(GroundCell(a*size,b*size,mapping(tmp.noise2d(a,b))))

with something like this:

tempcells.append(GroundCell(a*size,b*size,mapping(tmp.noise2d(a*0.1,b*0.1))))

Upvotes: 1

Related Questions