Skip to content

Commit

Permalink
Fix #60, #58, #56, #57
Browse files Browse the repository at this point in the history
(prepare for backporting)
  • Loading branch information
der-fruhling committed Jun 6, 2024
5 parents 358578a + 7fc139f + 68c4743 + 315938f + 60a0a18 commit 87569d7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.BlockGetter;
import org.joml.Quaternionf;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -49,8 +50,11 @@ private float modifyRoll(float original) {
public void modifyRotations(Camera instance,
float y,
float x,
@Local(argsOnly = true, ordinal = 0) boolean isThirdPerson,
@Local(argsOnly = true) float f) {
BlockGetter blockGetter,
Entity entity,
boolean isThirdPerson,
boolean bl2,
float f) {
if(entity instanceof AbstractClientPlayer player
&& Conditional.shouldApplyPerspectiveTo(entity)
&& Conditional.shouldApplyLeaning()
Expand Down Expand Up @@ -81,29 +85,24 @@ public void modifyPosition(Camera instance,
double x,
double y,
double z,
@Local(argsOnly = true, ordinal = 0) boolean isThirdPerson,
@Local(argsOnly = true) float f) {
BlockGetter blockGetter,
Entity entity,
boolean isThirdPerson,
boolean bl2,
float f) {
if(entity instanceof AbstractClientPlayer player
&& Conditional.shouldApplyPerspectiveTo(entity)
&& Conditional.shouldApplyLeaning()
&& 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package net.derfruhling.minecraft.create.trainperspective.mixin;

import com.mojang.authlib.GameProfile;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.AbstractClientPlayer;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.derfruhling.minecraft.create.trainperspective.Perspective;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;

// If anything else tries to @Overwrite getViewYRot, use their changes.
@Mixin(value = LocalPlayer.class, priority = 200)
public class LocalPlayerMixin extends AbstractClientPlayer {
private LocalPlayerMixin(ClientLevel clientLevel, GameProfile gameProfile) {
super(clientLevel, gameProfile);
}
@Mixin(value = LocalPlayer.class)
public class LocalPlayerMixin {
@Shadow @Final protected Minecraft minecraft;

/**
* @author der_frühling
* @reason Mojang, for some reason, is not using the value of getViewYRot unless
* the player is a passenger.
* This breaks yaw while standing on a train.
* This is an {@link Overwrite @Overwrite} method because of the small
* size of the target method and the simple fix it needs to apply.
*/
@Overwrite
public float getViewYRot(float f) {
return super.getViewYRot(f);
@WrapOperation(method = "getViewYRot", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/LocalPlayer;isPassenger()Z"))
public boolean isPassenger(LocalPlayer instance, Operation<Boolean> original) {
var perspective = (Perspective) minecraft.getEntityRenderDispatcher().getRenderer(instance);
return original.call(instance) || perspective.isEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ public class PlayerRendererMixin {
value = "HEAD"
)
)
protected void setupRotations(AbstractClientPlayer p_117802_, PoseStack p_117803_, float p_117804_, float p_117805_, float p_117806_, CallbackInfo ci) {
protected void setupRotations(AbstractClientPlayer p_117802_, PoseStack p_117803_, float p_117804_, float p_117805_, float f, CallbackInfo ci) {
if(ctp$perspectiveActive && Conditional.shouldApplyPerspectiveTo(p_117802_)) {
float height = 0;

if(p_117802_.getVehicle() != null) {
height = p_117802_.getEyeHeight();
}

p_117803_.rotateAround(Axis.ZP.rotationDegrees(Mth.cos(Mth.DEG_TO_RAD * ctp$yaw) * ctp$lean), 0, height, 0);
p_117803_.rotateAround(Axis.XP.rotationDegrees(Mth.sin(Mth.DEG_TO_RAD * ctp$yaw) * -ctp$lean), 0, height, 0);
var lean = ctp$getLean(f);
p_117803_.rotateAround(Axis.ZP.rotationDegrees(Mth.cos(Mth.DEG_TO_RAD * ctp$yaw) * lean), 0, height, 0);
p_117803_.rotateAround(Axis.XP.rotationDegrees(Mth.sin(Mth.DEG_TO_RAD * ctp$yaw) * -lean), 0, height, 0);
}
}
}
3 changes: 3 additions & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down

0 comments on commit 87569d7

Please sign in to comment.