Godot Version
Godot_v4.6.3-stable_linux.x86_64
Question
I created a small script that tests for box-collision and than draw a box - green if no collision is found, red if a collision is detected, but the results are sometimes inaccurate.
In the image, the center is a wall so the box should always be red.
debug_multimesh_solid.instance_count = 1
var pos = debugMeshPosition
var transform = Transform3D(global_basis, pos)
debug_multimesh_solid.set_instance_transform(0, transform)
debug_multimesh_solid.set_instance_color(0, Color.BLUE)
var space := get_world_3d().direct_space_state
var collision_box = SphereShape3D.new()
var params := PhysicsShapeQueryParameters3D.new()
params.shape = collision_box
params.transform = transform
params.collide_with_bodies = true
params.collide_with_areas = false
params.collision_mask = 0x7fffffff
var result := space.collide_shape(params, 1)
if result.size() != 0:
debug_multimesh_solid.set_instance_color(0, Color.RED)
else:
debug_multimesh_solid.set_instance_color(0, Color.GREEN)
That is the code to check the collision.
extends Node3D
@export var wireframe: Shader
var debugMeshPosition = Vector3()
var debug_multimesh_solid: MultiMesh = null
func debug_multimesh_solid_init():
var mm := MultiMeshInstance3D.new()
var box := BoxMesh.new()
var mat := StandardMaterial3D.new()
mat.vertex_color_use_as_albedo = true
box.material = mat
debug_multimesh_solid = MultiMesh.new()
debug_multimesh_solid.use_colors = true
debug_multimesh_solid.instance_count = 0
debug_multimesh_solid.transform_format = MultiMesh.TRANSFORM_3D
debug_multimesh_solid.mesh = box
mm.multimesh = debug_multimesh_solid
add_child(mm)
func debug_multimesh_update():
if true:
debug_multimesh_solid.instance_count = 1
var pos = debugMeshPosition
var transform = Transform3D(global_basis, pos)
debug_multimesh_solid.set_instance_transform(0, transform)
debug_multimesh_solid.set_instance_color(0, Color.BLUE)
var space := get_world_3d().direct_space_state
var collision_box = SphereShape3D.new()
var params := PhysicsShapeQueryParameters3D.new()
params.shape = collision_box
params.transform = transform
params.collide_with_bodies = true
params.collide_with_areas = false
params.collision_mask = 0x7fffffff
var result := space.collide_shape(params, 1)
if result.size() != 0:
debug_multimesh_solid.set_instance_color(0, Color.RED)
else:
debug_multimesh_solid.set_instance_color(0, Color.GREEN)
func _unhandled_input(event):
if event is InputEventKey:
var distance = 0.1
if event.keycode == KEY_UP and event.pressed:
debugMeshPosition.z += distance
if event.keycode == KEY_DOWN and event.pressed:
debugMeshPosition.z -= distance
if event.keycode == KEY_LEFT and event.pressed:
debugMeshPosition.x += distance
if event.keycode == KEY_RIGHT and event.pressed:
debugMeshPosition.x -= distance
if event.keycode == KEY_PAGEUP and event.pressed:
debugMeshPosition.y += distance
if event.keycode == KEY_PAGEDOWN and event.pressed:
debugMeshPosition.y -= distance
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
debug_multimesh_solid_init()
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
debug_multimesh_update()
pass
This is the whole code for my test. It is added to the root Node3D Node.
It allows you to move the Box with the KEY_UP … and KEY_PAGEUP Keys.
Thank you for your time ![]()


