Jump to content

[Solved]How do I properly use the Frustum class?


Recommended Posts

I am making a mod that warns you if mobs are coming up behind you, and to do that properly, I need to know how to correctly use the view Frustum. The code I currently have is extremely unhelpful in that it does absolutely nothing when you face away from the entity and intermittently turns on.

There is a creeper direcly behind me in both screenshots:



My Frustum checking code:


	public void renderEntityWarnings(RenderGameOverlayEvent.Pre event) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft());
		if (event.getType() == ElementType.CHAT || player == null || player.world == null) return;
		List<Entity> ewl = player.world.getEntitiesWithinAABBExcludingEntity(player, player.getEntityBoundingBox().grow(Config.Warnings.range, 5, Config.Warnings.range));
		List<EntityLivingBase> entl = new ArrayList();
		for (Entity e : ewl) {
			if (e instanceof EntityLivingBase) {
				Frustum fr = new Frustum();
				fr.setPosition(player.posX, player.posY, player.posZ);
				if  (!fr.isBoundingBoxInFrustum(e.getRenderBoundingBox())) entl.add((EntityLivingBase) e);
			} else {
				//if (e instanceof EntityArrow) {
				//	GlStateManager.pushMatrix();
				//	Minecraft.getMinecraft().getRenderItem().renderItem(new ItemStack(), cameraTransformType);
				//	GlStateManager.popMatrix();
		EntityLivingBase entity = getClosestLiving(entl, player);
		if (entity != null) {
			if (Config.Warnings.showModel) {
				EntityDisplay ed = new EntityDisplay(Minecraft.getMinecraft());
				ed.setPosition(sr.getScaledWidth() / 2 - 20, sr.getScaledHeight() / 10);
				GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
			String behind = I18n.format("highlighter.message.behind");
			Minecraft.getMinecraft().fontRenderer.drawString(behind, (sr.getScaledWidth() / 2) - (Minecraft.getMinecraft().fontRenderer.getStringWidth(behind) / 2), sr.getScaledHeight() / 10 - 20, Color.RED.getRGB());
			Minecraft.getMinecraft().fontRenderer.drawString(entity.getName(), (sr.getScaledWidth() / 2) - (Minecraft.getMinecraft().fontRenderer.getStringWidth(entity.getName()) / 2), sr.getScaledHeight() / 10 - 10, Color.WHITE.getRGB());
			String dist = Integer.toString((int) player.getDistance(entity)) + " " + I18n.format("highlighter.message.blocks_away");
			Minecraft.getMinecraft().fontRenderer.drawString(dist, (sr.getScaledWidth() / 2) - (Minecraft.getMinecraft().fontRenderer.getStringWidth(dist) / 2), sr.getScaledHeight() / 10 + (Config.Warnings.showModel ? 40 : 0), Color.WHITE.getRGB());



The entire point of this mod was to stop creepers from sneaking up behind me when I'm battleing lots of mobs :P

If you can help, Thanks! :)

Edited by GiantNuker
Link to comment
Share on other sites

Normally when I do this sort of thing I use the player's look vector instead. Basically every entity has a look vector which represents a "normalized" ray extending from the eyes. You can use vector math to determine whether the relative position is of interest for your needs. For example, a dot product function is available to indicate how much of another vector contains similar direction. So for example, you could create a vector from the other entity to the player and normalize it then dot product that with the player look vector. The result will tell you a number between -1 and 1 which represents from directly in front of you (-1) to directly behind you (1) to on a side (0) and everything in between. You might have to play around with it a bit, but basically something like anything greater than -0.2 or something would probably be out of sight.


If dot product and other vector math is not familiar to you, you can use normal trigonometry instead -- basically vector math is just simple way of doing the trig for you. So you can calculate the angle from the entity to the player and compare that to the angle that the player's look vector represents. If you draw a sketch you can probably figure it out pretty quick.

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

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.

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.


  • Create New...

Important Information

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