Partial Intersection Calculation

This commit is contained in:
Sebastian 2023-10-06 11:44:25 +02:00
parent 3d4d0485f3
commit fb3d4e3685
2 changed files with 8 additions and 52 deletions

View File

@ -123,7 +123,7 @@ public class Engine {
Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1)); Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1));
//Generate Simple, Flat Terrain //Generate Simple, Flat Terrain
Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("white")), 16, 32, 7); Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("white")), 16, 16, 7);
//Create Hexagon //Create Hexagon
HexagonModel hexagonModel = new HexagonModel(); HexagonModel hexagonModel = new HexagonModel();

View File

@ -3,6 +3,7 @@ package core.engine.toolbox;
import core.engine.Engine; import core.engine.Engine;
import core.engine.entity.Camera; import core.engine.entity.Camera;
import core.engine.terrain.Terrain; import core.engine.terrain.Terrain;
import core.engine.terrain.TerrainTile;
import org.lwjgl.system.windows.MOUSEINPUT; import org.lwjgl.system.windows.MOUSEINPUT;
import utils.MatrixGraphicUtils; import utils.MatrixGraphicUtils;
import utils.vectors.*; import utils.vectors.*;
@ -20,6 +21,7 @@ public class MousePicker {
private Terrain terrain; private Terrain terrain;
private Vector3f currentTerrainPoint; private Vector3f currentTerrainPoint;
private TerrainTile currentTerrainTile;
public MousePicker(Camera cam, Matrix4f projection, Terrain terrain) { public MousePicker(Camera cam, Matrix4f projection, Terrain terrain) {
camera = cam; camera = cam;
@ -39,11 +41,7 @@ public class MousePicker {
public void update(float mouseX, float mouseY) { public void update(float mouseX, float mouseY) {
viewMatrix = MatrixGraphicUtils.createViewMatrix(camera); viewMatrix = MatrixGraphicUtils.createViewMatrix(camera);
currentRay = calculateMouseRay(mouseX, mouseY); currentRay = calculateMouseRay(mouseX, mouseY);
if (intersectionInRange(0, RAY_RANGE, currentRay)) { currentTerrainPoint = calcCurrentTerrainIntersection();
currentTerrainPoint = binarySearch(0, 0, RAY_RANGE, currentRay);
} else {
currentTerrainPoint = null;
}
} }
private Vector3f calculateMouseRay(float mouseX, float mouseY) { private Vector3f calculateMouseRay(float mouseX, float mouseY) {
@ -51,9 +49,7 @@ public class MousePicker {
Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY); Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f); Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
Vector4f eyeCoords = toEyeCoords(clipCoords); Vector4f eyeCoords = toEyeCoords(clipCoords);
Vector3f worldRay = toWorldCoords(eyeCoords); return toWorldCoords(eyeCoords);
System.out.println(worldRay);
return worldRay;
} }
private Vector3f toWorldCoords(Vector4f eyeCoords) { private Vector3f toWorldCoords(Vector4f eyeCoords) {
@ -78,52 +74,12 @@ public class MousePicker {
//********************************************************** //**********************************************************
private Vector3f getPointOnRay(Vector3f ray, float distance) { private Vector3f calcCurrentTerrainIntersection() {
Vector3f camPos = camera.getPosition(); Vector3f camPos = camera.getPosition();
Vector3f start = new Vector3f(camPos.x, camPos.y, camPos.z);
Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
return Vector3f.add(start, scaledRay, null);
}
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.getX(), endPoint.getZ());
if (terrain != null) {
return endPoint;
} else {
return null; 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.getX(), testPoint.getZ());
float height = 0;
if (testPoint.y < height) {
return true;
} else {
return false;
}
}
private Terrain getTerrain(float worldX, float worldZ) {
return terrain;
}
} }