I am creating a project in which there are AStar points all over the place, when I place a mesh on a surface where there are points, I need to check if those points are inside said mesh so I can remove them from the navigation, but for now I have tried two solutions and I haven’t been successful.
I have tried this solution but it didn’t work
func is_point_in_hitbox(point: Vector3) -> bool:
# Cast a ray from the point to a random location (Vector3.ZERO)
# Then check the normal to see if the point was inside the mesh
# A mask is chosen so that level geometry doesn't get in the way.
var hitbox = self.area_3d
var space_state = self.get_world_3d().direct_space_state
var query = PhysicsRayQueryParameters3D.create(point, Vector3.ZERO, 0b10)
query.collide_with_areas = true
query.hit_from_inside = true
var result = space_state.intersect_ray(query)
if not result.is_empty():
var collider: Node3D = result["collider"]
var normal: Vector3 = result["normal"]
# The normal will be Vector3.ZERO in case of a collision from inside.
if collider == hitbox and normal == Vector3.ZERO:
return true
return false
I also tried using the bounding box of the object but it also didn’t work
Couple things, firstly I would never do a Vector3 comparison using equality as precision means it almost never truly is. Rather than : normal == Vector3.ZERO:
try normal.is_equal_approx(Vector3.ZERO).
Secondly your gonna have a bad time if you’re trying to use Trimesh collision shapes as this by default creates concave shapes that ‘have no volume’ (regardless of how they look). So double check your collision shapes are either the built-in primitives or convex.
I hashed up an example just using a raycast node to show this below so in principle the normal check does work. Throw a few print statements in to check if it’s still not working:
I know this is a bit old but I needed to find when a point was inside a shape as well and came across this thread.
I found that using:
query.position = point
query.mask = mask
direct_space_state.intersect_point(query,1)
Would work if the shapes are convex.
Also, as mentioned above, “create_trimesh_shape” will not work, you need convex_shapes. There is a slow create_multiple_convex_shapes function on MeshInstance3D that should convert concave shapes into multiple convex shapes that will work.