Skip to content

Commit

Permalink
fix: discard roll when converting worldspace->screenspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Octol1ttle committed Aug 20, 2024
1 parent a58fd2d commit 16c9a80
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/main/java/ru/octol1ttle/flightassistant/DrawHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.octol1ttle.flightassistant;

import java.awt.Color;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.MutableText;
Expand All @@ -18,7 +17,7 @@ public static MutableText asText(String key, Object... args) {
}

public static Vec3d getScreenSpace(Vec3d delta) {
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(delta));
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(delta);
if (!ScreenSpaceRendering.isVisible(vec)) {
return null;
}
Expand All @@ -27,7 +26,7 @@ public static Vec3d getScreenSpace(Vec3d delta) {
}

public static Integer getScreenSpaceX(float pitch, float yaw) {
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(Vec3d.fromPolar(-pitch, yaw)));
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(Vec3d.fromPolar(-pitch, yaw));
if (!ScreenSpaceRendering.isVisible(vec)) {
return null;
}
Expand All @@ -36,7 +35,7 @@ public static Integer getScreenSpaceX(float pitch, float yaw) {
}

public static Integer getScreenSpaceY(float pitch, float yaw) {
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(Vec3d.fromPolar(-pitch, yaw)));
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(Vec3d.fromPolar(-pitch, yaw));
if (!ScreenSpaceRendering.isVisible(vec)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import net.minecraft.util.Pair;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.world.World;
import org.joml.Matrix4f;
import ru.octol1ttle.flightassistant.commands.FlightPlanCommand;
import ru.octol1ttle.flightassistant.commands.ResetCommand;
import ru.octol1ttle.flightassistant.commands.SelectCommand;
Expand Down Expand Up @@ -91,9 +93,14 @@ public void onEndTick(MinecraftClient client) {
@Override
public void onStart(WorldRenderContext context) {
ComputerHost.instance().tick();

ScreenSpaceRendering.lastProjMat.set(RenderSystem.getProjectionMatrix());
ScreenSpaceRendering.lastModMat.set(RenderSystem.getModelViewMatrix());
ScreenSpaceRendering.lastWorldSpaceMatrix.set(context.matrixStack().peek().getPositionMatrix());

Matrix4f worldSpaceNoRoll = new Matrix4f();
worldSpaceNoRoll.rotate(RotationAxis.POSITIVE_X.rotationDegrees(context.camera().getPitch()));
worldSpaceNoRoll.rotate(RotationAxis.POSITIVE_Y.rotationDegrees(context.camera().getYaw() + 180.0F));
ScreenSpaceRendering.lastWorldSpaceMatrix.set(worldSpaceNoRoll);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.octol1ttle.flightassistant.util;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -40,23 +39,18 @@ public class ScreenSpaceRendering {
* }
* </pre>
*
* @param pos The world space coordinates to translate
* @param deltaPos The world space coordinates to translate, relative to the camera's current position
* @return The (x, y, d) coordinates
* @throws NullPointerException If {@code pos} is null
*/
@Contract(value = "_ -> new", pure = true)
public static Vec3d fromWorldSpace(@NotNull Vec3d pos) {
Camera camera = client.getEntityRenderDispatcher().camera;
public static Vec3d fromWorldSpace(@NotNull Vec3d deltaPos) {
int displayHeight = client.getWindow().getHeight();
int[] viewport = new int[4];
GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
Vector3f target = new Vector3f();

double deltaX = pos.x - camera.getPos().x;
double deltaY = pos.y - camera.getPos().y;
double deltaZ = pos.z - camera.getPos().z;

Vector4f transformedCoordinates = new Vector4f((float) deltaX, (float) deltaY, (float) deltaZ, 1.f).mul(
Vector4f transformedCoordinates = new Vector4f((float) deltaPos.x, (float) deltaPos.y, (float) deltaPos.z, 1.f).mul(
lastWorldSpaceMatrix);

Matrix4f matrixProj = new Matrix4f(lastProjMat);
Expand Down

0 comments on commit 16c9a80

Please sign in to comment.