Jump to content

Entity Looking at Player


charsmud

Recommended Posts

Hello.  I am trying to detect whether a player is visible by an entity;  I have fiddled with the Enderman code but to no avail, as that code only detects if the enderman is seen by the player.  Is there a way to detect this? 

Link to comment
Share on other sites

Hi

 

Depending on what you want to do, it might be fine to assume that the entity can see the player if (and only if) the player can see the entity.

 

If you're not happy with that, check the vanilla

EntitySenses

and

EntityLivingBase.canEntityBeSeen

for inspiration...

 

-TGG

Link to comment
Share on other sites

That assumption won't work for me, but I'll look into the EntitySenses and canEntityBeSeen methods. 

 

After experimentation, I've discovered that canEntityBeSeen does not use the direction that the entity is facing:  It just raytraces from the entity to the target entity, and assumes it is seen.  Is there a way to modify this to get it to work for my needs?

Link to comment
Share on other sites

Now see, you are changing you requirements.  First, you asked if the player was visible - TGG told you how to find that. Now, you want to know if the entity is actually looking at the player - another thing entirely.

Link to comment
Share on other sites

Hi

You can get the direction that the entity is looking in using

 

EntityLivingBase::
    /**
     * interpolated look vector
     */
    public Vec3 getLook(float partialTick)

 

I would suggest the following algorithm:

(1) calculate the displacement of the player relative to the entity (i.e. create a Vec3 of [playerx-entityx, playery - entityy, playerz-entityz].  You can get their positions using EntityLivingBase.getPosition(partialTick).

(2) Calculate the dot product of the displacement vector with the look vector

(use displacementVector.dotProduct(lookVector)).  If the dot product is negative, the two vectors are pointing in opposite directions and so the entity is facing the wrong way to see the player.

(3) if the dot product is positive, calculate the canEntityBeSeen method to check whether there are walls etc in the way.

 

If you want to narrow the entity's field of vision below 180 degrees, perform normalize on the two vectors first, and compare it to the cosine of half the angle of vision.

 

For example: you want the field of vision to be 120 degrees.

cosine of 120/2 degrees is 0.5.

So

dotProduct = displacementVector.normalize().dotProduct(lookVector.normalize());
if (dotProduct >= 0.5) { // he's looking at me

 

-TGG

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.