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?
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)
.
i know i’m 4 years late but in 4.2 stable, i subtract the global positions of the 2 objects and do pythagoras on them.
i did this
func dist3(from : Vector3, to : Vector3):
var v = from - to
var d = sqrt(
v.xv.x
+ v.yv.y
+ v.z*v.z)
return d
The Vector3.distance_to
function still exists in 4.2 stable:
# assuming from and to are both Vector3, like in your example:
var d : float = from.distance_to(to)