From ec41db1d88fb2a8403a346b1bfd13f63e4144e8e Mon Sep 17 00:00:00 2001 From: redrockruby <43187285+redrockruby@users.noreply.github.com> Date: Sat, 6 Aug 2022 22:16:44 -0400 Subject: [PATCH 1/3] Add files via upload --- FlightComputer.java | 160 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 FlightComputer.java diff --git a/FlightComputer.java b/FlightComputer.java new file mode 100644 index 0000000..c7ed08c --- /dev/null +++ b/FlightComputer.java @@ -0,0 +1,160 @@ +package net.torocraft.flighthud; + +import net.minecraft.block.BlockState; +import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec3d; + +public class FlightComputer { + private static final float TICKS_PER_SECOND = 20; + + private float previousRollAngle = 0.0f; + + public Vec3d velocity; + public float speed; + public float pitch; + public float heading; + public Vec3d flightPath; + public float flightPitch; + public float flightHeading; + public float roll; + public float altitude; + public Integer groundLevel; + public Float distanceFromGround; + public Float elytraHealth; + + public void update(MinecraftClient client, float partial) { + velocity = client.player.getVelocity(); + pitch = computePitch(client, partial); + speed = computeSpeed(client); + roll = computeRoll(client, partial); + heading = computeHeading(client); + altitude = computeAltitude(client); + groundLevel = computeGroundLevel(client); + distanceFromGround = computeDistanceFromGround(client, altitude, groundLevel); + flightPitch = computeFlightPitch(velocity, pitch); + flightHeading = computeFlightHeading(velocity, heading); + elytraHealth = computeElytraHealth(client); + } + + private Float computeElytraHealth(MinecraftClient client) { + ItemStack stack = client.player.getEquippedStack(EquipmentSlot.CHEST); + if (stack != null && stack.getItem() == Items.ELYTRA) { + float remain = ((float) stack.getMaxDamage() - (float) stack.getDamage()) / (float) stack.getMaxDamage(); + return remain * 100f; + } + return null; + } + + private float computeFlightPitch(Vec3d velocity, float pitch) { + if (velocity.length() < 0.01) { + return pitch; + } + Vec3d n = velocity.normalize(); + return (float) (90 - Math.toDegrees(Math.acos(n.y))); + } + + private float computeFlightHeading(Vec3d velocity, float heading) { + if (velocity.length() < 0.01) { + return heading; + } + return toHeading((float) Math.toDegrees(-Math.atan2(velocity.x, velocity.z))); + } + + /** + * Roll logic is from: + * https://github.com/Jorbon/cool_elytra/blob/main/src/main/java/edu/jorbonism/cool_elytra/mixin/GameRendererMixin.java + * to enable both mods will sync up when used together. + */ + private float computeRoll(MinecraftClient client, float partial) { + if (!FlightHud.CONFIG_SETTINGS.calculateRoll) { + return 0; + } + + float wingPower = FlightHud.CONFIG_SETTINGS.rollTurningForce; + float rollSmoothing = FlightHud.CONFIG_SETTINGS.rollSmoothing; + Vec3d facing = client.player.getRotationVecClient(); + Vec3d velocity = client.player.getVelocity(); + double horizontalFacing2 = facing.horizontalLengthSquared(); + double horizontalSpeed2 = velocity.horizontalLengthSquared(); + + float rollAngle = 0.0f; + + if (horizontalFacing2 > 0.0D && horizontalSpeed2 > 0.0D) { + double dot = (velocity.x * facing.x + velocity.z * facing.z) / Math.sqrt(horizontalFacing2 * horizontalSpeed2); + dot = MathHelper.clamp(dot, -1, 1); + double direction = Math.signum(velocity.x * facing.z - velocity.z * facing.x); + rollAngle = (float) (Math.atan(Math.sqrt(horizontalSpeed2) * Math.acos(dot) * wingPower) * direction + * 57.29577951308); + } + + rollAngle = (float) ((1.0 - rollSmoothing) * rollAngle + rollSmoothing * previousRollAngle); + previousRollAngle = rollAngle; + + //Make rollAngle follow terrain + rollAngle = rollAngle * -1; + return rollAngle; + } + + private float computePitch(MinecraftClient client, float parital) { + return client.player.getPitch(parital) * -1; + } + + private boolean isGround(BlockPos pos, MinecraftClient client) { + BlockState block = client.world.getBlockState(pos); + return !block.isAir(); + } + + public BlockPos findGround(MinecraftClient client) { + BlockPos pos = client.player.getBlockPos(); + while (pos.getY() >= 0) { + pos = pos.down(); + if (isGround(pos, client)) { + return pos; + } + } + return null; + } + + private Integer computeGroundLevel(MinecraftClient client) { + BlockPos ground = findGround(client); + return ground == null ? null : ground.getY(); + } + + private Float computeDistanceFromGround(MinecraftClient client, float altitude, + Integer groundLevel) { + if (groundLevel == null) { + return null; + } + return Math.max(0f, altitude - groundLevel); + } + + private float computeAltitude(MinecraftClient client) { + return (float) client.player.getPos().y - 1; + } + + private float computeHeading(MinecraftClient client) { + return toHeading(client.player.getYaw()); + } + + private float computeSpeed(MinecraftClient client) { + float speed = 0; + var player = client.player; + if (player.hasVehicle()) { + Entity entity = player.getVehicle(); + speed = (float) entity.getVelocity().length() * TICKS_PER_SECOND; + } else { + speed = (float) client.player.getVelocity().length() * TICKS_PER_SECOND; + } + return speed; + } + + private float toHeading(float yawDegrees) { + return (yawDegrees + 180) % 360; + } +} From 01dcae490ec64fec1c7a5f17b43fe89d603f758e Mon Sep 17 00:00:00 2001 From: redrockruby <43187285+redrockruby@users.noreply.github.com> Date: Sat, 6 Aug 2022 22:17:38 -0400 Subject: [PATCH 2/3] Add files via upload Align pitch ladder to horizon --- src/main/java/net/torocraft/flighthud/FlightComputer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/torocraft/flighthud/FlightComputer.java b/src/main/java/net/torocraft/flighthud/FlightComputer.java index de26854..c7ed08c 100644 --- a/src/main/java/net/torocraft/flighthud/FlightComputer.java +++ b/src/main/java/net/torocraft/flighthud/FlightComputer.java @@ -96,6 +96,8 @@ private float computeRoll(MinecraftClient client, float partial) { rollAngle = (float) ((1.0 - rollSmoothing) * rollAngle + rollSmoothing * previousRollAngle); previousRollAngle = rollAngle; + //Make rollAngle follow terrain + rollAngle = rollAngle * -1; return rollAngle; } From 4fd296f7606cdd26d3079eb0e1189ad9ebc5893e Mon Sep 17 00:00:00 2001 From: redrockruby Date: Sat, 6 Aug 2022 22:23:05 -0400 Subject: [PATCH 3/3] Revert "Add files via upload" This reverts commit ec41db1d88fb2a8403a346b1bfd13f63e4144e8e. --- FlightComputer.java | 160 -------------------------------------------- 1 file changed, 160 deletions(-) delete mode 100644 FlightComputer.java diff --git a/FlightComputer.java b/FlightComputer.java deleted file mode 100644 index c7ed08c..0000000 --- a/FlightComputer.java +++ /dev/null @@ -1,160 +0,0 @@ -package net.torocraft.flighthud; - -import net.minecraft.block.BlockState; -import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Vec3d; - -public class FlightComputer { - private static final float TICKS_PER_SECOND = 20; - - private float previousRollAngle = 0.0f; - - public Vec3d velocity; - public float speed; - public float pitch; - public float heading; - public Vec3d flightPath; - public float flightPitch; - public float flightHeading; - public float roll; - public float altitude; - public Integer groundLevel; - public Float distanceFromGround; - public Float elytraHealth; - - public void update(MinecraftClient client, float partial) { - velocity = client.player.getVelocity(); - pitch = computePitch(client, partial); - speed = computeSpeed(client); - roll = computeRoll(client, partial); - heading = computeHeading(client); - altitude = computeAltitude(client); - groundLevel = computeGroundLevel(client); - distanceFromGround = computeDistanceFromGround(client, altitude, groundLevel); - flightPitch = computeFlightPitch(velocity, pitch); - flightHeading = computeFlightHeading(velocity, heading); - elytraHealth = computeElytraHealth(client); - } - - private Float computeElytraHealth(MinecraftClient client) { - ItemStack stack = client.player.getEquippedStack(EquipmentSlot.CHEST); - if (stack != null && stack.getItem() == Items.ELYTRA) { - float remain = ((float) stack.getMaxDamage() - (float) stack.getDamage()) / (float) stack.getMaxDamage(); - return remain * 100f; - } - return null; - } - - private float computeFlightPitch(Vec3d velocity, float pitch) { - if (velocity.length() < 0.01) { - return pitch; - } - Vec3d n = velocity.normalize(); - return (float) (90 - Math.toDegrees(Math.acos(n.y))); - } - - private float computeFlightHeading(Vec3d velocity, float heading) { - if (velocity.length() < 0.01) { - return heading; - } - return toHeading((float) Math.toDegrees(-Math.atan2(velocity.x, velocity.z))); - } - - /** - * Roll logic is from: - * https://github.com/Jorbon/cool_elytra/blob/main/src/main/java/edu/jorbonism/cool_elytra/mixin/GameRendererMixin.java - * to enable both mods will sync up when used together. - */ - private float computeRoll(MinecraftClient client, float partial) { - if (!FlightHud.CONFIG_SETTINGS.calculateRoll) { - return 0; - } - - float wingPower = FlightHud.CONFIG_SETTINGS.rollTurningForce; - float rollSmoothing = FlightHud.CONFIG_SETTINGS.rollSmoothing; - Vec3d facing = client.player.getRotationVecClient(); - Vec3d velocity = client.player.getVelocity(); - double horizontalFacing2 = facing.horizontalLengthSquared(); - double horizontalSpeed2 = velocity.horizontalLengthSquared(); - - float rollAngle = 0.0f; - - if (horizontalFacing2 > 0.0D && horizontalSpeed2 > 0.0D) { - double dot = (velocity.x * facing.x + velocity.z * facing.z) / Math.sqrt(horizontalFacing2 * horizontalSpeed2); - dot = MathHelper.clamp(dot, -1, 1); - double direction = Math.signum(velocity.x * facing.z - velocity.z * facing.x); - rollAngle = (float) (Math.atan(Math.sqrt(horizontalSpeed2) * Math.acos(dot) * wingPower) * direction - * 57.29577951308); - } - - rollAngle = (float) ((1.0 - rollSmoothing) * rollAngle + rollSmoothing * previousRollAngle); - previousRollAngle = rollAngle; - - //Make rollAngle follow terrain - rollAngle = rollAngle * -1; - return rollAngle; - } - - private float computePitch(MinecraftClient client, float parital) { - return client.player.getPitch(parital) * -1; - } - - private boolean isGround(BlockPos pos, MinecraftClient client) { - BlockState block = client.world.getBlockState(pos); - return !block.isAir(); - } - - public BlockPos findGround(MinecraftClient client) { - BlockPos pos = client.player.getBlockPos(); - while (pos.getY() >= 0) { - pos = pos.down(); - if (isGround(pos, client)) { - return pos; - } - } - return null; - } - - private Integer computeGroundLevel(MinecraftClient client) { - BlockPos ground = findGround(client); - return ground == null ? null : ground.getY(); - } - - private Float computeDistanceFromGround(MinecraftClient client, float altitude, - Integer groundLevel) { - if (groundLevel == null) { - return null; - } - return Math.max(0f, altitude - groundLevel); - } - - private float computeAltitude(MinecraftClient client) { - return (float) client.player.getPos().y - 1; - } - - private float computeHeading(MinecraftClient client) { - return toHeading(client.player.getYaw()); - } - - private float computeSpeed(MinecraftClient client) { - float speed = 0; - var player = client.player; - if (player.hasVehicle()) { - Entity entity = player.getVehicle(); - speed = (float) entity.getVelocity().length() * TICKS_PER_SECOND; - } else { - speed = (float) client.player.getVelocity().length() * TICKS_PER_SECOND; - } - return speed; - } - - private float toHeading(float yawDegrees) { - return (yawDegrees + 180) % 360; - } -}