How do I find a spheres distance to it’s radius? It seems that the in-editor units for the spheres radius Is different than using the function distance_to(), and distance_squared_to(). I want the spheres radius distance so I can have a percentage of how far a Node3D is from the center of the sphere while inside.
You mean distance to center, not distance to radius, right? because radius is not something that has position. So if you know the sphere’s center then distance_to() should work.
distance_to() gives you the distance to the center of the sphere, since spheres are defined by their center and radius. Conveniently, if you subtract the sphere’s radius from that distance, it gives you the distance to the closest point on the surface of the sphere. This remains true even if you’re inside the sphere, you’ll just get a negative distance.
The sphere’s position is it’s center, and the radius is a constant distance from that point. If you draw a line connecting some thing and the center of the sphere, it will pass through the sphere’s surface exactly one radius away from the center of the sphere.
I’d probably rename things a bit:
var distance_node_to_sphere_center = sphere_center.distance_to(node3d.global_position)
var distance_node_to_sphere_surface = distance_node_to_sphere_center - sphere_radius
Bahahah. I was using distance_squared_to() which was returns a different value than distance_to(). distance_to() will return the same unit that the radius on which is badum tss the distance from the center of the circle to the radius which is what i needed. thanks guys.
Use distance_to() if you want to compare the radius with something else.
The squared distance is cheaper, and is useful in some circumstances. If you’re just testing whether something is within range (that is, greater or less than a specific distance), you can check the squared distances instead.
It’s cheaper because the length equation is sqrt((dx * dx) + (dy * dy) + (dz * dz)).
Distance squared is just (dx * dx) + (dy * dy) + (dz * dz), and square roots are expensive to calculate.
So if I wanted it to be less computationally expensive and use distance_squared_to() to be able to compare it to the radius I could square the radius and compare it to that value? also, thanks so much for helping me look into this, I really appreciate it.
EDIT: Just tested it in code and looks like the math is lining up.