Direction between two locations.

Discussion in 'Plugin Development' started by evyatar, Apr 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    evyatar

    I have got a problem while making my plugin...

    I want get the direction between two locations like in a compass but it's not working for me...


    Code:java
    1. LivingEntity mostnearby = null;
    2.  
    3. for (Entity le : p.getNearbyEntities(20, 20, 20))
    4. if (le instanceof LivingEntity) {
    5. LivingEntity target = (LivingEntity) le;
    6.  
    7. if (target.getLocation().getY() > p.getLocation().getY())
    8. continue;
    9.  
    10.  
    11. if (mostnearby == null) {
    12. mostnearby = target;
    13. continue;
    14. }
    15.  
    16. if (mostnearby.getLocation().distance(p.getLocation()) > target.getLocation().distance(p.getLocation()))
    17. mostnearby = target;
    18. else
    19. continue;
    20. }
    21.  
    22. if (mostnearby == null)
    23. return;
    24.  
    25.  
    26. Location pLoc = p.getLocation();
    27. pLoc.setX(p.getLocation().getBlockX());
    28. pLoc.setY(p.getLocation().getBlockY());
    29. pLoc.setZ(p.getLocation().getBlockZ());
    30. pLoc.setPitch(0);
    31. pLoc.setYaw(p.getLocation().getYaw());
    32.  
    33. Location tLoc = new Location(mostnearby.getWorld(), mostnearby.getLocation().getBlockX(), mostnearby.getLocation().getBlockY(),
    34. mostnearby.getLocation().getBlockZ());
    35. tLoc.setPitch(0);
    36. tLoc.setYaw(0);
    37.  
    38. float yaw = (float) (tLoc.toVector().angle(pLoc.toVector()) * Math.PI * 6300) ;


    Thanks for any help
     
  2. Offline

    Zethariel

    You mean the vector between two points? That's geometry. Substract coordinates of the beginning point from the end point - you'll receive a fector facing towards the end point.
     
Thread Status:
Not open for further replies.

Share This Page