Fix offset when camera moving (offset with camera rotating remains)
This commit is contained in:
parent
8b8497b53b
commit
be82d76bba
@ -138,6 +138,7 @@ public class Engine {
|
|||||||
MousePicker mousePicker = new MousePicker(camera, renderer.getProjectionMatrix(), terrain);
|
MousePicker mousePicker = new MousePicker(camera, renderer.getProjectionMatrix(), terrain);
|
||||||
input(camera, renderer, mousePicker);
|
input(camera, renderer, mousePicker);
|
||||||
while ( !glfwWindowShouldClose(window) ) {
|
while ( !glfwWindowShouldClose(window) ) {
|
||||||
|
|
||||||
renderer.processTerrain(terrain);
|
renderer.processTerrain(terrain);
|
||||||
|
|
||||||
//renderer.processEntity(hexagonEntity);
|
//renderer.processEntity(hexagonEntity);
|
||||||
@ -185,9 +186,7 @@ public class Engine {
|
|||||||
DoubleBuffer posX = BufferUtils.createDoubleBuffer(1);
|
DoubleBuffer posX = BufferUtils.createDoubleBuffer(1);
|
||||||
DoubleBuffer posY = BufferUtils.createDoubleBuffer(1);
|
DoubleBuffer posY = BufferUtils.createDoubleBuffer(1);
|
||||||
glfwGetCursorPos(window, posX, posY);
|
glfwGetCursorPos(window, posX, posY);
|
||||||
System.out.println(posX.get(0) + "|" + posY.get(0));
|
|
||||||
mousePicker.update((float) posX.get(0), (float) posY.get(0));
|
mousePicker.update((float) posX.get(0), (float) posY.get(0));
|
||||||
mousePicker.calcIntersectingTerrainTile();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -10,6 +10,9 @@ import utils.vectors.*;
|
|||||||
|
|
||||||
public class MousePicker {
|
public class MousePicker {
|
||||||
|
|
||||||
|
private static final int RECURSION_COUNT = 200;
|
||||||
|
private static final float RAY_RANGE = 600;
|
||||||
|
|
||||||
private Vector3f currentRay = new Vector3f();
|
private Vector3f currentRay = new Vector3f();
|
||||||
|
|
||||||
private Matrix4f projectionMatrix;
|
private Matrix4f projectionMatrix;
|
||||||
@ -18,7 +21,6 @@ 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;
|
||||||
@ -38,10 +40,15 @@ 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);
|
||||||
currentTerrainPoint = calcCurrentTerrainIntersection();
|
if (intersectionInRange(0, RAY_RANGE, currentRay)) {
|
||||||
currentTerrainTile = calcIntersectingTerrainTile();
|
currentTerrainPoint = binarySearch(0, 0, RAY_RANGE, currentRay);
|
||||||
if(currentTerrainTile != null) {
|
} else {
|
||||||
currentTerrainTile.setColor(new Vector3f(1f, 1f, 1f));
|
currentTerrainPoint = null;
|
||||||
|
}
|
||||||
|
System.out.println(currentTerrainPoint);
|
||||||
|
TerrainTile terrainTile = calcIntersectingTerrainTile();
|
||||||
|
if(terrainTile != null) {
|
||||||
|
terrainTile.setColor(new Vector3f(1f, 1f, 1f));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -49,18 +56,16 @@ public class MousePicker {
|
|||||||
private Vector3f calculateMouseRay(float mouseX, float mouseY) {
|
private Vector3f calculateMouseRay(float mouseX, float mouseY) {
|
||||||
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);
|
||||||
|
|
||||||
// Transform the clip coordinates into eye coordinates using the projection matrix
|
|
||||||
Vector4f eyeCoords = toEyeCoords(clipCoords);
|
Vector4f eyeCoords = toEyeCoords(clipCoords);
|
||||||
|
Vector3f worldRay = toWorldCoords(eyeCoords);
|
||||||
// Transform the eye coordinates into world coordinates using the view matrix
|
return worldRay;
|
||||||
return toWorldCoords(eyeCoords);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector3f toWorldCoords(Vector4f eyeCoords) {
|
private Vector3f toWorldCoords(Vector4f eyeCoords) {
|
||||||
Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
|
Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
|
||||||
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
|
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
|
||||||
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
|
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
|
||||||
|
mouseRay.normalise();
|
||||||
return mouseRay;
|
return mouseRay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,18 +83,55 @@ public class MousePicker {
|
|||||||
|
|
||||||
//**********************************************************
|
//**********************************************************
|
||||||
|
|
||||||
private Vector3f calcCurrentTerrainIntersection() {
|
private Vector3f getPointOnRay(Vector3f ray, float distance) {
|
||||||
Vector3f camPos = camera.getPosition();
|
Vector3f camPos = camera.getPosition();
|
||||||
float rayFactor = -camPos.y / currentRay.y;
|
Vector3f start = new Vector3f(camPos.x, camPos.y, -camPos.z);
|
||||||
|
Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
|
||||||
float terrainX = camPos.x + rayFactor * currentRay.x;
|
return Vector3f.add(start, scaledRay, null);
|
||||||
float terrainY = camPos.y + rayFactor * currentRay.y;
|
}
|
||||||
float terrainZ = camPos.z + rayFactor * currentRay.z;
|
|
||||||
|
private Vector3f binarySearch(int count, float start, float finish, Vector3f ray) {
|
||||||
return new Vector3f(terrainX, terrainY, terrainZ);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//**********************************************************
|
|
||||||
public TerrainTile calcIntersectingTerrainTile() {
|
public TerrainTile calcIntersectingTerrainTile() {
|
||||||
|
|
||||||
for(TerrainTile terrainTile : terrain.getTerrainTiles()) {
|
for(TerrainTile terrainTile : terrain.getTerrainTiles()) {
|
||||||
@ -109,4 +151,5 @@ public class MousePicker {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user