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



    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.

    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. }
    14. HashMap<LivingEntity, Double> toreturn = new HashMap<>(); // This is where we'll save
    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();
    23.   double dotproduct =;
    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. }
    29. return toreturn;
    30. }
  2. Offline


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


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


    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