# Distance between 2 location

Hello, i need to have the distance between 2 location, in my exemple, i want to use the location of 2 players
but it’s not only for players…

in bukkit you can use
loc1.distance(loc2)
how can i use it in sponge?

Make sure the locations are in the same extent/world then:
`loc1.getPosition().distance(loc2.getPosition())`
If you are comparing distances use `distanceSquared(..)` instead

2 Likes

THX
it’s exactly that!

Also, just to clarify @Faithcaio’s point on distance comparisons- you should opt for distanceSquared(…) when you don’t need the actual distance. The reasoning behind that is because the distance(…) method makes a call to Math.sqrt(…) which is an intensive operation. Thus, the usage of it in a scenario in which it is not necessary will put more strain on the server (app etc) and slowing things down (very-situational, and something to just keep in mind when performance is something you need).

Sorry for the revive, but does anyone know how to do this in API 6?
It looks like the distance functions aren’t under position anymore.

It’s still there. I just checked, API 6 (if I read github right) uses `org.flowpowered.flow-math v1.0.3`. I couldn’t find a site hosting the javadoc easily so I just downloaded it here: Central Repository: com/flowpowered/flow-math/1.0.3. In `com/flowpowered/math/vector/Vector3d.html` the following methods are documented:

``````double	distance(double x, double y, double z)
double	distance(float x, float y, float z)
double	distance(Vector3d v)
double	distanceSquared(double x, double y, double z)
double	distanceSquared(float x, float y, float z)
double	distanceSquared(Vector3d v)
``````

I don’t believe that has changed if Sponge ever used an earlier version. Also, make sure the position you are working with is from `Location#getPosition()`.

Although please read the above replies as they go over good practice when dealing with the basics of distance performance.

1 Like