Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#57 fix standing issues #63

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);
}
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
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
Loading