Jump to content

Entity Looking at Player


charsmud

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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