I thought this would be a simple matter of getting the mesh that defines the CollisionShape3D
and checking if any of the triangles of the mesh intersect with the AABB
, but that didn't work. Let me explain.
I'm doing some custom volumetric 3D pathfinding in Godot 4. For this, I need to loop over all the CollisionShape3D
s in a scene and check if they're in some region of space defined by an AABB
.
I thought I'd loop over each CollisionShape3D
, get its mesh, loop over each surface in the mesh, use MeshDataTool
to loop over the faces in the surface, and then finally check each face for a collision with the AABB
:
func check_for_colliders_in_aabb(colliders: Array[CollisionShape3D], box: AABB):
for collider in colliders:
var mesh = collider.shape.get_debug_mesh()
for surface_idx in range(mesh.get_surface_count()):
var mdt = MeshDataTool.new()
mdt.create_from_surface(mesh, surface_idx)
for face_idx in mdt.get_face_count():
# Check if the face intersects with the AABB
But nothing happens. Specifically, I've figured out that mdt.get_edge_count()
and mdt.get_face_count()
both return zero. Also, while mesh.get_aabb()
returns a correct-looking AABB, mesh.get_faces()
returns an empty list.
I think this might be because the "primitive type" of the mesh is wrong. When I check mesh.surface_get_primitive_type(0)
, the primitive type I get back is 1
which corresponds to PRIMITIVE_LINES
, so I guess the mesh doesn't have any triangles, and therefore no faces? Each CollisionShape3D
is just using a BoxShape3D
as it's shape, no other adjustments have been made from the default CollisionShape3D
.
Note that checking if any of the vertices/edges are in the AABB
is NOT an option, because the faces of the CollisionShape3D
s can be quite big, and my AABB
s quite small so it's entirely possible that an AABB
could be bisected by a face of a CollisionShape3D
without any of the edges/vertices of that face actually intersecting with the AABB
.
Is it possible to convert the mesh into one that does have triangles so that I can check each of the triangles for a collision with the AABB
? If this is not possible, how would I check if some CollisionShape3D
is intersecting/colliding with some AABB
?