Solved Small Problem with Getting Entities in FOV

Discussion in 'Plugin Development' started by TheHandfish, Aug 11, 2014.

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

    TheHandfish

    Hi,

    Before you say anything, I am well aware that with hacked clients and FOV-modification, it's impossible to get the exact field of view. But I don't really mind.

    Here's my dilemma. I have a code that detects if another player is within my field of view. It sort of works, but it seems really inaccurate. So I'm wondering if someone who is pretty good at math can take a look at this and see if I could be doing this better. Right now, at certain angles it will say there is no entity in my field of view. If I turn around, sometimes it also says the player is in my field of view. Bottom line, it's pretty flawed. :p

    Step back, Math is beyond this point. If you're not ready to quit Summer, that's okay, I understand. But for those of you eager to learn or use what you've learned, do take a look.

    Code:Java
    1.  
    2. public HashMap<LivingEntity, Double> leVector(Player p)
    3. {
    4. ArrayList<LivingEntity> living = new ArrayList<>(); // This is where we'll save all the living entities that are nearby.
    5. for(Entity e : p.getNearbyEntities(Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16))
    6. {
    7. if(e instanceof LivingEntity && !e.equals(p))
    8. {
    9. living.add((LivingEntity) e);
    10. }
    11. }
    12.  
    13.  
    14. HashMap<LivingEntity, Double> toreturn = new HashMap<>(); // This is where we'll save
    15.  
    16. for(LivingEntity near : living)
    17. {
    18. Location nearloc = near.getLocation();
    19. Location ploc = p.getLocation();
    20. /*Vector tovec = near.getLocation().toVector().normalize();
    21.   Vector dir = near.getLocation().getDirection().normalize();
    22.  
    23.   double dotproduct = tovec.dot(dir);
    24.   mab = Math.abs(dotproduct);*/
    25. double prod = Math.abs(nearloc.subtract(ploc).toVector().normalize().dot(p.getLocation().getDirection().normalize()));
    26. toreturn.put(near, prod);
    27. }
    28.  
    29. return toreturn;
    30. }
    31.  
     
  2. Offline

    hintss

    just check how far it's off from the player's look direction in the pitch and yaw directions using inverse trig?
     
  3. Offline

    TheHandfish

    Could you explain a bit more? Keep on mind I have tried a lot of things here.

    Bump.

    Bah, bit the bullet and went with a check that sees if the player is near your crosshairs. At least it works.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page