Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Detecting Player/Entity Distance

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

  1. zgeorge2222
    Offline

    zgeorge2222 New Member

    Joined:
    Mar 3, 2014
    Posts:
    9
    Minecraft User:
    zgeorge22
    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. Falk
    Offline

    Falk Staff Member Global Moderator

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

    zgeorge2222 New Member

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

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    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. TheDiamondYT
    Offline

    TheDiamondYT New Member

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

    aliuly Notable Member Plugin Developer

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

    PEMapModder Notable Member Plugin Developer

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

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

Share This Page

Advertisement