Godot Version
4.4.stable
Question
I’m relatively new to coding, and I’m having trouble figuring out how to add collision to this mesh. I got code for basic random terrain generation from a tutorial, and I’ve been trying to research on how I could add collision, but I can’t find anything. I feel like the solution is absurdly simple, but I don’t want to waste any more time attempting to blindly solve this by myself. I’ll copy and paste the current code I have in the mesh. (I just got back into game development so I have no idea if there’s a better way of sending code)
@tool
extends MeshInstance3D
const size := 256.0
func _ready() → void:
if not Engine.is_editor_hint():
var new_noise = noise.duplicate() if noise else FastNoiseLite.new()
new_noise.seed = randi()
self.noise = new_noise
@export_range(4, 256, 4) var resolution := 32:
set(new_resolution):
resolution = new_resolution
update_mesh()
@export var noise: FastNoiseLite:
set(new_noise):
noise = new_noise
update_mesh()
if noise:
noise.changed.connect(update_mesh)
@export_range(4.0, 128.0, 4.0) var height := 64.0:
set(new_height):
height = new_height
material_override.set_shader_parameter(“height”, height * 2.0)
update_mesh()
func get_height(x: float, y: float) → float:
return noise.get_noise_2d(x, y) * height
func get_normal(x: float, y: float) → Vector3:
var epsilon := size / resolution
var normal := Vector3(
(get_height(x + epsilon, y) - get_height(x - epsilon, y)) / (2.0 * epsilon),
1.0,
(get_height(x, y + epsilon) - get_height(x, y - epsilon)) / (2.0 * epsilon),
)
return normal.normalized()
func update_mesh() → void:
var plane := PlaneMesh.new()
plane.subdivide_depth = resolution
plane.subdivide_width = resolution
plane.size = Vector2(size, size)
var plane_arrays := plane.get_mesh_arrays()
var vertex_array: PackedVector3Array = plane_arrays[ArrayMesh.ARRAY_VERTEX]
var normal_array: PackedVector3Array = plane_arrays[ArrayMesh.ARRAY_NORMAL]
var tangent_array: PackedFloat32Array = plane_arrays[ArrayMesh.ARRAY_TANGENT]
for i:int in vertex_array.size():
var vertex := vertex_array[i]
var normal := Vector3.UP
var tangent := Vector3.RIGHT
if noise:
vertex.y = get_height(vertex.x, vertex.z)
normal = get_normal(vertex.x, vertex.z)
tangent = normal.cross(Vector3.UP)
vertex_array[i] = vertex
normal_array[i] = normal
tangent_array[4 * i] = tangent.x
tangent_array[4 * i + 1] = tangent.y
tangent_array[4 * i + 2] = tangent.z
var array_mesh := ArrayMesh.new()
array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, plane_arrays)
mesh = array_mesh