GiantNuker Posted August 25, 2018 Posted August 25, 2018 (edited) 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: Spoiler @SubscribeEvent 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.setEntity(entity); ed.setPosition(sr.getScaledWidth() / 2 - 20, sr.getScaledHeight() / 10); GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); ed.draw(); } 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 If you can help, Thanks! Edited August 27, 2018 by GiantNuker Quote
jabelar Posted August 25, 2018 Posted August 25, 2018 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. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
GiantNuker Posted August 26, 2018 Author Posted August 26, 2018 I was using the frustum so it takes your FOV into account. Anyway it seems to be working now, not sure what i tweaked Quote
Recommended Posts
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.