Skip to content
Open
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
plugins {
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'maven-publish'
}

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

archivesBaseName = project.archives_base_name
version = project.mod_version
Expand Down
23 changes: 10 additions & 13 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
# After making changes
# gradlew --refresh-dependencies
# To remap the mixin locations:
# gradlew migrateMappings --mappings "1.16.1+build.9"

minecraft_version=1.19
yarn_mappings=1.19+build.1
loader_version=0.14.7
minecraft_version=1.21
yarn_mappings=1.21+build.9
loader_version=0.16.2

#Fabric api
fabric_version=0.55.3+1.19

fabric_version=0.102.0+1.21
# Mod Properties
mod_version = 1.19-fabric-1


# Dependencies
# check this on https://modmuss50.me/fabric.html
mod_version = 1.21.01

maven_group = net.torocraft
archives_base_name = flighthud
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.3.1-bin.zip
distributionUrl=https://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
31 changes: 28 additions & 3 deletions src/main/java/net/torocraft/flighthud/Dimensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import net.minecraft.client.MinecraftClient;
import net.torocraft.flighthud.config.HudConfig;

import java.lang.Math;

public class Dimensions {

public float hScreen;
public float wScreen;
public float degreesPerPixel;
public float pixelsPerDegree;
public float xMid;
public float yMid;

Expand All @@ -31,7 +33,30 @@ public void update(MinecraftClient client) {
wScreen = wScreen * c.scale;
}

degreesPerPixel = hScreen / client.options.getFov().getValue();
// The HUD is rendered as a plane in front of the player and not at a
// spehere. The pixelsPerDegree parameter is therefore a simplification,
// which is not exact.
//
// To use this simplification, a given spot on the screen has to be picked
// to be be correct for the approximation.
//
// Most intuitive would then be to have the horizon line correctly stick to
// the world when in the center of the screen. So therefore, calculate the
// number of degrees per pixel in the _center_ of the screen
//
// This simplifies rendering a lot, instead of always calculate the correct
// transformation between spherical and cartesian coordinates


// Calculate height of virtual screen at distance of 1 unit
// Note that FOV is degrees from top to bottom, we need height from center
// to top

Integer fov_deg = client.options.getFov().getValue();
double hud_height = Math.tan(fov_deg * Math.PI / 180.0 / 2.0);
double hud_pixel_height = hud_height / (double)(hScreen/2);
pixelsPerDegree = 1.0f / (float)(Math.atan(hud_pixel_height) * 180.0 / Math.PI);

xMid = wScreen / 2;
yMid = hScreen / 2;

Expand All @@ -45,4 +70,4 @@ public void update(MinecraftClient client) {
bFrame = tFrame + hFrame;
}

}
}
92 changes: 60 additions & 32 deletions src/main/java/net/torocraft/flighthud/FlightComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
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;
import org.joml.Quaternionf;
import org.joml.Vector3f;

public class FlightComputer {
private static final float TICKS_PER_SECOND = 20;

private float previousRollAngle = 0.0f;
static final float rad2deg = (float)(180/Math.PI);

public Vec3d velocity;
public float speed;
Expand All @@ -28,12 +28,52 @@ public class FlightComputer {
public Float distanceFromGround;
public Float elytraHealth;

public void update(MinecraftClient client, float partial) {
public static Vector3f quaternionToEuler(Quaternionf q) {
/**
* Roll logic is from:
* https://github.com/HuJK/FlightHUD-Do-a-Barrel-Roll-Fix/blob/dabr-1.20.5/src/main/java/net/torocraft/flighthud/FlightComputer.java#L32
* it retrive rotation quaternion from camara to enable both mods will sync up when used together.
*/
double x = q.x;
double y = q.y;
double z = q.z;
double w = q.w;

double yaw, pitch, roll;

// Yaw (Y-axis rotation)
double sinr_cosp = 2 * (w * y + x * z);
double cosr_cosp = 1 - 2 * (y * y + x * x);
yaw = Math.atan2(sinr_cosp, cosr_cosp);

// Pitch (X-axis rotation)
double sinp = 2 * (w * x - z * y);
if (Math.abs(sinp) >= 1)
pitch = Math.copySign(Math.PI / 2, sinp); // use 90 degrees if out of range
else
pitch = Math.asin(sinp);

// Roll (Z-axis rotation)
double siny_cosp = 2 * (w * z + y * x);
double cosy_cosp = 1 - 2 * (x * x + z * z);
roll = Math.atan2(siny_cosp, cosy_cosp);

return new Vector3f((float) yaw, (float) pitch, (float) roll);
}

public void update(MinecraftClient client, Quaternionf rotation) {
if (client==null){
return;
}
Vector3f eulerrotation = quaternionToEuler(rotation);
heading = -eulerrotation.x* rad2deg;
pitch = eulerrotation.y* rad2deg;
roll = eulerrotation.z* rad2deg;
//heading = computeHeading(client);
//pitch = computePitch(client);
//roll = computeRoll(client, eulerrotation.z* rad2deg);
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);
Expand Down Expand Up @@ -71,36 +111,20 @@ private float computeFlightHeading(Vec3d velocity, float heading) {
* 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) {
private float computeRoll(MinecraftClient client, float roll) {
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);
return 0.0f;
}

rollAngle = (float) ((1.0 - rollSmoothing) * rollAngle + rollSmoothing * previousRollAngle);
previousRollAngle = rollAngle;

return rollAngle;
return roll;
}

private float computePitch(MinecraftClient client, float parital) {
return client.player.getPitch(parital) * -1;
private float computePitch(MinecraftClient client) {
if (client.player == null) {
return 0.0f;
}

return -client.player.getPitch();
}

private boolean isGround(BlockPos pos, MinecraftClient client) {
Expand Down Expand Up @@ -137,6 +161,10 @@ private float computeAltitude(MinecraftClient client) {
}

private float computeHeading(MinecraftClient client) {
if (client.player == null) {
return 0.0f;
}

return toHeading(client.player.getYaw());
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/torocraft/flighthud/FlightHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class FlightHud implements ClientModInitializer {

private static KeyBinding keyBinding;

public static final FlightComputer computer = new FlightComputer();

@Override
public void onInitializeClient() {
CONFIG_LOADER_SETTINGS.load();
Expand Down
Loading