Posted March 23, 201411 yr 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?
March 23, 201411 yr 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
March 23, 201411 yr Author 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?
March 24, 201411 yr 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. -S- (if I helped, please click Thank and applaud) http://6upnqa.dm2301.livefilestore.com/y2mtf-vG7Tqq1TiiVpIm53KWj7294NDPoHfSHHb4PzZiMAUfRCfK0UY0MwOu7Q3zTBNVTKqWjr2-xgBfFRpQT5p-QivtvknPpoABMNUw9br9WuZcBFkjePhnAbW500gVm-P/sequiturian.png[/img]
March 24, 201411 yr 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
March 25, 201411 yr Author Thanks for that great explanation! I should hopefully be able to get that implemented sometime soon!
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.