From 8b8497b53b4bb2e51479d4ad86329c521900d7cf Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 6 Oct 2023 15:33:52 +0200 Subject: [PATCH] Mousepicker with offset in selection --- client/src/main/java/core/engine/Engine.java | 10 ++--- .../main/java/core/engine/entity/Camera.java | 2 +- .../java/core/engine/terrain/TerrainTile.java | 8 ++++ .../java/core/engine/toolbox/MousePicker.java | 39 ++++++++++++++++--- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/client/src/main/java/core/engine/Engine.java b/client/src/main/java/core/engine/Engine.java index 61ff0f8..e3838ea 100644 --- a/client/src/main/java/core/engine/Engine.java +++ b/client/src/main/java/core/engine/Engine.java @@ -138,14 +138,14 @@ public class Engine { MousePicker mousePicker = new MousePicker(camera, renderer.getProjectionMatrix(), terrain); input(camera, renderer, mousePicker); while ( !glfwWindowShouldClose(window) ) { - DoubleBuffer posX = BufferUtils.createDoubleBuffer(1); - DoubleBuffer posY = BufferUtils.createDoubleBuffer(1); - glfwGetCursorPos(window, posX, posY); - mousePicker.update((float) posX.get(0), (float) posY.get(0)); renderer.processTerrain(terrain); //renderer.processEntity(hexagonEntity); renderer.render(light, camera); + DoubleBuffer posX = BufferUtils.createDoubleBuffer(1); + DoubleBuffer posY = BufferUtils.createDoubleBuffer(1); + glfwGetCursorPos(window, posX, posY); + mousePicker.update((float) posX.get(0), (float) posY.get(0)); glfwSwapBuffers(window); // swap the color buffers // Poll for window events. The key callback above will only be // invoked during this call. @@ -187,7 +187,7 @@ public class Engine { glfwGetCursorPos(window, posX, posY); System.out.println(posX.get(0) + "|" + posY.get(0)); mousePicker.update((float) posX.get(0), (float) posY.get(0)); - System.out.println(mousePicker.getCurrentRay()); + mousePicker.calcIntersectingTerrainTile(); } } }; diff --git a/client/src/main/java/core/engine/entity/Camera.java b/client/src/main/java/core/engine/entity/Camera.java index 2399148..c900ceb 100644 --- a/client/src/main/java/core/engine/entity/Camera.java +++ b/client/src/main/java/core/engine/entity/Camera.java @@ -4,7 +4,7 @@ import utils.vectors.Vector3f; public class Camera { - private Vector3f position = new Vector3f(0,37,-1); + private Vector3f position = new Vector3f(0, 37,0); private float pitch = 90; private float yaw = 180; private float roll; diff --git a/client/src/main/java/core/engine/terrain/TerrainTile.java b/client/src/main/java/core/engine/terrain/TerrainTile.java index 1778f59..dc06ca3 100644 --- a/client/src/main/java/core/engine/terrain/TerrainTile.java +++ b/client/src/main/java/core/engine/terrain/TerrainTile.java @@ -85,4 +85,12 @@ public class TerrainTile { public void setColor(Vector3f color) { this.color = color; } + + public int getRow() { + return row; + } + + public int getColumn() { + return column; + } } \ No newline at end of file diff --git a/client/src/main/java/core/engine/toolbox/MousePicker.java b/client/src/main/java/core/engine/toolbox/MousePicker.java index 80525d3..f5dc7b7 100644 --- a/client/src/main/java/core/engine/toolbox/MousePicker.java +++ b/client/src/main/java/core/engine/toolbox/MousePicker.java @@ -10,9 +10,6 @@ import utils.vectors.*; public class MousePicker { - private static final int RECURSION_COUNT = 200; - private static final float RAY_RANGE = 600; - private Vector3f currentRay = new Vector3f(); private Matrix4f projectionMatrix; @@ -42,13 +39,21 @@ public class MousePicker { viewMatrix = MatrixGraphicUtils.createViewMatrix(camera); currentRay = calculateMouseRay(mouseX, mouseY); currentTerrainPoint = calcCurrentTerrainIntersection(); + currentTerrainTile = calcIntersectingTerrainTile(); + if(currentTerrainTile != null) { + currentTerrainTile.setColor(new Vector3f(1f, 1f, 1f)); + } + } private Vector3f calculateMouseRay(float mouseX, float mouseY) { - Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY); Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f); + + // Transform the clip coordinates into eye coordinates using the projection matrix Vector4f eyeCoords = toEyeCoords(clipCoords); + + // Transform the eye coordinates into world coordinates using the view matrix return toWorldCoords(eyeCoords); } @@ -56,7 +61,6 @@ public class MousePicker { Matrix4f invertedView = Matrix4f.invert(viewMatrix, null); Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null); Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z); - mouseRay.normalise(); return mouseRay; } @@ -76,10 +80,33 @@ public class MousePicker { private Vector3f calcCurrentTerrainIntersection() { Vector3f camPos = camera.getPosition(); - return null; + float rayFactor = -camPos.y / currentRay.y; + + float terrainX = camPos.x + rayFactor * currentRay.x; + float terrainY = camPos.y + rayFactor * currentRay.y; + float terrainZ = camPos.z + rayFactor * currentRay.z; + + return new Vector3f(terrainX, terrainY, terrainZ); } //********************************************************** + public TerrainTile calcIntersectingTerrainTile() { + for(TerrainTile terrainTile : terrain.getTerrainTiles()) { + float distanceX = (float) Math.pow(currentTerrainPoint.x - terrainTile.getPosition().x, 2); + float distanceZ = (float) Math.pow(-currentTerrainPoint.z - terrainTile.getPosition().z, 2); + + float radius = (float) Math.pow(1, 2); + + if(distanceX + distanceZ < radius) { + if(!terrainTile.getColor().equals(new Vector3f(1f, 1f, 1f))) { + System.out.println("Center: " + terrainTile.getPosition()); + System.out.println("TerrainPoint: " + currentTerrainPoint); + } + return terrainTile; + } + } + return null; + } }