How to autospawn in voxels based on Proximity to a player

Just getting my teeth stuck into BGE after a background in Blender stills and also python. I want to create a simple ‘game’ based on the principle that the world is built of cubes or ‘voxels’ and that new voxels are added in based on distance from the player, but don’t know how to turn this pseudo code into a BGE functioning ‘game’. The effect I’m trying to achieve is the affect that as the player moves forward move cubes are added in for the character to walk to.

Load distance = 10 Blender Units
Voxel = default cube
Player = object

if distance (player)(voxel) > load distance
add object (voxel)

I’m assuming you want to make something like minecraft? first before you do any fancy loading and unloading you will need terrain generation code that returns a value based on the x, y location of an position to which you plan to add a block. Now, if you want to skip that part and just do flat generation, what I would do is have to for loops like this:


amt = 5 #how many loop iteration thingys to do

for x in range(-amt, amt):
    for y in range(-amt, amt):
        pos = [x,y,0] #multiply by a scalar if blocks are not 1x1x1
        if pos not in currentBlockPositionList: #a list of positions of all the current blocks in the scene
            block = scene.addObject(Block", own)
            block.worldPosition = [x,y,0] #multiply by a scalar if blocks are not 1x1x1

thats for loading in blocks. if you want to remove them just have a inverted near sensor or something to delete them at a distance. be sure to remove their position from the currentBlockPositionList when you do!

You can also try to mess with this blend I’m kind of still working on (which is still kind of rudimentary and very experimental) which does what you need but on an actual terrain.
noise.blend (8.9 MB)

Also it does require UPBGE to run.

Thanks for the reply! I’ll try and integrate the code as you said. Eventually, I’d like to create something akin to Minecraft but really my long term goal is to make a procedurally generated world but you’ve got to start somewhere.

One last thing, is this all in the Python editor? I’m assuming the only other thing I would have to do is to add in a moving player for this to work but please enlighten me if I’m wrong.

Yeah I’m not sure if you saw my second post (posted right before yours^^) but yes, it’s all in python editor and you need a moving player.

Cheers man, I’ll check that out. It would be a nice progression after this to apply it to real person noise on a proper landscape with LOD, but I’m getting a bit carried away there for the moment.

Never used UPBGE before, but I guess someone had to try and do something about BGE as Ton Roosendaal isn’t focused on it. Can’t blame him though as Cycles has come leaps and bounds.

How about you check out marcoit’s blend on post 16.I had a lot of fun playing with it.https://blenderartists.org/forum/showthread.php?411416-I-could-not-figure-out-how-to-dig-on-any-ground-i-walk-on

Hi there, I’ve implemented the code you suggested. If also introduced a basic moving character. However, I can’t getting it working and get the error: “Python script fail, look in console for now”. I think this is because blockPositionList has not been defined but I’d appreciate you shedding some light on this.

BGE Procedurals.blend (431 KB)

I strongly recommend you download upbge and try my noise.blend.

The reason blockPositionList is not defined is because you have not defined it. It is a list of block positions in the scene.

I tried your blend file and turns out it works just fine in Blender, as I was having trouble extracting UPBGE. There were some issues with the player falling under the map but its getting there and looks like I could learn a lot from it. I’ll study your code and let you know if I’ve got any questions.

Here i fixed your .blend.

What did i do?

  • added a cube on layer 2, this one gets spawned.
  • connected to script on the player cube (how would you run it else?)
  • removed the character movement, set to simple motion, using velocity (ticked the L behind it) to let your character jump (this solves another error that you had).
  • added missing stuff into the script and made it run in module mode.
  • added physics to your character and a bounding box.

The script given to you at above post is incomplete.

BGE Procedurals_working.blend (466 KB)

Hi mate, thanks for this. However, when I run the script and move far away from the cubes no more cubes load in. I’m perhaps doing something wrong as I see you’ve added in code.

I have done nothing else then making this script work.


amt = 5 


#how many loop iteration thingys to do


for x in range(-amt, amt):
    for y in range(-amt, amt):
        pos = [x,y,0] 
        #multiply by a scalar if blocks are not 1x1x1
        if pos not in currentBlockPositionList: 
            #a list of positions of all the current blocks in the scene
            block = scene.addObject(Cube)
            block.worldPosition = [x,y,0] 
            #multiply by a scalar if blocks are not 1x1x1

Did you look at the link i posted?

Yes I did, the code is slightly above my comprehension at the moment but I’ll try and break it down.

What part of the noise.blend code deals with creating the terrain shape?

The code inside of the setVerts() function.