raycast cursor

This commit is contained in:
2025-04-27 20:12:38 +02:00
parent 1b61eca58b
commit cbbce43ede
4 changed files with 74 additions and 5 deletions

View File

@@ -1,7 +1,9 @@
package chess.view.DDDrender;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Vector4f;
public class Camera {
public static final float fov = 70.0f;
@@ -14,7 +16,6 @@ public class Camera {
private final float distance = 1.5f;
private final float camHeight = 1.5f;
private float aspectRatio;
private float angle;
@@ -22,7 +23,7 @@ public class Camera {
public Camera() {
this.pos = new Vector3f(0.0f, camHeight, 0.0f);
this.angle = 0.0f;
setRotateAngle(0.0f);
}
public void move(float x, float y) {
@@ -80,4 +81,22 @@ public class Camera {
public Matrix4f getViewMatrix() {
return new Matrix4f().lookAt(pos, center, up);
}
public Vector2f getCursorWorldFloorPos(Vector2f screenPos, int windowWidth, int windowHeight) {
float relativeX = (screenPos.x / (float) windowWidth * 2.0f) - 1.0f;
float relativeY = 1.0f - (screenPos.y / (float) windowHeight * 2.0f);
Vector4f rayClip = new Vector4f(relativeX, relativeY, -1.0f, 1.0f);
Vector4f rayEye = getPerspectiveMatrix().invert().transform(rayClip);
rayEye = new Vector4f(rayEye.x, rayEye.y, -1.0f, 0.0f);
Vector4f rayWorld = getViewMatrix().invert().transform(rayEye);
float lambda = -this.pos.y / rayWorld.y;
return new Vector2f(lambda * rayWorld.x + this.pos.x, lambda *
rayWorld.z + this.pos.z);
}
}