# Detecting Player/Entity Distance

Comments in 'Plugin Development' started by zgeorge2222, Mar 9, 2014.

1. Offline

### zgeorge2222New Member

Joined:
Mar 3, 2014
Posts:
9
Minecraft User:
I am making a plugin that needs to detect if an entity is a certain distance (0.1 blocks away, practically up against) from a player. Also this includes if it is near the players head or the players feet, because I don't think that the players coordinates/position refers to the whole body, just the feet/block that they are standing on. Also, I need to detect if a specified entity is in or a block, because if it is I need to get rid of it. Thanks for any help.
2. Offline

### FalkStaff MemberGlobal Moderator

Joined:
Sep 2, 2013
Posts:
1,699
Plugins:
22
Minecraft User:
So do you want to calculate the exact distance or just run a function on close proximity between two players?
3. Offline

### zgeorge2222New Member

Joined:
Mar 3, 2014
Posts:
9
Minecraft User:
I want to run a function on close proximity of a player and an entity.
4. Offline

### FalkStaff MemberGlobal Moderator

Joined:
Sep 2, 2013
Posts:
1,699
Plugins:
22
Minecraft User:
Hmm...I'm not sure how to do that most efficiently.
You could loop through players and check if a->z == b->z and a->x is within 0.1 of b->x and then check if a->y is within 0.1 of b->z. Oh and you need to check a->level == b->level.

Coordinates are part of the entity object so you access them via \$player->entity->[x/y/z/level]. Life would be so much easier if PocketMine had a built in distance function to calculate the distance between two entities.
zgeorge2222 likes this.
5. Offline

### TheDiamondYTNew Member

Joined:
Dec 20, 2014
Posts:
44
Minecraft User:
I need this for my turf wars plugin
6. Offline

### aliulyNotable MemberPlugin Developer

Joined:
Feb 8, 2014
Posts:
1,084
Plugins:
17
Vector3 has a distance method.
7. Offline

### PEMapModderNotable MemberPlugin Developer

Joined:
Oct 9, 2013
Posts:
7,230
Plugins:
11
Minecraft User:
Jr wants to find the closest player.

If you simply want to compare distance, using distanceSquared is faster.