automatic creation of BoxShape3D around mesh

Godot Version

4.4.1

Question

Is there are way to specify a BoxShape3D as the collision object for a mesh, and have it set automatically to fit the mesh like a bounding box? I can manually pull the bounds around in the editor, but I’d rather it was lined up perfectly and quickly.

You can set one on import and calculate it in an import script.

You can get the bounding box (AABB) from a Mesh with Mesh.get_aabb()

2 Likes

@mrcdk that works :slight_smile: the aabb uses a start position while the boxshape uses the center of the node, but with a small calculation it works fine. Here is a function that does it for static bodies.

## creates a minimal static BoxShape3D collision shape around the given mesh
## and attaches it to the given Node3D.
static func add_box_collider( node:Node3D, mesh:Mesh )->void:
	var coll_obj:CollisionObject3D = StaticBody3D.new()
	var coll_shape = CollisionShape3D.new()
	var box = BoxShape3D.new()
	
	var aabb:AABB = mesh.get_aabb()
	var center:Vector3 = aabb.position + aabb.size/2
	coll_obj.position = center
	box.size = aabb.size
	
	coll_shape.shape = box
	coll_obj.add_child( coll_shape )
	node.add_child( coll_obj )

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.