Inaccurate Collision Box problem

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 :slight_smile:

You do not appear to be using any CollisionObject3D objects. I suggest fixing that, or showing us where they are in your hierarchy.

You’re right. The walls are just collision-enabled with static collisions.
But shouldnt space.collide_shape or space.intersect_shape still work for it?

Depends on where you’re calling it from. You are trying to get low-level access to the physics engine, so I suggest you read up on how to use it. You haven’t provided enough code to tell if you are using it correctly.

But I have posted the whole code?
Just create a new project, add some CSGBox3D with collision enabled and add the code (the second) to root Node3D.

It will automatically render the Collision-Box with green or red and you can move it around with the cursor keys.

Just remove the
@export var wireframe: Shader
that is not needed.

You attached two scripts, and the first one is not complete.

The first is just a snippet from the second that contains collision test and the rendering so you do not need to read all of it.

I have now tested something else:
Before I just used CSGBox3D with Use Collision enabled. If I use a StaticBody3D with CSGBox3d and a CollisionShape with the same size as the CSGBox3D instead it works.

So It seems UseCollision is buggy or at least behaves weird when using it with CSGBox3d.

That’s because you should not be using a CSGBox3D. The only thing they should be used for is creating MeshInstance3D objects. They are horribly inefficient. But there was no indication in your code that you were using one so I couldn’t tell you that.