[Help] Random Terrain Generation

Hey guys! I tried to create a random mesh generator for my game! I want to create the terrain in chunks, so i can change the subdivision when i get closer! I made good progress and the script works for me!
But now there is a problem: when i create more than one chunk they dont fit together as they should! (They are wrong-positioned) And i can not figure it out how to solve it!

When you open the blend file you can see the script on the right! In the 3d viewport you can see a larger chunk and 4 smaller chunks!

When you scroll up to the top of the script you can change the value of the xpos and the ypos coordinates in the first number of the for command! By increasing this number you can tell blender where it should generate the chunk! When you set both to zero it will generate the chunk at 0,0,0
When you increase xpos to 1 it will generate the chunk one “chunksize” further on x-direction! So they are already at the right location!

You can also tell blender how much of this chunks should be generated by changing the “2”!
for xpos in range(0,2):
for ypos in range(0,2):

You can also change the size of the chunks by increasing the chunksize, but the final result should be size 10!

But to get a smooth terrain these chunks should have the same vertices on the edges, so it looks like “one”! And i can not figure it out how to solve this, so that they fit together, because they always have a different shape!

Can you teach me how to solve this?
Thank you for your help!

Attachments

Random TerrainOnline.blend (602 KB)

How are they supposed to fit together? I can’t tell from looking at your file.

Could you clean up the blend a little? Remove everything that is not part of the problem.
Tell how to start it, maybe post a picture where or how it should fit.

also you can add the huge cube to an background scene, so it would always be the background of your game without cluttering the main scene.

I updated the file and the question! :smiley:

I don’t know what you did with your grid, but I can’t really tell what position your terrain segments are at without it.

first of all that is what your modifers do, consider not using them if you don’t understand what and why they do.
In short, you cant apply modifiers on individual tiles. Works only on single mesh map.

http://i.imgur.com/4XfI6d5.jpg

Anyway, modified your thing a lot because it didn’t make sense to me.
(i dont mean that mine will either :slight_smile: )

http://i.imgur.com/bWwLa1J.jpg


import bpy
import random
from math import sin,cos,sqrt,exp




def createChunk(xPos,yPos, resolution = 35, chunkSize = 10,  noiseScale = 0.55):   


        scale = chunkSize / resolution     


        xGlobal, yGlobal = xPos*chunkSize, yPos*chunkSize
      
        #Create Mesh
        bpy.ops.object.add(type='MESH')
        ob = bpy.context.object  
        ob.name = "Terrain_{}_{}".format(xPos,yPos)
        name = ob.name   
        me = ob.data                 


        verts=[]
        faces=[]


        vertexMap = {}


        # Create vertices
        for x in range(0,resolution+1):
            for y in range(0,resolution+1):
                dx, dy = (x*scale+xGlobal)*noiseScale, (y*scale+yGlobal)*noiseScale
                z = sin(dx)*sin(dy) * 0.25 + sin(dx/3)*sin(dy/3) * 0.25 + sin(dx/11)*sin(dy/11) * 0.5
                vertexMap[(x,y)] = len(verts)                
                verts.append([x*scale,y*scale,z * chunkSize ])


        #Connect Vertices t faces
        for x in range(0,resolution):
            for y in range(0,resolution):
                A  = vertexMap[(x,y)]
                B  = vertexMap[(x+1,y)]
                C  = vertexMap[(x+1,y+1)]
                D  = vertexMap[(x,y+1)]


                faces.append([A,B,C,D])


        # Fill Mesh with data      
        me.from_pydata(verts, [], faces)
        me.update()
        
        bpy.data.objects[name].location = [xGlobal, yGlobal, 0.0]


        #Collision Bounds
        bpy.data.objects[name].game.use_collision_bounds = True
        bpy.data.objects[name].game.collision_bounds_type = "TRIANGLE_MESH"
        bpy.data.objects[name].game.collision_margin = 1.0


        #Material
        mat = bpy.data.materials["ground"]
        ob = bpy.context.object
        me = ob.data
        me.materials.append(mat)


bpy.ops.object.select_all(action='SELECT')


bpy.ops.object.delete()


MAP_SIZE = 6
        
for xPos in range(MAP_SIZE):
    for yPos in range(MAP_SIZE):
        createChunk(xPos,yPos)
        

The code has auto delete all obj in it

Attachments

terrain.blend (567 KB)