Skip to content

Commit

Permalink
Fix gimble lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jun 20, 2023
1 parent f8a74a2 commit 4cd2e5f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/main/java/com/fusionflux/portalcubed/PortalCubed.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -353,10 +354,24 @@ public static TeleportResult commonTeleport(
if (currentAnimationDelta.isPresent()) {
oldCameraRotation = oldCameraRotation.hamiltonProduct(currentAnimationDelta.get());
}
final IPQuaternion immediateFinalRot = oldCameraRotation.hamiltonProduct(portalTransform.getConjugated());
IPQuaternion immediateFinalRot = oldCameraRotation.hamiltonProduct(portalTransform.getConjugated());
final Vector3d euler = immediateFinalRot.toQuaterniond().getEulerAnglesZXY(new Vector3d());
final float finalYaw = (float)Math.toDegrees(euler.y) + 180;
final float finalPitch = (float)Math.toDegrees(euler.x);
float finalPitch = (float)Math.toDegrees(euler.x);

if (entity instanceof Player) {
boolean tweak = false;
if (finalPitch <= -50) {
finalPitch = -90;
tweak = true;
} else if (finalPitch >= 50) {
finalPitch = 90;
tweak = true;
}
if (tweak) {
immediateFinalRot = immediateFinalRot.hamiltonProduct(IPQuaternion.rotationByDegrees(new Vec3(0, 1, 0), 180));
}
}

final Vec3 dest = portal.getDestination().get().add(rotatedOffsets);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@
import com.fusionflux.portalcubed.util.PortalDirectionUtils;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.unascribed.lib39.recoil.api.RecoilEvents;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler;
import net.fabricmc.fabric.api.client.rendering.v1.*;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
Expand All @@ -60,6 +63,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.*;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.GsonHelper;
Expand All @@ -74,6 +78,7 @@
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.lwjgl.opengl.GL11;
import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.loader.api.QuiltLoader;
Expand All @@ -91,6 +96,7 @@
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.DecimalFormat;
import java.util.*;

import static com.fusionflux.portalcubed.PortalCubed.LOGGER;
Expand All @@ -110,6 +116,8 @@ public class PortalCubedClient implements ClientModInitializer {
};
public static final int ZOOM_TIME = 2;

private static final DecimalFormat CL_SHOWPOS_FORMAT = new DecimalFormat("0.00");

private static final File GLOBAL_ADVANCEMENTS_FILE = QuiltLoader.getGameDir().resolve("portal_cubed_global_advancements.dat").toFile();
private static final Set<ResourceLocation> GLOBAL_ADVANCEMENTS = new HashSet<>();

Expand Down Expand Up @@ -137,6 +145,9 @@ public class PortalCubedClient implements ClientModInitializer {

public static WorldRenderContext worldRenderContext; // QFAPI impl detail: this is a mutable singleton

private static String clShowPosValue = "0";
public static boolean clShowPos = false;

@Override
public void onInitializeClient(ModContainer mod) {

Expand Down Expand Up @@ -475,6 +486,38 @@ Items.LIGHT, new ResourceLocation("level")

WorldRenderEvents.START.register(context -> worldRenderContext = context);

HudRenderCallback.EVENT.register((poseStack, tickDelta) -> {
if (!clShowPos) return;
final Minecraft minecraft = Minecraft.getInstance();
if (minecraft.options.renderDebug) return;
final LocalPlayer player = minecraft.player;
if (player == null) return;

minecraft.font.draw(poseStack, Component.literal("name: ").append(player.getName()), 2, 11, 0xffffffff);

minecraft.font.draw(
poseStack,
"pos: " + CL_SHOWPOS_FORMAT.format(player.getX()) +
' ' + CL_SHOWPOS_FORMAT.format(player.getY()) +
' ' + CL_SHOWPOS_FORMAT.format(player.getZ()),
2, 20, 0xffffffff
);

IPQuaternion quat = IPQuaternion.getCameraRotation(player.getXRot(), player.getYRot());
final Optional<IPQuaternion> rotation = interpCamera();
if (rotation.isPresent()) {
quat = quat.hamiltonProduct(rotation.get());
}
final Vector3d angle = quat.toQuaterniond().getEulerAnglesZXY(new Vector3d());
minecraft.font.draw(
poseStack,
"ang: " + CL_SHOWPOS_FORMAT.format(Math.toDegrees(angle.x)) +
' ' + CL_SHOWPOS_FORMAT.format(Mth.wrapDegrees(Math.toDegrees(angle.y) + 180)) +
' ' + CL_SHOWPOS_FORMAT.format(Math.toDegrees(angle.z)),
2, 29, 0xffffffff
);
});

try {
final CompoundTag compound = NbtIo.readCompressed(GLOBAL_ADVANCEMENTS_FILE);
for (final Tag element : compound.getList("Advancements", Tag.TAG_STRING)) {
Expand All @@ -484,8 +527,8 @@ Items.LIGHT, new ResourceLocation("level")
LOGGER.warn("Failed to load global advancements", e);
}

if (QuiltLoader.isDevelopmentEnvironment()) {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext, environment) -> {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext, environment) -> {
if (QuiltLoader.isDevelopmentEnvironment()) {
dispatcher.register(literal("face")
.then(argument("direction", DirectionArgumentType.direction())
.executes(ctx -> {
Expand All @@ -502,8 +545,31 @@ Items.LIGHT, new ResourceLocation("level")
})
)
);
});
}
}
dispatcher.register(literal("cl_showpos")
.then(argument("value", StringArgumentType.greedyString())
.executes(ctx -> {
clShowPosValue = StringArgumentType.getString(ctx, "value");
try {
clShowPos = Integer.parseInt(clShowPosValue) != 0;
} catch (NumberFormatException e) {
clShowPos = false;
}
return 1;
})
)
.executes(ctx -> {
ctx.getSource().sendFeedback(
Component.empty()
.append(Component.literal("\"cl_showpos\" = \"" + clShowPosValue + "\"").withStyle(ChatFormatting.RED))
.append(" (def. \"0\" )")
);
ctx.getSource().sendFeedback(Component.literal(" client"));
ctx.getSource().sendFeedback(Component.literal(" - Draw current position at top of screen"));
return 1;
})
);
});
}

private void registerEmissiveModels(ModContainer mod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.fusionflux.portalcubed.PortalCubed;
import net.minecraft.util.Mth;
import net.minecraft.util.Tuple;
import net.minecraft.world.phys.Vec3;
import org.joml.Math;
Expand Down Expand Up @@ -311,7 +312,7 @@ public double dotProduct(IPQuaternion q) {
}

public static IPQuaternion getCameraRotation(double pitch, double yaw) {
IPQuaternion r1 = rotationByDegrees(new Vec3(1, 0, 0), pitch);
IPQuaternion r1 = rotationByDegrees(new Vec3(1, 0, 0), Mth.clamp(pitch, -89.9999, 89.9999));
IPQuaternion r2 = rotationByDegrees(new Vec3(0, 1, 0), yaw + 180);
return r1.hamiltonProduct(r2);
}
Expand Down

0 comments on commit 4cd2e5f

Please sign in to comment.