From 60a0a18a3216254061a2a7a4df4ce33c7fcbdc86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?der=5Ffr=C3=BChling?= Date: Wed, 5 Jun 2024 22:28:09 -0700 Subject: [PATCH] #57 generally fix standing issues --- .../create/trainperspective/MixinUtil.java | 21 +++++++++++++++++++ .../trainperspective/mixin/CameraMixin.java | 16 ++++---------- .../mixin/CreateRaycastHelperMixin.java | 15 +++++++++++++ forge/build.gradle | 3 +++ 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/MixinUtil.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/MixinUtil.java index 02e29b5..5ca777a 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/MixinUtil.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/MixinUtil.java @@ -2,6 +2,10 @@ import net.minecraft.client.Camera; import net.minecraft.util.Mth; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.phys.Vec3; +import org.joml.Vector3d; +import org.joml.Vector3f; public class MixinUtil { private MixinUtil() {} @@ -23,4 +27,21 @@ public static float applyDirectionXRotChange(Perspective persp, float xRot, floa public static float getExtraYRot(Perspective persp, float xRot, float yRot, float f) { return persp.getLean(f) * (xRot / 90.0f) * invCos((persp.getYaw(f) - yRot) * Mth.DEG_TO_RAD); } + + public static Vector3d applyStandingCameraRotation(Player player, double x, double y, double z, Perspective persp, float f) { + var lean = persp.getLean(f) * Mth.DEG_TO_RAD; + var yaw = persp.getYaw(f) * Mth.DEG_TO_RAD; + var height = player.getEyeHeight(); + var newY = y + ((height * Mth.cos(lean)) - height); + var leanSin = Mth.sin(lean); + var newZ = z - (height * Mth.sin(yaw) * leanSin); + var newX = x - (height * Mth.cos(yaw) * leanSin); + + return new Vector3d(newX, newY, newZ); + } + + public static Vec3 applyStandingCameraRotation(Player player, Vec3 v, Perspective persp, float f) { + var vec = applyStandingCameraRotation(player, v.x, v.y, v.z, persp, f); + return new Vec3(vec.x, vec.y, vec.z); + } } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java index 28f612d..d0ac88d 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java @@ -89,21 +89,13 @@ public void modifyPosition(Camera instance, && player.getVehicle() == null && !isThirdPerson) { var persp = (Perspective) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player); - var lean = persp.getLean(f); - var yaw = persp.getYaw(f); + var newV = MixinUtil.applyStandingCameraRotation(player, x, y, z, persp, f); - var height = player.getEyeHeight(); - // var leanFactor = (1.0f - Mth.cos(2.0f * lean * Mth.DEG_TO_RAD)) / 2.0f; - var leanFactor = (lean / 90.0f); - var newY = y - (height * leanFactor); - var newX = x - Mth.abs(height * height * (Mth.cos(yaw * Mth.DEG_TO_RAD)) * leanFactor); - var newZ = z - Mth.abs(height * height * (Mth.sin(yaw * Mth.DEG_TO_RAD)) * leanFactor); - - if(ModConfig.INSTANCE.dbgShowStandingTransforms) { - player.displayClientMessage(Component.literal(String.format("%f, %f, %f", newX - x, newY - y, newZ - z)), true); + if (ModConfig.INSTANCE.dbgShowStandingTransforms) { + player.displayClientMessage(Component.literal("%f, %f, %f".formatted(x - newV.x, y - newV.y, z - newV.z)), true); } - setPosition(newX, newY, newZ); + setPosition(newV.x, newV.y, newV.z); } else { setPosition(x, y, z); } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java index 3ee7d3c..ec55acf 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java @@ -1,17 +1,32 @@ package net.derfruhling.minecraft.create.trainperspective.mixin; +import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.simibubi.create.foundation.utility.RaycastHelper; import net.derfruhling.minecraft.create.trainperspective.Conditional; import net.derfruhling.minecraft.create.trainperspective.MixinUtil; import net.derfruhling.minecraft.create.trainperspective.Perspective; import net.minecraft.client.Minecraft; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.phys.Vec3; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyVariable; @Mixin(RaycastHelper.class) public class CreateRaycastHelperMixin { + @ModifyReturnValue(method = "getTraceOrigin", at = @At("RETURN")) + private static Vec3 applyLeaning(Vec3 original, Player player) { + if(player.isLocalPlayer()) { + var renderer = Minecraft.getInstance() + .getEntityRenderDispatcher() + .getRenderer(player); + var persp = (Perspective) renderer; + return MixinUtil.applyStandingCameraRotation(player, original, persp, 1.0f); + } else { + return original; + } + } + @ModifyVariable(method = "getTraceTarget", at = @At("STORE"), index = 4) private static float modifyPitch(float pitch, Player player) { if(Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player) instanceof Perspective persp diff --git a/forge/build.gradle b/forge/build.gradle index 302a168..b2afb2c 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -38,6 +38,9 @@ dependencies { modApi("me.shedaniel.cloth:cloth-config-forge:${cloth_config_version}") + compileOnly(annotationProcessor("io.github.llamalad7:mixinextras-common:0.3.6")) + implementation(include("io.github.llamalad7:mixinextras-forge:0.3.6")) + common(project(path: ":common", configuration: "namedElements")) { transitive false } shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive = false } }