# Solved Get all entities that are in line of sight/aren't behind a wall

Discussion in 'Plugin Development' started by MisterErwin, Jun 8, 2013.

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

### MisterErwin

Hello together,

how can I get a list of entities that are "visible" (aren't behind a wall) to a player .

Thanks,
MisterErwin

#1
2. Offline

### CubieX

You could use ".getNearbyEntities()" to get all entities around the player in a specified radius.
And then you could loop though all of these entities and check if "player.hasLineOfSight(entity)"

#2
3. Offline

### nitrousspark

Code:
```
public static ArrayList<Entity> getEntitiesBetweenPlayerAndBlockLookingAt(Player player)
{
Location loc = player.getEyeLocation();

ArrayList<Entity> entitylist = new ArrayList<Entity>();

double px = loc.getX();
double py = loc.getY();
double pz = loc.getZ();

double yaw = Math.toRadians(loc.getYaw() + 90);
double pitch = Math.toRadians(loc.getPitch() + 90);

double x = Math.sin(pitch) * Math.cos(yaw);
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);

for(int i = 1; i <= 70; i++)
{
Location loc1 = new Location(player.getWorld(), px + i * x, py + i * z, pz + i * y);
for(Entity e : this.entitiesDistanceSquared(loc1, 3))
{
entitylist.add(e);
}
}
return entitylist;
}

public ArrayList<Entity> entitiesDistanceSquared(Location loc, double radius)
{

ArrayList<Entity> entities = new ArrayList<Entity>();

double i1 = loc.getX();
double j1 = loc.getY();
double k1 = loc.getZ();

for(Entity e : loc.getWorld().getEntities())
{

if(e.getWorld().equals(loc.getWorld()))
{

double i2 = e.getLocation().getX();
double j2 = e.getLocation().getY();
double k2 = e.getLocation().getZ();

double ad = (i2 - i1) * (i2 - i1) + (j2 - j1) * (j2 - j1) + (k2 - k1) * (k2 - k1);

if(ad < radius)
{
entities.add(e);
}

}

}

return entities;

}```

#3
CubieX likes this.
4. Offline

### CubieX

I don't know if he wants to find all mobs in "real" LOS (where the player looks),
or just mobs that are not visually blocked.
The question seemed to request the latter.
But I'm sure MisterErwin will tell us.

#4
5. Offline

### MisterErwin

Yes....
Only them that are not visually blocked, not only them in the line of sight.

My "tries" won't work really

And nitrousspark: Thanks, but.... I didn't asked precisely

#5
6. Offline

#6
7. Offline

### CubieX

Well, I guess using ".hasLineOfSight()" will do the trick with less hassle,
if he really does not need to check for the players current field of view.

#7
8. Offline

### MisterErwin

blablubbabc: AND CubieX: Both true - I totally forgot that.
For my currently Project is CubieX's solution better, but I need blablubbabc's for another Project

And now... I have to find a new location on my wall to smash my head against it

And I've repainted it only before a week ;(

#8
Thread Status:
Not open for further replies.