Skip to content

Commit

Permalink
"Fix" stuttering when turning the player about the Y axis (#7)
Browse files Browse the repository at this point in the history
* Use yRotO for movement interpolation, fix forward axis

* Fix mouse camera movement

* Always process resources

* Use the armour stand's axes, not the player's

* tidyup
  • Loading branch information
lucyydotp authored Jun 7, 2024
1 parent 12b85db commit 4f60ca1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {


tasks.processResources {
outputs.upToDateWhen { false }
filesMatching("fabric.mod.json") {
expand("version" to project.version)
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/me/lucyydotp/papers/CameraPerspectiveMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.joml.Quaternionf;

Expand Down Expand Up @@ -31,12 +32,13 @@ final class ArmorStandHead implements CameraPerspectiveMode {

private final ArmorStand armorStand;
private final long creationTime;

private float lastYRot;

public ArmorStandHead(ArmorStand armorStand) {
this.armorStand = armorStand;
this.creationTime = armorStand.level.getGameTime();
this.lastYRot = armorStand.getYRot();
this.lastYRot = armorStand.yHeadRot;
}

/**
Expand All @@ -56,24 +58,22 @@ public void transform(PoseStack cameraPoseStack, float tickProgress, long time)
final var player = Minecraft.getInstance().player;
if (!player.isPassenger()) return;

// fixme: is doing this on the render thread bad?
player.turn(
(armorStand.getYRot() - lastYRot) / 0.15,
0
);

lastYRot = armorStand.getYRot();
final var lerpedYRot = Mth.rotLerp(tickProgress, armorStand.yRotO, armorStand.getYRot());
player.setYRot(player.getYRot() + lerpedYRot - lastYRot);
lastYRot = lerpedYRot;

final var pose = armorStand.getHeadPose();
final var lastPose = ((ArmorStandExt) armorStand).paper$lastHeadRot();
final var forward = Vec3.directionFromRotation(0, lerpedYRot).toVector3f();

// fixme: properly slerp instead of doing whatever this is
cameraPoseStack.rotateAround(
new Quaternionf()
.rotateAxis((float) Math.toRadians(Mth.rotLerp(tickProgress, lastPose.getZ(), pose.getZ())), player.getForward().toVector3f())
.rotateAxis((float) Math.toRadians(Mth.rotLerp(tickProgress, lastPose.getY(), pose.getY())), player.getUpVector(1).toVector3f())
.rotateAxis((float) Math.toRadians(Mth.rotLerp(tickProgress, -lastPose.getX(), -pose.getX())), player.getForward().toVector3f().rotateY((float) (Math.PI / 2f))),
.rotateAxis((float) Math.toRadians(Mth.rotLerp(tickProgress, lastPose.getZ(), pose.getZ())), forward)
.rotateLocalY((float) Math.toRadians(Mth.rotLerp(tickProgress, lastPose.getY(), pose.getY())))
.rotateAxis((float) Math.toRadians(Mth.rotLerp(tickProgress, lastPose.getX(), pose.getX())), forward.rotateY((float) (Math.PI / -2f))),
0,
// fixme: this might be a little low?
// fixme: i completely made this value up. it feels okay-ish but isn't right
(float) -1,
0
);
Expand Down
30 changes: 5 additions & 25 deletions src/main/java/me/lucyydotp/papers/mixin/GameRendererMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,26 @@
import me.lucyydotp.papers.PassengerPerspectiveMod;
import net.minecraft.client.renderer.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GameRenderer.class)
public class GameRendererMixin {

@Unique
private float papers$f;

@Unique
private long papers$l;

@Inject(
method = "renderLevel",
at = @At("HEAD")
)
public void setF(float f, long l, PoseStack poseStack, CallbackInfo ci) {
papers$f = f;
papers$l = l;
}

/**
* Applies the active perspective mode's camera transformation.
*/
@ModifyArg(
@Inject(
method = "renderLevel",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/renderer/LevelRenderer;prepareCullFrustum(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/world/phys/Vec3;Lorg/joml/Matrix4f;)V",
ordinal = 0
value = "NEW",
target = "org/joml/Matrix3f"
)
)
public PoseStack addRidePerspectiveRotation(PoseStack poseStack) {
public void addRidePerspectiveRotation(float f, long l, PoseStack poseStack, CallbackInfo ci) {
final var perspectiveMode = PassengerPerspectiveMod.getPerspectiveMode();
if (perspectiveMode != null) {
perspectiveMode.transform(poseStack, papers$f, papers$l);
perspectiveMode.transform(poseStack, f, l);
}
return poseStack;
}
}

0 comments on commit 4f60ca1

Please sign in to comment.