Java 光线投射提供不稳定的值

Java 光线投射提供不稳定的值,java,opengl,lwjgl,Java,Opengl,Lwjgl,使用LWJGL3和JOML 我正在尝试解决如何使用光线投射系统获取地形上的点。我使用点来设置字符位置,以查看输出的点 我不认为这是我的地形造成的问题,因为移动角色(用键盘或只是在每帧添加一个值)并将其附加到地形可以很好地工作 我得到的问题是: 反转投影矩阵会导致点在正确位置和其他点之间闪烁,但我不确定与其他值的关系 不反转投影矩阵会停止闪烁,但现在该点以指数方式远离鼠标位置 在屏幕中央附近,两个位置将合并在一起 如果我打印出地形点矢量,出于某种原因,它会以科学记数法显示: ( 5.335E+

使用LWJGL3和JOML

我正在尝试解决如何使用光线投射系统获取地形上的点。我使用点来设置字符位置,以查看输出的点

我不认为这是我的地形造成的问题,因为移动角色(用键盘或只是在每帧添加一个值)并将其附加到地形可以很好地工作

我得到的问题是:

  • 反转投影矩阵会导致点在正确位置和其他点之间闪烁,但我不确定与其他值的关系
  • 不反转投影矩阵会停止闪烁,但现在该点以指数方式远离鼠标位置
  • 在屏幕中央附近,两个位置将合并在一起
如果我打印出地形点矢量,出于某种原因,它会以科学记数法显示:

( 5.335E+1  3.849E-2 -9.564E+1)
( 8.804E+1 -6.256E-3 -2.815E+2)
( 5.335E+1  3.849E-2 -9.564E+1)
( 8.804E+1 -6.256E-3 -2.815E+2)
( 5.335E+1  3.849E-2 -9.564E+1)
如果我分别打印出每个x、y和z值,它实际上显示的是正确的位置,但也会切换到另一个位置(两者之间的差异会随着鼠标移动距离屏幕中心越远而增大):

光线投射类:

public class Raycast
{
    private static final int RECURSION_COUNT = 200;
    private static final float RAY_RANGE = 600;

    private Input input;
    private Vector3f currentRay = new Vector3f();

    private Matrix4f projectionMatrix;
    private Matrix4f viewMatrix;

    private Camera camera;
    private Terrain terrain;
    private Vector3f currentTerrainPoint;

    public Raycast(Camera camera, Matrix4f projectionMatrix, Terrain terrain, Input input) {
        this.camera = camera;
        this.projectionMatrix = projectionMatrix;
        this.input = input;
        this.viewMatrix = MathUtils.createViewMatrix(camera);
        this.terrain = terrain;
    }

    public void update()
    {
        viewMatrix = MathUtils.createViewMatrix(camera);
        currentRay = calculateRay();
        if (intersectionInRange(0, RAY_RANGE, currentRay)) {
            currentTerrainPoint = binarySearch(0, 0, RAY_RANGE, currentRay);
        } else {
            currentTerrainPoint = null;
        }
    }

    private Vector3f calculateRay()
    {
        float mouseX = (float) input.getMouseDx();
        float mouseY = (float) input.getMouseDy();
        Vector2f deviceCoords = getNormalizedDeviceCoordinates(mouseX, mouseY);
        //System.out.println(deviceCoords.x+", "+deviceCoords.y);
        Vector4f clipCoords = new Vector4f(deviceCoords.x, deviceCoords.y, -1f, 1f);
        Vector4f eyeCoords = toEyeCoords(clipCoords);
        Vector3f worldRay = toWorldCoords(eyeCoords);
        return worldRay;
    }

    private Vector3f toWorldCoords(Vector4f eyeCoords)
    {
        Matrix4f invertedView = viewMatrix.invert();
        Vector4f rayWorld = invertedView.transform(eyeCoords);
        Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
        mouseRay.normalize();
        return mouseRay;
    }

    private Vector4f toEyeCoords(Vector4f clipCoords)
    {
        Matrix4f invertedProjection = projectionMatrix.invert();
        Vector4f eyeCoords = invertedProjection.transform(clipCoords);

        return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
    }

    private Vector2f getNormalizedDeviceCoordinates(float mouseX, float mouseY)
    {
        float x = (2f * mouseX) / Constants.DISPLAY_WIDTH - 1f;
        float y = (2f * mouseY) / Constants.DISPLAY_HEIGHT - 1f;

        return new Vector2f(x, -y);
    }

    private Vector3f getPointOnRay(Vector3f ray, float distance) {
        //Vector3f camPos = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
        Vector3f start = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
        Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
        return start.add(scaledRay);
    }

    private Vector3f binarySearch(int count, float start, float finish, Vector3f ray) {
        float half = start + ((finish - start) / 2f);
        if (count >= RECURSION_COUNT) {
            Vector3f endPoint = getPointOnRay(ray, half);
            Terrain terrain = getTerrain(endPoint.x, endPoint.z);
            if (terrain != null) {
                return endPoint;
            } else {
                return null;
            }
        }
        if (intersectionInRange(start, half, ray)) {
            return binarySearch(count + 1, start, half, ray);
        } else {
            return binarySearch(count + 1, half, finish, ray);
        }
    }

    private boolean intersectionInRange(float start, float finish, Vector3f ray) {
        Vector3f startPoint = getPointOnRay(ray, start);
        Vector3f endPoint = getPointOnRay(ray, finish);
        if (!isUnderGround(startPoint) && isUnderGround(endPoint)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean isUnderGround(Vector3f testPoint) {
        Terrain terrain = getTerrain(testPoint.x, testPoint.z);
        float height = 0;
        if (terrain != null) {
            height = terrain.getTerrainHeight(testPoint.x, testPoint.z);
        }
        if (testPoint.y < height) {
            return true;
        } else {
            return false;
        }
    }

    private Terrain getTerrain(float worldX, float worldZ) {
        return terrain;
    }

    public Vector3f getCurrentTerrainPoint() {
        return currentTerrainPoint;
    }

    public Vector3f getCurrentRay() {
        return currentRay;
    }
}

视图空间中的坐标是一个包含3个分量的坐标,分别为
x
y
z
。投影矩阵从视图空间变换到剪辑空间。剪辑空间坐标由4个分量组成
x
y
z
w

剪辑空间坐标可以通过转换为标准化设备坐标。
这意味着
x
y
z
分量被
w
分量除以

如果要从规范化设备空间转换到视图空间,则必须执行反向操作。这意味着您必须通过逆投影矩阵进行变换,并将结果的
x
y
z
分量除以结果的
w
分量

private Vector4f-toeyecords(Vector4f-ndccords)
{
Matrix4f invertedProjection=projectionMatrix.inverte(新Matrix4f());
Vector4f eyeCoords=反向投影变换(clipCoords);
返回新向量4f(eyecords.x/eyecords.w,eyecords.y/eyecords.w,eyecords.z/eyecords.w,0.0f);
}
请阅读:
没有“dest”参数的方法修改此参数并返回此参数!所以,像
Matrix4f invertedproject=projectionMatrix.invert()这样的行是错误的。您可以在这里更改
投影矩阵
。一般的经验法则是:JOML中的任何方法都不会实例化新对象。另外,请查看Matrix4f.unprojectRay()方法。
public class Raycast
{
    private static final int RECURSION_COUNT = 200;
    private static final float RAY_RANGE = 600;

    private Input input;
    private Vector3f currentRay = new Vector3f();

    private Matrix4f projectionMatrix;
    private Matrix4f viewMatrix;

    private Camera camera;
    private Terrain terrain;
    private Vector3f currentTerrainPoint;

    public Raycast(Camera camera, Matrix4f projectionMatrix, Terrain terrain, Input input) {
        this.camera = camera;
        this.projectionMatrix = projectionMatrix;
        this.input = input;
        this.viewMatrix = MathUtils.createViewMatrix(camera);
        this.terrain = terrain;
    }

    public void update()
    {
        viewMatrix = MathUtils.createViewMatrix(camera);
        currentRay = calculateRay();
        if (intersectionInRange(0, RAY_RANGE, currentRay)) {
            currentTerrainPoint = binarySearch(0, 0, RAY_RANGE, currentRay);
        } else {
            currentTerrainPoint = null;
        }
    }

    private Vector3f calculateRay()
    {
        float mouseX = (float) input.getMouseDx();
        float mouseY = (float) input.getMouseDy();
        Vector2f deviceCoords = getNormalizedDeviceCoordinates(mouseX, mouseY);
        //System.out.println(deviceCoords.x+", "+deviceCoords.y);
        Vector4f clipCoords = new Vector4f(deviceCoords.x, deviceCoords.y, -1f, 1f);
        Vector4f eyeCoords = toEyeCoords(clipCoords);
        Vector3f worldRay = toWorldCoords(eyeCoords);
        return worldRay;
    }

    private Vector3f toWorldCoords(Vector4f eyeCoords)
    {
        Matrix4f invertedView = viewMatrix.invert();
        Vector4f rayWorld = invertedView.transform(eyeCoords);
        Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
        mouseRay.normalize();
        return mouseRay;
    }

    private Vector4f toEyeCoords(Vector4f clipCoords)
    {
        Matrix4f invertedProjection = projectionMatrix.invert();
        Vector4f eyeCoords = invertedProjection.transform(clipCoords);

        return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
    }

    private Vector2f getNormalizedDeviceCoordinates(float mouseX, float mouseY)
    {
        float x = (2f * mouseX) / Constants.DISPLAY_WIDTH - 1f;
        float y = (2f * mouseY) / Constants.DISPLAY_HEIGHT - 1f;

        return new Vector2f(x, -y);
    }

    private Vector3f getPointOnRay(Vector3f ray, float distance) {
        //Vector3f camPos = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
        Vector3f start = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
        Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
        return start.add(scaledRay);
    }

    private Vector3f binarySearch(int count, float start, float finish, Vector3f ray) {
        float half = start + ((finish - start) / 2f);
        if (count >= RECURSION_COUNT) {
            Vector3f endPoint = getPointOnRay(ray, half);
            Terrain terrain = getTerrain(endPoint.x, endPoint.z);
            if (terrain != null) {
                return endPoint;
            } else {
                return null;
            }
        }
        if (intersectionInRange(start, half, ray)) {
            return binarySearch(count + 1, start, half, ray);
        } else {
            return binarySearch(count + 1, half, finish, ray);
        }
    }

    private boolean intersectionInRange(float start, float finish, Vector3f ray) {
        Vector3f startPoint = getPointOnRay(ray, start);
        Vector3f endPoint = getPointOnRay(ray, finish);
        if (!isUnderGround(startPoint) && isUnderGround(endPoint)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean isUnderGround(Vector3f testPoint) {
        Terrain terrain = getTerrain(testPoint.x, testPoint.z);
        float height = 0;
        if (terrain != null) {
            height = terrain.getTerrainHeight(testPoint.x, testPoint.z);
        }
        if (testPoint.y < height) {
            return true;
        } else {
            return false;
        }
    }

    private Terrain getTerrain(float worldX, float worldZ) {
        return terrain;
    }

    public Vector3f getCurrentTerrainPoint() {
        return currentTerrainPoint;
    }

    public Vector3f getCurrentRay() {
        return currentRay;
    }
}
public class MathUtils {

    public static float baryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
        float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
        float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
        float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
        float l3 = 1.0f - l1 - l2;
        return l1 * p1.y + l2 * p2.y + l3 * p3.y;
    }

    public static Matrix4f createTransformationMatrix(Vector2f translation, Vector2f scale) {
        Matrix4f matrix = new Matrix4f();
        matrix.identity();
        matrix.translate(translation.x,translation.y,0f);
        matrix.scale(scale.x,scale.y,1f);
        return matrix;
    }

    public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale) {
        Matrix4f transformationMatrix = new Matrix4f();
        transformationMatrix.identity();
        transformationMatrix.translate(translation);
        transformationMatrix.rotate((float) Math.toRadians(rx), 1,0,0);
        transformationMatrix.rotate((float) Math.toRadians(ry), 0,1,0);
        transformationMatrix.rotate((float) Math.toRadians(rz), 0,0,1);
        transformationMatrix.scale(scale);
        return transformationMatrix;
    }

    public static Matrix4f createViewMatrix(Camera camera) {
        Matrix4f viewMatrix = new Matrix4f();
        viewMatrix.identity();
        viewMatrix = viewMatrix.rotate((float) Math.toRadians(camera.getPitch()), 1,0,0);//((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix);
        viewMatrix = viewMatrix.rotate((float) Math.toRadians(camera.getYaw()),0, 1, 0);
        Vector3f cameraPos = new Vector3f(camera.getPosX(), camera.getPosY(), camera.getPosZ());
        Vector3f negativeCameraPos = new Vector3f(-cameraPos.x, -cameraPos.y, -cameraPos.z);
        viewMatrix  = viewMatrix.translate(negativeCameraPos);
        return viewMatrix;
    }

    public static Matrix4f createProjectionMatrix() {
        Matrix4f projectionMatrix = new Matrix4f();
        float aspectRatio = (float) Constants.DISPLAY_WIDTH / (float) Constants.DISPLAY_HEIGHT;
        float fov = Constants.FOV;
        float near = Constants.NEAR_PLANE;
        float far = Constants.FAR_PLANE;
        projectionMatrix = projectionMatrix.perspective((float) java.lang.Math.toRadians(fov), aspectRatio, near, far);
        return projectionMatrix;
    }