# Cone-View Question

Discussion in 'Plugin Development' started by TheHandfish, Jun 24, 2014.

Not open for further replies.
1. Offline

### TheHandfish

Hi,

I'm designing a plugin that has to do with whether or not the player can see other players. This does not me if his crosshair is over the player, it means if anywhere on the screen a player can be seen. I'm thinking I'll need to make some kind of cone that extends out (probably really far). I did do some Googling, and I did find a few methods, such as this one:

Code:Java
`  import java.util.ArrayList;import java.util.List;import org.bukkit.Location;import org.bukkit.entity.Player; public class Cone{    public static List<Player> getPlayersInCone(List<Player> players, Location startLoc, int radius, int degrees, int direction)     {        List<Player> newPlayers = new ArrayList<>();         int[] startPos = new int[] { (int)startLoc.getX(), (int)startLoc.getZ() };         int[] endA = new int[] { (int)(radius * Math.cos(direction - (degrees/2))), (int)(radius * Math.sin(direction - (degrees/2))) };        int[] endB = new int[] { (int)(radius * Math.cos(direction + (degrees/2))), (int)(radius * Math.sin(direction + (degrees/2))) };         for(Player p :players) {            Location l = p.getLocation();            if (!isPointInCircle(startPos[0], startPos[1], radius, l.getBlockX(), l.getBlockZ())) {                continue;            }            int [] playerVector = getVectorForPoints(startPos[0], startPos[1], l.getBlockX(), l.getBlockZ());             double angle = getAngleBetweenVectors(endA, playerVector) + getAngleBetweenVectors(endB, playerVector);            if (Math.abs(angle) * 10 - 2 < degrees) {                newPlayers.add(p);            }        }        return newPlayers;    }          private static boolean isPointInCircle(int cx, int cy, int radius, int px, int py)         {            double dist = (px-cx)^2 + (py-cy)^2;            return dist < (radius^2);        }     private static int[] getVectorForPoints(int x1, int y1, int x2, int y2)     {        return new int[]         {             Math.abs(x2-x1), Math.abs(y2-y1)         };    }         private static double getAngleBetweenVectors(int[] vector1, int[] vector2)        {        return Math.atan2(vector2[1], vector2[0]) - Math.atan2(vector1[1], vector1[0]);        } }`

I tried this:

Code:text
`    public List<Player> getPlayersInCone(Player p)    {        List<Player> players = null;        for(Entity e : p.getNearbyEntities(Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16))        {            if(e instanceof Player && !e.equals(p))            {                players.add((Player) e);            }        }        return Cone.getPlayersInCone(players, p.getLocation(), Bukkit.getViewDistance(), p.getLocation().getYaw());    } `

I get the feeling I have no clue what I'm doing, so I don't bother testing it. So, question: Is this a good way to do it? Am I doing it wrong?
Is there a better way to do it?

#1
2. Offline

### fireblast709

TheHandfish Isn't it easier to just take the Vector from your location to the other, and the direction Vector. Then just normalize both and use the dot product, which will give you the cosine of the angle between the cursor and the player

#2
jthort likes this.
3. Offline

### TheHandfish

fireblast709: So, would it be something like:

Code:java
` public List<Player> getPlayersInCone(Player p){ArrayList<Player> players = new ArrayList<>();for(Entity e : p.getNearbyEntities(Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16)){if(e instanceof Player && !e.equals(p)){players.add((Player) e);}} List<Player> toreturn = null; for(Player near : players){Vector tovec = near.getLocation().toVector().normalize();Vector dir = near.getLocation().getDirection().normalize(); double dotproduct = tovec.dot(dir);} return toreturn;} `

Does this look right so far? What else should I do? (I'm not awesome with Vectors x_x)

#3
4. Offline

### fireblast709

TheHandfish subtract near's location with p's location, and use p's direction

#4
5. Offline

### TheHandfish

fireblast709:

Code:text
` Location prod = near.getLocation().subtract(p.getLocation()); `

How do you mean "use P's direction?"

EDIT: Or more like:

Code:java
`            prod = near.getLocation().subtract(p.getLocation()).toVector().normalize().dot(p.getLocation().getDirection().normalize()); `

UPDATE:

Never mind, figured it out.

Code:java
`    public HashMap<LivingEntity, Double> leVector(Player p)    {        ArrayList<LivingEntity> living = new ArrayList<>(); // This is where we'll save all the living entities that are nearby.        for(Entity e : p.getNearbyEntities(Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16, Bukkit.getViewDistance() * 16))        {            if(e instanceof LivingEntity && !e.equals(p))            {                living.add((LivingEntity) e);            }        }          HashMap<LivingEntity, Double> toreturn = new HashMap<>(); // This is where we'll save         for(LivingEntity near : living)        {            /*Vector tovec = near.getLocation().toVector().normalize();            Vector dir = near.getLocation().getDirection().normalize();                        double dotproduct = tovec.dot(dir);            mab = Math.abs(dotproduct);*/            double prod = Math.abs(near.getLocation().subtract(p.getLocation()).toVector().normalize().dot(p.getLocation().getDirection().normalize()));            toreturn.put(near, prod);        }         return toreturn;    }`

Thanks for the help!

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 9, 2016
#5
6. Offline

### blablubbabc

Keep in mind that clients can theoretically use different field of views.

You can check if the other player is behind the player by checking if the dot product between the players view direction and the normalized vector towards the other player is smaller than zero:
Vector viewDirection = player.getDirection();
Vector direction = otherPlayer.getLocation().subtract(player.getLocation()).toVector().normalize();
if (viewDirection.dot(direction) > 0)
{ /* otherPlayer is somwhere in front of the player */ }

#6
7. Offline

### TheHandfish

Thanks! My solution works pretty well, and keep in mind, all that really matters (to me) is if the character himself can see others, not the person controlling him.

I was looking for a way to force first-person view but I don't think that is server-side.

#7
8. Offline

### fireblast709

Which is pretty much where I ninja'd you in the first reply I gave :3

#8
TheHandfish likes this.