get_distance_to

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By D
:warning: Old Version Published before Godot 3 was released.

The command get_distance_to() appears in the Look at Pointer Example project. But I cannot get it to work, gives a non-existent function error. What is the easiest way to get the distance between two points in the 2d engine.

Thanks,
D

The easiest way to get the distance between two points in the 2D engine, the simplest approach is to find the distance formula.


terraria

Scerfumfor | 2023-03-08 09:34

:bust_in_silhouette: Reply From: Cryingpants

Vector2 has a method called distance_to ( Vector2 to ). Make sure that you’re using it with a Vector2.

Otherwise you can calculate it with your own function using this formula:
|ab| = sqrt(sqr(b.x - a.x) + sqr(b.y - a.y))

You can take the difference as you do in basic numbers.

If a and b are two vectors, then a-b will give you the direction vector from b to a. The magnitude represent the distance.

vinod | 2016-08-07 09:44

To avoid mistakes and for readability, use the built in distance_to instead of rolling your own. A

Also, the magnitude of the difference of the two points, is the equivalent definition of distance_to, it just does all the work in the library code.

Razzlegames | 2016-08-08 02:03

:bust_in_silhouette: Reply From: SupremeAutoCity

great topic!

Mahadev Dp

Love Status

God Wallpaper

lakki365 | 2023-03-11 16:13