Skip to content

Commit

Permalink
Half working get object at point function
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotjarnit committed Jan 16, 2024
1 parent 0cf89d9 commit 6a8c5ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/BasicCubeDraw/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,9 +55,6 @@ public void setup() {
22, 52, 5, 555,
33, 33, 555, 5
});


System.out.println(test.inverse());
}

@Override
Expand All @@ -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();

Expand Down

0 comments on commit 6a8c5ec

Please sign in to comment.