Jump to content

Recommended Posts

Posted

I have a code that detect if the mob is in the player field of view and can be seen.

public void onUpdate()
{
	super.onUpdate();

	 EntityPlayer ep = Minecraft.getMinecraft().thePlayer;

	if(isInFieldOfVision(this, ep) && canEntityBeSeen(ep)){ //The problem
		this.worldObj.playSoundEffect(this.posX, this.posY, this.posZ, "fluffy:grunt.Enabled", 2.0F, 1.0F);
	}
}

protected boolean isInFieldOfVision(EntityLiving e1, EntityLiving e2){
	//save Entity 2's original rotation variables
	float rotationYawPrime = e2.rotationYaw;
	float rotationPitchPrime = e2.rotationPitch;
	//make Entity 2 directly face Entity 1
	e2.faceEntity(e1, 360F, 360F);
	//switch values of prime rotation variables with current rotation variables
	float f = e2.rotationYaw;
	float f1 = e2.rotationPitch;
	e2.rotationYaw = rotationYawPrime;
	e2.rotationPitch = rotationPitchPrime;
	rotationYawPrime = f;
	rotationPitchPrime = f1;
	//assuming field of vision consists of everything within X degrees from rotationYaw and Y degrees from rotationPitch, check if entity 2's current rotationYaw and rotationPitch within this X and Y range
	float X = 60F;
	float Y = 60F;
	float yawFOVMin = e2.rotationYaw - X;
	float yawFOVMax = e2.rotationYaw + X;
	float pitchFOVMin = e2.rotationPitch - Y;
	float pitchFOVMax = e2.rotationPitch + Y;
	boolean flag1 = (yawFOVMin < 0F && (rotationYawPrime >= yawFOVMin + 360F || rotationYawPrime <= yawFOVMax)) || (yawFOVMax >= 360F && (rotationYawPrime <= yawFOVMax - 360F || rotationYawPrime >= yawFOVMin)) || (yawFOVMax < 360F && yawFOVMin >= 0F && rotationYawPrime <= yawFOVMax && rotationYawPrime >= yawFOVMin);
	boolean flag2 = (pitchFOVMin <= -180F && (rotationPitchPrime >= pitchFOVMin + 360F || rotationPitchPrime <= pitchFOVMax)) || (pitchFOVMax > 180F && (rotationPitchPrime <= pitchFOVMax - 360F || rotationPitchPrime >= pitchFOVMin)) || (pitchFOVMax < 180F && pitchFOVMin >= -180F && rotationPitchPrime <= pitchFOVMax && rotationPitchPrime >= pitchFOVMin);
	if(flag1 && flag2 && e2.canEntityBeSeen(e1))
		return true;
	else return false;
}

 

The problem is only EntityLiving has .faceEntity method and i can't cast EntityPlayer to EntityLiving coz they are from EntityLivingBase.

It gives me an error if i use EntityPlayer rather than EntityLiving.

Thanks for helping me out!

Posted

Well... You can create your own faceEntity method?

You would have to use EntityClientPlayerMP to change the yaw and pitch though.

    /**
     * Arguments: new yaw, new pitch.
     */
    private void setPlayerRotation(float yaw, float pitch) {
    	Minecraft mc = Minecraft.getMinecraft();
    	EntityClientPlayerMP ecpmp = mc.thePlayer;
    	ecpmp.rotationYaw = yaw;
    	ecpmp.rotationPitch = pitch;
    }

Posted

EntityLivingBase has a method called canEntityBeSeen() which should do the job.  The code for that class is:

    /**
     * returns true if the entity provided in the argument can be seen. (Raytrace)
     */
    public boolean canEntityBeSeen(Entity par1Entity)
    {
        return this.worldObj.rayTraceBlocks(this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY + (double)this.getEyeHeight(), this.posZ), this.worldObj.getWorldVec3Pool().getVecFromPool(par1Entity.posX, par1Entity.posY + (double)par1Entity.getEyeHeight(), par1Entity.posZ)) == null;
    }

 

However, while the above method is good enough for AI (like for a mob to notice player) I'm not sure it technically finds all cases where a player could possibly see something.  To check if something is being rendered to the player, you might need to consider the "frustum" (http://en.wikipedia.org/wiki/Frustum) which is how the rendering determines that something is possibly in camera view so needs to be rendered.  You might want to consider this method in the Frustrum class (notice that Frustrum class name is misspelled with extra "r").  Here is the method that might be interesting (it checks that a bounding box, which could be for an entity, is in the camera view).

    /**
     * Returns true if the bounding box is inside all 6 clipping planes, otherwise returns false.
     */
    public boolean isBoundingBoxInFrustum(AxisAlignedBB par1AxisAlignedBB)
    {
        return this.isBoxInFrustum(par1AxisAlignedBB.minX, par1AxisAlignedBB.minY, par1AxisAlignedBB.minZ, par1AxisAlignedBB.maxX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.maxZ);
    }

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

I can create my own faceEntity method coz some methods in EntityLiving is private.

I don't want to use Acess Transformer.

.canEntityBeSeen is just raytraceing between two entity, checking if there is any block.

Anyway thanks for trying to help me.

Posted

I can't create my own faceEntity method coz some methods in EntityLiving is private.

Sorry, but that is bs.

All you have to do is change the rotation yaw + pitch.

Those fields exists in EntityLivingBase. You can literally copy the EntityLookHelper class and let it pass EntityLivingBase or EntityPlayer in the constructor's parameter.

Posted

What about the frustum stuff I recommended?  That code exactly determines what is shown on the player screen.

 

I don't think faceEntity is the right code for what you're looking for -- that code just turns the head to make an entity to look in the right direction.  Do you want to force the player to look at something?  Even if it is facing, don't you care whether the view is blocked?

 

The frustum will tell you whether something is already in the camera view, and the canEntityBeSeen() method will tell you if the view is blocked.  I feel like those are closer to what you're trying to do.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.