Generating runtime terrain collision for open world chunk loading

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fasi123
    extends Spatial


# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var camera
var oldChunk = -1
var mapX = 0
var mapZ = 0
var offsetX = 0
var offsetZ = 0
var consoleLogger = 0
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
func _input(event):
	if event is InputEventKey and event.pressed:
		if event.scancode == KEY_A:
			mapX += 0.1
			offsetX += 0.1
		if event.scancode == KEY_W:
			mapZ -= 0.1
			offsetZ -= 0.1
		if event.scancode == KEY_S:
			mapZ += 0.1
			offsetZ += 0.1
		if event.scancode == KEY_D:
			mapX -= 0.1
			offsetX -= 0.1
# Called when the node enters the scene tree for the first time.
func _ready():
	camera = get_node("KinematicBody/camDir")


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var currChunk = int(mapX/16) + 6 * int(mapZ/16)
	var currX = int(currChunk)%6
	var currZ = int(currChunk/6)
	if (oldChunk != currChunk):
		print(currChunk)
		for x in get_children():
			x.transform.origin.z -= 2*offsetZ
			x.transform.origin.x -= 2*offsetX
		var xNeighbors = [-1,0,1,-1,0,1,-1,0,1]
		var zNeighbors = [-1,-1,-1,0,0,0,1,1,1]
		var loopIndex = 0
		var chunksToLoad = []
		var chunksToRemove = []
		var chunkNeighborsX = []
		var chunkNeighborsZ = []
		for x in xNeighbors:
			var neighborChunkX = currX + xNeighbors[loopIndex]
			var neighborChunkZ = currZ + zNeighbors[loopIndex]
			if (neighborChunkX < 0 || neighborChunkZ < 0 || neighborChunkX > 6 || neighborChunkZ > 6):
				loopIndex += 1
				continue
			var neighborChunk = neighborChunkX + 6*neighborChunkZ
			var chunkExists = ResourceLoader.exists('chunk0' + str(neighborChunk) + '.gltf')
			if (chunkExists):
				chunksToLoad.append(neighborChunk)
				chunkNeighborsX.append(xNeighbors[loopIndex])
				chunkNeighborsZ.append(zNeighbors[loopIndex])
			loopIndex += 1
		print(chunkNeighborsX)
		for x in get_children():
			loopIndex = 0
			var chunkAlreadyLoaded = 0
			for y in chunksToLoad:
				if (x.name == 'chunk0'+str(y)):
					chunksToLoad.remove(loopIndex)
					chunkNeighborsX.remove(loopIndex)
					chunkNeighborsZ.remove(loopIndex)
					chunkAlreadyLoaded = 1	
				loopIndex += 1
			if (chunkAlreadyLoaded == 0):
				if ('chunk' in x.name):
					chunksToRemove.append(x.name)
		loopIndex = 0
		for x in chunksToLoad:
			var chunk = ResourceLoader.load('chunk0'+str(x)+'.gltf')
			var chunkInst = chunk.instance()
			chunkInst.set_name('chunk0'+str(x))
			add_child(chunkInst)
			var xTranslate = chunkNeighborsX[loopIndex]*16 - mapX - fmod(mapX ,16)
			var zTranslate = chunkNeighborsZ[loopIndex]*16 - mapZ - fmod(mapZ, 16)
			chunkInst.transform.origin.x += xTranslate
			chunkInst.transform.origin.z += zTranslate
			var shape = ConvexPolygonShape.new()
			var colObj = StaticBody.new()
			chunkInst.add_child(colObj)
			shape.set_points(chunkInst.get_node('BC'+str(x)).get_mesh().get_faces())
			var ownerId = colObj.create_shape_owner(colObj)
			colObj.shape_owner_add_shape(ownerId,shape)
			
			loopIndex += 1
		oldChunk = currChunk
		for x in chunksToRemove:
			print('yup')
			get_node(x).free()
		if (consoleLogger < 3):
			for x in get_children():
				for y in x.get_children():
					for z in y.get_children():
						print(z.transform.origin)
			consoleLogger += 1
		offsetX = 0
		offsetZ = 0

SO heres the code I have been using for my chunk loader. When I load the chunks though and try to add a static body and a shape with the chunks mesh data it seems to generate the shapes but my kinematic body player passes right through as if the collisions were never added. Ive tried adding the -col suffix to my scenes, that doesn’t work and the create_trimesh_collision doesn’t work either. What am I doing wrong here?

check for the collision mask and layers on both sides?

bloodsign | 2021-03-28 10:25