public static float signAngle(Vec3d a, Vec3d b, Vec3d n) { Vec3d cross = a.crossProduct(b); double s = cross.length(); double c = a.dotProduct(b); double angle = MathHelper.atan2(s, c); if (n != null) { if (n.dotProduct(cross) < 0) { angle = -angle; } } return (float) Math.toDegrees(angle); }
public void rotateCameraYawTo(Vec3d dir, float offset) { double yaw = Math.acos(new Vec3d(-1, 0, 0).dotProduct(dir)); Vec3d cross = new Vec3d(-1, 0, 0).crossProduct(dir); Vec3d up = new Vec3d(0, -1, 0); if (up.dotProduct(cross) < 0) { yaw = -yaw; } yaw = Math.PI + yaw; setCameraYaw((float) Math.toDegrees(yaw) + offset); }
public static void rotateTowards(Vec3d from, Vec3d to, Vec3d up) { double dot = from.dotProduct(to); if (Math.abs(dot - (-1.0)) < Double.MIN_VALUE) { GlStateManager.rotate(180, (float) up.x, (float) up.y, (float) up.z); } if (Math.abs(dot - (1.0)) < Double.MIN_VALUE) { return; } double rotAngle = Math.acos(dot); Vec3d rotAxis = from.crossProduct(to).normalize(); GlStateManager.rotate((float) (rotAngle * (180d / Math.PI)), (float) rotAxis.x, (float) rotAxis.y, (float) rotAxis.z); }
boolean intersectsAnomaly(Vec3d origin, Vec3d dir, Vec3d anomaly, double radius) { if (origin.distanceTo(anomaly) <= radius) { return true; } else { Vec3d intersectDir = origin.subtract(anomaly); double c = intersectDir.lengthVector(); double v = intersectDir.dotProduct(dir); double d = radius * radius - (c * c - v * v); return d >= 0; } }
/** * Checks if the target entity is in the field of view (180 degree) of the base entity. Only works reliable for players (due to server-client sync) * * @param entity * @param target * @param alsoRaytrace Raytraces first * @return */ public static boolean canReallySee(EntityLivingBase entity, EntityLivingBase target, boolean alsoRaytrace) { if (alsoRaytrace && !entity.canEntityBeSeen(target)) { return false; } Vec3d look1 = new Vec3d(-Math.sin(entity.rotationYawHead / 180 * Math.PI), 0, Math.cos(entity.rotationYawHead / 180 * Math.PI)); Vec3d dist = new Vec3d(target.posX - entity.posX, 0, target.posZ - entity.posZ); //look1.yCoord = 0; look1 = look1.normalize(); dist = dist.normalize(); //Check if the vector is left or right of look1 double alpha = Math.acos(look1.dotProduct(dist)); return alpha < Math.PI / 1.8; }
protected void handleRicochets(RayTraceResult hit) { Vec3d motion = new Vec3d(this.motionX, this.motionY, this.motionZ); Vec3d surfaceNormal = new Vec3d(hit.sideHit.getDirectionVec()); double deflectDot = surfaceNormal.dotProduct(motion) * 2; Vec3d projection = new Vec3d(surfaceNormal.x * deflectDot, surfaceNormal.y * deflectDot, surfaceNormal.z * deflectDot); Vec3d deflectDir = motion.subtract(projection); this.motionX = deflectDir.x; this.motionY = deflectDir.y; this.motionZ = deflectDir.z; this.distanceTraveled += 2; canHurtCaster = true; }
private static double getLineDistance( final Vec3d v, final Vec3d w, final EntityPlayer player, final float partialTicks ) { final Vec3d p = player.getPositionEyes( partialTicks ); final double segmentLength = v.squareDistanceTo( w ); if ( segmentLength == 0.0 ) { return p.distanceTo( v ); } final double t = Math.max( 0, Math.min( 1, p.subtract( v ).dotProduct( w.subtract( v ) ) / segmentLength ) ); final Vec3d projection = v.add( w.subtract( v ).scale( t ) ); return p.distanceTo( projection ); }
@Override public void redirectPacket(final Vec3d position, final Vec3d direction, final int addedLifetime) { lifetime += addedLifetime; if (lifetime > 0) { // Revive! revive(); // Apply new position. final Vec3d oldPos = getPositionVector(); final Vec3d delta = position.subtract(oldPos); final double sqrDelta = delta.dotProduct(delta); if (sqrDelta > TRAVEL_SPEED * TRAVEL_SPEED) { // Clamp to an area where we won't get weird effects due to // the later adjustment of the position to compensate for // manual movement (see `checkCollisions`). final double normalizer = TRAVEL_SPEED * TRAVEL_SPEED / sqrDelta; posX = position.xCoord * normalizer; posY = position.yCoord * normalizer; posZ = position.zCoord * normalizer; } else { posX = position.xCoord; posY = position.yCoord; posZ = position.zCoord; } // Apply new direction. final Vec3d motionVec = direction.normalize(); motionX = motionVec.xCoord * TRAVEL_SPEED; motionY = motionVec.yCoord * TRAVEL_SPEED; motionZ = motionVec.zCoord * TRAVEL_SPEED; } }
public static boolean isEntityLookingAt(EntityLivingBase looker, EntityLivingBase seen, double degree) { Vec3d vec3d = looker.getLook(1.0F).normalize(); Vec3d vec3d1 = new Vec3d(seen.posX - looker.posX, seen.getEntityBoundingBox().minY + (double) seen.getEyeHeight() - (looker.posY + (double) looker.getEyeHeight()), seen.posZ - looker.posZ); double d0 = vec3d1.length(); vec3d1 = vec3d1.normalize(); double d1 = vec3d.dotProduct(vec3d1); return d1 > 1.0D - degree / d0; }
public boolean canBlockDamageSource(EntityLivingBase player, DamageSource source) { if (!source.isUnblockable() && player.isActiveItemStackBlocking()) { Vec3d sourceLocation = source.getDamageLocation(); if (sourceLocation != null) { Vec3d playerLook = player.getLook(1.0F); Vec3d sourceToPlayer = sourceLocation.subtractReverse(new Vec3d(player.posX, player.posY, player.posZ)) .normalize(); sourceToPlayer = new Vec3d(sourceToPlayer.x, 0.0, sourceToPlayer.z); if (sourceToPlayer.dotProduct(playerLook) < 0.0D) { return true; } } } return false; } }
public static boolean isEntityLookingAt(EntityLivingBase looker, EntityLivingBase seen, double degree) { degree *= 1 + (looker.getDistance(seen) * 0.1); Vec3d vec3d = looker.getLook(1.0F).normalize(); Vec3d vec3d1 = new Vec3d(seen.posX - looker.posX, seen.getEntityBoundingBox().minY + (double) seen.getEyeHeight() - (looker.posY + (double) looker.getEyeHeight()), seen.posZ - looker.posZ); double d0 = vec3d1.length(); vec3d1 = vec3d1.normalize(); double d1 = vec3d.dotProduct(vec3d1); return d1 > 1.0D - degree / d0 ? looker.canEntityBeSeen(seen) && !isStoneMob(seen) : false; }
/** * Checks to see if this enderman should be attacking this player */ public boolean shouldAttackPlayer(EntityPlayer player) { ItemStack itemstack = player.inventory.armorInventory.get(3); // 3: Helmet, 2: Chestpiece, 1: Legs, 0: Boots if (itemstack.getItem() == Item.getItemFromBlock(Blocks.PUMPKIN)) { return false; } else { Vec3d relativePlayerEyePos = new Vec3d(posX - player.posX, getEntityBoundingBox().minY + height / 2.0F - (player.posY + player.getEyeHeight()), posZ - player.posZ); double distance = relativePlayerEyePos.lengthVector(); relativePlayerEyePos = relativePlayerEyePos.normalize(); // NB: inverse of normal enderman, attack when this guy looks at the player instead of the other // way around Vec3d lookVec = getLook(1.0F).normalize(); double dotTangent = -lookVec.dotProduct(relativePlayerEyePos); return dotTangent > 1.0D - 0.025D / distance; } }
/** * Checks to see if this enderman should be attacking this player */ private boolean shouldAttackPlayer(EntityPlayer player) { ItemStack itemstack = player.inventory.armorInventory.get(0); if (itemstack.getItem() == Item.getItemFromBlock(Blocks.PUMPKIN)) { return false; } else { Vec3d vec3d = player.getLook(1.0F).normalize(); Vec3d vec3d1 = new Vec3d(enderman.posX - player.posX, enderman.getEntityBoundingBox().minY + enderman.getEyeHeight() - (player.posY + player.getEyeHeight()), enderman.posZ - player.posZ); double d0 = vec3d1.lengthVector(); vec3d1 = vec3d1.normalize(); double d1 = vec3d.dotProduct(vec3d1); return d1 > 1.0D - 0.025D / d0 && player.canEntityBeSeen(enderman); } } }
/** * Checks to see if this enderman should be attacking this player */ private boolean shouldAttackPlayer(EntityPlayer player) { ItemStack itemstack = player.inventory.armorInventory.get(0); if (itemstack.getItem() == Item.getItemFromBlock(Blocks.PUMPKIN)) { return false; } else { Vec3d vec3d = player.getLook(1.0F).normalize(); Vec3d vec3d1 = new Vec3d(enderman.posX - player.posX, enderman.getEntityBoundingBox().minY + enderman.getEyeHeight() - (player.posY + player.getEyeHeight()), enderman.posZ - player.posZ); double d0 = vec3d1.lengthVector(); vec3d1 = vec3d1.normalize(); double d1 = vec3d.dotProduct(vec3d1); return d1 > 1.0D - 0.025D / d0 && player.canEntityBeSeen(enderman); } } }
Vec3d dir = norm.rotatePitch(pitchOff).rotateYaw(yawOff).normalize(); newPoint = dir.scale(norm.dotProduct(diff) / norm.dotProduct(dir)).add(bolt.getData());
vec3d2 = new Vec3d(vec3d2.x, 0.0D, vec3d2.z); Vec3d vec3d3 = (new Vec3d(vec3d1.x - vec3d.x, 0.0D, vec3d1.z - vec3d.z)).normalize().rotateYaw(((float)Math.PI / 2F)); double d1 = vec3d2.dotProduct(vec3d3); this.Eye.rotationPointX = MathHelper.sqrt((float)Math.abs(d1)) * 2.0F * (float)Math.signum(d1);
Vec3d mapPosition = new Vec3d(starMap.getPos().getX() + 0.5, 0, starMap.getPos().getZ() + 0.5); Vec3d dir = mapPosition.subtract(playerPosition).normalize(); double angle = Math.acos(dir.dotProduct(new Vec3d(1, 0, 0))); if (new Vec3d(0, 1, 0).dotProduct(dir.crossProduct(new Vec3d(1, 0, 0))) < 0) { angle = Math.PI * 2 - angle;
avec3d[l] = vec3d.scale(2.0D * avec3d[l].dotProduct(vec3d)).add(avec3d[l].scale((f9 * f9) - vec3d.dotProduct(vec3d))).add(vec3d.crossProduct(avec3d[l]).scale((2.0F * f9)));
avec3d[l] = vec3d.scale(2.0D * avec3d[l].dotProduct(vec3d)).add(avec3d[l].scale((double) (f9 * f9) - vec3d.dotProduct(vec3d))).add(vec3d.crossProduct(avec3d[l]).scale((double) (2.0F * f9)));
avec3d[l] = vec3d.scale(2.0D * avec3d[l].dotProduct(vec3d)).add(avec3d[l].scale((double) (f9 * f9) - vec3d.dotProduct(vec3d))).add(vec3d.crossProduct(avec3d[l]).scale((double) (2.0F * f9)));