Inconsistency between distance values

Godot Version

4.2.1

Question

I have multiple StaticBody2Ds in a scene. I am getting all other sibling StaticBody2D’s within a distance visibility (set to 5) below:

func get_neighbors() -> Array[StaticBody2D]:
	var neighbors: Array[StaticBody2D] = []
	var all_boids = get_tree().get_nodes_in_group("boids")
	for boid in all_boids:
		if boid == self: continue
		if position.distance_to(boid.position) <= visibility:
			boid.modulate = Color.BLACK # CODE FOR DEBUGGING
			neighbors.append(boid)
	return neighbors

I added a Node2D to one of these StaticBody2Ds to give it a circular highlight

extends Node2D

func _draw():
	draw_circle(position, 5, Color.from_hsv(0, 0, 1, 0.2))

While both visibility and the radius of draw_circle are both set to 5, the circle ends up covering a bigger area than visibility. The position.distance_to(boid.position) <= visibility only returns True when the neighbor objects are significantly closer to the object, rather than returning True when it enters the highlight circle

Any idea what might be happening/How to convert the distance units?

It’s hard to know what you mean by significantly closer to the object. Your condition is checking the distance between the origin point of both objects, which means the center of the boid must be fully in range in order to be true. Does your debug circle show on both boids being checked? If so how far into the radius is it before it is true? Maybe you can add a debug label that shows the distance between the boids you’re checking.

(Temporarily changed visibility to 25. Circle radius remains 5)

Debug circle is only on one boid. Outputted the value at which the distance check triggers, is always less than 25. In this case its ~23.9

This is what a distance of ~47.3 looks like

Can you try updating your code to check the global_position of the boids in the distance check? I’m not sure what the root node that contains the boids looks like, but the position of each element will be relative of their parent, so if they exist within the same parent, it shouldn’t be an issue, but it may be the cause.

Tried that, no change. On my display at least, I can approximate the correct radius of the circle by dividing my visibility by 14, and then using that as the radius for the circle.