diff --git a/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java b/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java index 490be6e..cc12324 100644 --- a/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java +++ b/src/main/java/dev/elliotjarnit/elliotengine/Graphics/RenderingEngine.java @@ -195,6 +195,33 @@ public int getFPS() { return this.fps; } + public EObject getObjectAtPoint(Vector2 point) { + for (EObject object : this.scene.getObjects()) { + for (EFace face : object.getFaces()) { + Vector3[] facePoints = new Vector3[3]; + for (int i = 0; i < 3; i++) { + Vector3 objectSpace = face.getVertices()[i]; + Vector3 worldSpace = this.objectToWorldSpace(objectSpace, object); + Vector3 cameraSpace = this.worldToCameraSpace(worldSpace, this.scene); + Vector3 screenSpace = this.cameraToScreenSpace(cameraSpace, this.scene); + facePoints[i] = screenSpace; + } + + // Don't render face if point is missing + if (facePoints[0] == null || facePoints[1] == null || facePoints[2] == null) continue; + + if (this.pointInTriangle(new Vector3(point.x, point.y, 0), facePoints[0], facePoints[1], facePoints[2])) { + return object; + } + } + } + return null; + } + + private boolean pointInTriangle(Vector3 p, Vector3 A, Vector3 B, Vector3 C){ + return sameSide(A,B,C,p) && sameSide(B,C,A,p) && sameSide(C,A,B,p); + } + private boolean sameSide(Vector3 A, Vector3 B, Vector3 C, Vector3 p){ Vector3 V1V2 = new Vector3(B.x - A.x,B.y - A.y,B.z - A.z); Vector3 V1V3 = new Vector3(C.x - A.x,C.y - A.y,C.z - A.z); diff --git a/src/test/java/BasicCubeDraw/Engine.java b/src/test/java/BasicCubeDraw/Engine.java index 1eaf397..9e82059 100644 --- a/src/test/java/BasicCubeDraw/Engine.java +++ b/src/test/java/BasicCubeDraw/Engine.java @@ -25,7 +25,7 @@ public void optionSetup() { this.setOption(Options.VERSION, "0.0.1"); this.setOption(Options.WINDOW_WIDTH, "800"); this.setOption(Options.WINDOW_HEIGHT, "800"); - this.setOption(Options.LOADING_SCREEN, "true"); + this.setOption(Options.LOADING_SCREEN, "false"); } @Override @@ -55,9 +55,6 @@ public void setup() { 22, 52, 5, 555, 33, 33, 555, 5 }); - - - System.out.println(test.inverse()); } @Override @@ -77,6 +74,13 @@ public void loop() { playerCamera.moveRight(0.5); } + if (this.inputManager.isMouseDown(InputManager.MouseButton.LEFT)) { + System.out.println("Left mouse button down"); + Vector2 mousePos = this.inputManager.getMousePos(); + + System.out.println(this.renderer.getObjectAtPoint(mousePos)); + } + // Mouse movement Vector2 mouseDelta = this.inputManager.getMouseDelta();