How to generate voxels?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SilicateWielder
:warning: Old Version Published before Godot 3 was released.

So, I’m trying to write a voxel-based dungeon crawler, how would one go about generating voxels in godot?

:bust_in_silhouette: Reply From: Zylann

Which kind of voxels? In which kind of view? FPS? Top down?

There is RPG-in-a-box, which is an editor made with Godot that lets you design games with voxels: http://www.rpginabox.com/

Otherwise I think you will need to code this yourself, most likely in C++ with a module. There is quite a lot of material to do voxel stuff in general on the internet, like this: Voxels – Page 2 – 0 FPS

I also did my own attempt here, though it’s still experimental: GitHub - Zylann/godot_voxel: Voxel module for Godot Engine

:bust_in_silhouette: Reply From: DrPlamsa

I made this node called a “cuber” whose “drop_cube()” method you call where you want to drop a cube. It requires the parent node to have a spatial node called “active_character”. You can then reference all the cubes you’ve dropped in the “cube” array. You’ll also need a cube.msh mesh.

Also it sounds like you want StaticBody, not RigidBody, and also you’ll want to snap the character’s position to a grid. This is a hackish way of making voxels.

extends Node

var cube_drop_dist=5
var cubes=Array()
var max_cubes=500
var max_cube_rate=3
var last_cube_time=0

func _ready():
	pass

func drop_cube():
	if OS.get_ticks_msec()>1000/max_cube_rate+last_cube_time:
		if cubes.size()>max_cubes:
			cubes.back().free()
			cubes.pop_back()
		force_drop_cube()
		last_cube_time=OS.get_ticks_msec()

func force_drop_cube():
	var cubedrop=RigidBody.new()
	var active_character=get_parent().active_character
	cubedrop.set_translation(active_character.get_translation()-cube_drop_dist*active_character.get_transform().basis[2])
	var cubemeshinstance=MeshInstance.new()
	var cubemesh=load('res://cube.msh')
	cubemeshinstance.set_mesh(cubemesh)
	cubedrop.add_child(cubemeshinstance)
	var collshape=BoxShape.new()
	collshape.set_extents(Vector3(1,1,1))
	cubedrop.add_shape(collshape)
	cubes.push_front(cubedrop)
	add_child(cubes[0])

func delete_cubes():
	for i in range(cubes.size()):
		print("cubes.size()=",cubes.size()," i=",i)
		cubes[i].free()
	cubes=Array()