在 Java 中从 3D 位置计算屏幕空间位置

问题描述 投票:0回答:0

概览

嗨,我正在尝试计算屏幕上航路点的屏幕空间位置,我不知道这样做的数学,我试过谷歌搜索甚至问过 chat-gpt,但没有任何效果,我没有不知道这里是否有人有任何建议。

   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);
        
    }

github 回购:https://github.com/DcmanProductions/The-Warp-Mod/blob/be5400678cfe3f05a8a0501ac9d2b9f3d04a4a63/common/src/main/java/chase/minecraft/architectury/warpmod/client/gui/waypoint/WaypointOverlay。 java

java opengl minecraft lwjgl
© www.soinside.com 2019 - 2024. All rights reserved.