How to check if a player is looking at a player?

Discussion in 'Plugin Development' started by Trevor1134, Jul 29, 2013.

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

    Trevor1134

    How do I check if player1 can see player2 and how to establish player2 as a 'Player player2 = #####'?
     
  2. Trevor1134
    Quite hard to achieve, but what i'd suggest doing is get the block the player is looking at, then subtract that location from the players location to get a direction vector. Then 'travel along' this vector checking if a player is within a few blocks at each stage, this should give you a broad idea of whether a player is in someone's crosshairs but it's far from accurate.
     
  3. Offline

    RainoBoy97

    chasechocolate did something like this, but with mob entities. Maybe he could help :)
     
  4. Offline

    chasechocolate

    This is what I originally used:
    Code:java
    1. private LivingEntity getEntityInLineOfSight(Player player){
    2. Block targetBlock = player.getTargetBlock(null, 50);
    3. Location blockLoc = targetBlock.getLocation();
    4. double bx = blockLoc.getX();
    5. double by = blockLoc.getY();
    6. double bz = blockLoc.getZ();
    7. List<Entity> nearby = player.getNearbyEntities(100, 100, 100);
    8.  
    9. for(Entity entity : nearby){
    10. if(entity instanceof LivingEntity){
    11. Location loc = entity.getLocation();
    12. double ex = loc.getX();
    13. double ey = loc.getY();
    14. double ez = loc.getZ();
    15.  
    16. if((bx - 1.5 <= ex && ex <= bx + 2) && (bz - 1.5 <= ez && ez <= bz + 2) && (by - 1 <= ey && ey <= by + 2.5)){
    17. return (LivingEntity) entity;
    18. }
    19. }
    20. }
    21.  
    22. return null;
    23. }

    However, this sometimes will not work. Using Comphenix 's Vector3D class (from this: http://forums.bukkit.org/threads/allow-invisible-hidden-players-to-be-attacked.114668/), you can do it this way:
    Code:java
    1. private Entity getEntityInSight(Player player){
    2. Location playerLoc = player.getLocation();
    3. Vector3D playerDirection = new Vector3D(playerLoc.getDirection());
    4. Vector3D start = new Vector3D(playerLoc);
    5. Vector3D end = start.add(playerDirection.multiply(scanDistance));
    6. Entity inSight = null;
    7.  
    8. for(Entity nearbyEntity : player.getNearbyEntities(scanDistance, scanDistance, scanDistance)){
    9. Vector3D nearbyLoc = new Vector3D(nearbyEntity.getLocation());
    10.  
    11. //Bounding box
    12. Vector3D min = nearbyLoc.subtract(0.5D, 0, 0.5D);
    13. Vector3D max = nearbyLoc.add(0.5D, 1.67D, 0.5D);
    14.  
    15. if(hasIntersection(start, end, min, max)){
    16. if(inSight == null || inSight.getLocation().distanceSquared(playerLoc) > nearbyEntity.getLocation().distanceSquared(playerLoc)){
    17. inSight = nearbyEntity;
    18. }
    19. }
    20. }
    21.  
    22. return inSight;
    23. }
    24.  
    25. private boolean hasIntersection(Vector3D start, Vector3D end, Vector3D min, Vector3D max) {
    26. final double epsilon = 0.0001f;
    27.  
    28. Vector3D d = end.subtract(start).multiply(0.5);
    29. Vector3D e = max.subtract(min).multiply(0.5);
    30. Vector3D c = start.add(d).subtract(min.add(max).multiply(0.5));
    31. Vector3D ad = d.abs();
    32.  
    33. if(Math.abs(c.getX()) > e.getX() + ad.getX()){
    34. return false;
    35. }
    36.  
    37. if(Math.abs(c.getY()) > e.getY() + ad.getY()){
    38. return false;
    39. }
    40.  
    41. if(Math.abs(c.getZ()) > e.getX() + ad.getZ()){
    42. return false;
    43. }
    44.  
    45. if(Math.abs(d.getY() * c.getZ() - d.getZ() * c.getY()) > e.getY() * ad.getZ() + e.getZ() * ad.getY() + epsilon){
    46. return false;
    47. }
    48.  
    49. if(Math.abs(d.getZ() * c.getX() - d.getX() * c.getZ()) > e.getZ() * ad.getX() + e.getX() * ad.getZ() + epsilon){
    50. return false;
    51. }
    52.  
    53. if(Math.abs(d.getX() * c.getY() - d.getY() * c.getX()) > e.getX() * ad.getY() + e.getY() * ad.getX() + epsilon){
    54. return false;
    55. }
    56.  
    57. return true;
    58. }
     
    SnowGears, Comphenix and xWatermelon like this.
Thread Status:
Not open for further replies.

Share This Page