'distance_to' in 3d? How to get distance between 2 bodies in 3d>

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Macryc

Hi All!
I have a 3d scene with a cannon in it that shoots a rigid body projectile. Once the projectile hits the ground, I want to measure the distance between the cannon (which is a static body) and the projectile (which is a rigid body), and pass the value into a variable.
I’ve read about a distance_to method in 2d. Is there something similar in 3d?

:bust_in_silhouette: Reply From: Magso

It’s the same in 3D.

Vector3.distance_to(Vector3)

translation, transform.origin and global_transform.origin can be used to get the Vector3’s and try not to mix one with the other for example

translation.distance_to(other_node.global_transform.origin)

will not work properly if the nodes are parented either to each other or to the same spatial node that’s not at (0,0,0).

1 Like