Skip to content

Commit

Permalink
Merge pull request #55 from /issues/53-perspective-translation
Browse files Browse the repository at this point in the history
Mostly fix perpsective translation issue
  • Loading branch information
der-fruhling authored May 22, 2024
2 parents e70b614 + 6ceccd0 commit 66345ca
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ subprojects {
apply plugin: "dev.architectury.loom"
apply plugin: "maven-publish"

configurations {
mixinDependency
}

loom {
silentMojangMappingsLicense()
}

dependencies {
minecraft "com.mojang:minecraft:${rootProject.minecraft_version}"
mappings loom.officialMojangMappings()
mixinDependency("net.fabricmc:sponge-mixin:0.12.5+mixin.0.8.5") { transitive = false }
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.simibubi.create.foundation.config.ui.entries.BooleanEntry;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigScreen;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
Expand All @@ -21,6 +20,7 @@ private ModConfig() {}
public boolean rollEnabled = true;
public float rollMagnitude = 1.0f;
public boolean applyToOthers = true;
public boolean dbgShowStandingTransforms = false;

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final WatchService WATCH_SERVICE;
Expand Down Expand Up @@ -157,6 +157,22 @@ public static Screen createConfigScreen(Screen parent) {
.setDefaultValue(1.0f)
.build());

var debug = entryBuilder.startSubCategory(Component.translatable("category.create_train_perspective.debug"));

debug.add(entryBuilder
.startTextDescription(Component.translatable("category.create_train_perspective.debug.description").withStyle(ChatFormatting.BOLD))
.build());

debug.add(entryBuilder
.startBooleanToggle(
Component.translatable("option.create_train_perspective.debug.standing_transforms"),
INSTANCE.dbgShowStandingTransforms)
.setSaveConsumer(value -> INSTANCE.dbgShowStandingTransforms = value)
.setTooltip(Component.translatable("option.create_train_perspective.debug.standing_transforms.tooltip"))
.setDefaultValue(false)
.build());

advanced.add(debug.build());
general.addEntry(advanced.build());

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import org.joml.Quaternionf;
Expand All @@ -27,6 +28,8 @@ public abstract class CameraMixin {

@Shadow @Final private Quaternionf rotation;

@Shadow protected abstract void setPosition(double d, double e, double f);

@ModifyArg(method = "setRotation", at = @At(value = "INVOKE", target = "Lorg/joml/Quaternionf;rotationYXZ(FFF)Lorg/joml/Quaternionf;"), index = 2)
private float modifyRoll(float original) {
return original + (ctp$zRot * Mth.DEG_TO_RAD);
Expand Down Expand Up @@ -72,4 +75,37 @@ public void modifyRotations(Camera instance,
setRotation(y, x);
}
}

@Redirect(method = "setup", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Camera;setPosition(DDD)V"))
public void modifyPosition(Camera instance,
double x,
double y,
double z,
@Local(argsOnly = true, ordinal = 0) boolean isThirdPerson,
@Local(argsOnly = true) 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 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);
}

setPosition(newX, newY, newZ);
} else {
setPosition(x, y, z);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected void setupRotations(AbstractClientPlayer p_117802_, PoseStack p_117803
float height = 0;

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

p_117803_.rotateAround(Axis.ZP.rotationDegrees(Mth.cos(Mth.DEG_TO_RAD * ctp$yaw) * ctp$lean), 0, height, 0);
Expand Down
6 changes: 5 additions & 1 deletion common/src/main/resources/assets/minecraft/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"category.create_train_perspective.leaning": "Leaning",
"category.create_train_perspective.multiplayer": "Multiplayer",
"category.create_train_perspective.advanced": "Advanced",
"category.create_train_perspective.debug": "Debug Features",
"category.create_train_perspective.debug.description": "These are debugging features used for development. You probably don't want to touch these unless you're working on the mod itself.",

"option.create_train_perspective.enabled": "Enabled",
"option.create_train_perspective.enabled.tooltip": "Enables the mod. If False, the mod is disabled.",
Expand All @@ -17,5 +19,7 @@
"option.create_train_perspective.advanced.lean_magnitude": "Leaning Magnitude",
"option.create_train_perspective.advanced.lean_magnitude.tooltip": "The magnitude to which the player will lean. Higher = more intense.",
"option.create_train_perspective.advanced.roll_magnitude": "Rolling Magnitude",
"option.create_train_perspective.advanced.roll_magnitude.tooltip": "The magnitude to which the player will roll. Higher = more intense."
"option.create_train_perspective.advanced.roll_magnitude.tooltip": "The magnitude to which the player will roll. Higher = more intense.",
"option.create_train_perspective.debug.standing_transforms": "Show Standing Translations",
"option.create_train_perspective.debug.standing_transforms.tooltip": "Shows translations used to transform the player while standing on a train. (X, Y, Z)"
}
15 changes: 15 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ architectury {
fabric()
}

loom {
runs {
client {
client()
ideConfigGenerated = true

configurations.mixinDependency.files.each {
vmArg "-javaagent:$it.absolutePath".toString()
}

source sourceSets.main
}
}
}

configurations {
common
shadowCommon // Don't use shadow from the shadow plugin since it *excludes* files.
Expand Down

0 comments on commit 66345ca

Please sign in to comment.