Привет, я пытаюсь рассчитать положение путевой точки на экране в пространстве экрана, я не могу понять математику, чтобы сделать это.
private Vector2f calc(float partialTicks)
{
// Getting the gui scale adjusted width and height
Window window = _minecraft.getWindow();
int width = window.getGuiScaledWidth();
int height = window.getGuiScaledHeight();
// Calculating the center of the screen
double screenCenterX = width / 2.0;
double screenCenterY = height / 2.0;
// Getting the player from minecraft
LocalPlayer player = _minecraft.player;
assert player != null;
// The players looking angle
float pitch = player.getXRot();
float yaw = player.getYRot();
// The players field of view
double fov = Math.toRadians(player.getFieldOfViewModifier() * 70f);
float pitchRadians = (float) Math.toRadians(pitch);
float yawRadians = (float) Math.toRadians(yaw);
double verticalPixelPerRad = screenCenterY / fov;
double horizontalPixelPerRad = screenCenterX / fov;
// The players position
double px = player.getX();
double py = player.getY();
double pz = player.getZ();
// The warps position
double wx = _warp.x();
double wy = _warp.y();
double wz = _warp.z();
// Getting the delta between the players position and the position of the warp
double dx = wx - px;
double dy = wy - py;
double dz = wz - pz;
// Calculate the distance between the player and the warp
double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
// TODO: Calculate the screen space position of the warp point relative to the players position and rotation
return new Vector2f(0,0);
}
Итак, я решил проблему
private static final Minecraft client = Minecraft.getInstance();
public static final Matrix4f lastProjMat = new Matrix4f();
public static final Matrix4f lastModMat = new Matrix4f();
public static final Matrix4f lastWorldSpaceMatrix = new Matrix4f();
public static Vector3d worldSpaceToScreenSpace(Vector3d pos)
{
Camera camera = client.getEntityRenderDispatcher().camera;
int displayHeight = client.getWindow().getHeight();
int[] viewport = new int[4];
GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
Vector3f target = new Vector3f();
double dx = pos.x - camera.getPosition().x;
double dy = pos.y - camera.getPosition().y;
double dz = pos.z - camera.getPosition().z;
Vector4f transformedCoordinates = new Vector4f((float) dx, (float) dy, (float) dz, 1.f).mul(lastWorldSpaceMatrix);
Matrix4f matrixProj = new Matrix4f(lastProjMat);
Matrix4f matrixModel = new Matrix4f(lastModMat);
matrixProj.mul(matrixModel).project(transformedCoordinates.x(), transformedCoordinates.y(), transformedCoordinates.z(), viewport, target);
return new Vector3d(target.x / client.getWindow().getGuiScale(), (displayHeight - target.y) / client.getWindow().getGuiScale(), target.z);
}
public static boolean screenSpaceCoordinateIsVisible(Vector3d pos)
{
return pos != null && (pos.z > -1 && pos.z < 1);
}