diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-17 12:12:01 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-17 12:12:01 -0400 |
commit | 180aad05decc7eefa87e4e45d6747c48f40e5361 (patch) | |
tree | 51545197f7c94b4022acab880772c9f4fc65db0e /src/main/java/net/tylermurphy/Minecraft/Util/MousePicker.java | |
download | minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.gz minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.bz2 minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.zip |
Diffstat (limited to 'src/main/java/net/tylermurphy/Minecraft/Util/MousePicker.java')
-rwxr-xr-x | src/main/java/net/tylermurphy/Minecraft/Util/MousePicker.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main/java/net/tylermurphy/Minecraft/Util/MousePicker.java b/src/main/java/net/tylermurphy/Minecraft/Util/MousePicker.java new file mode 100755 index 0000000..d35a1d1 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Util/MousePicker.java @@ -0,0 +1,73 @@ +package net.tylermurphy.Minecraft.Util;
+
+import net.tylermurphy.Minecraft.Render.Data.Display;
+import org.joml.Matrix4f;
+import org.joml.Vector2f;
+import org.joml.Vector3f;
+import org.joml.Vector4f;
+
+import net.tylermurphy.Minecraft.Chunk.Cube;
+import net.tylermurphy.Minecraft.Scene.World;
+
+public class MousePicker {
+
+ public static Vector3f breakPos = new Vector3f();
+ public static Vector3f placePos = new Vector3f();
+
+ private static Matrix4f projectionMatrix;
+ private static Matrix4f viewMatrix;
+
+ public static void init(Matrix4f projection) {
+ projectionMatrix = projection;
+ }
+
+ public static void update() {
+ viewMatrix = Maths.createViewMatrix(World.camera);
+ Vector3f currentRay = calculateMouseRay();
+ breakPos = search(currentRay, World.player.getTransform().getPosition(), 0);
+ if(breakPos == null) placePos = null;
+ }
+
+ private static Vector3f calculateMouseRay() {
+ float mouseX = (float) Display.getWidth()/2;
+ float mouseY = (float) Display.getHeight()/2;
+ Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
+ Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
+ Vector4f eyeCoords = toEyeCoords(clipCoords);
+ return toWorldCoords(eyeCoords);
+ }
+
+ private static Vector3f toWorldCoords(Vector4f eyeCoords) {
+ Matrix4f invertedView = viewMatrix;
+ Vector4f rayWorld = invertedView.invert().transform(eyeCoords);;
+ Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
+ mouseRay.normalize();
+ return mouseRay;
+ }
+
+ private static Vector4f toEyeCoords(Vector4f clipCoords) {
+ Matrix4f invertedProjection = projectionMatrix;
+ Vector4f eyeCoords = invertedProjection.invert().transform(clipCoords);
+ return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
+ }
+
+ private static Vector2f getNormalisedDeviceCoordinates(float mouseX, float mouseY) {
+ float x = (2.0f * mouseX) / Display.getWidth() - 1f;
+ float y = (2.0f * mouseY) / Display.getHeight() - 1f;
+ return new Vector2f(x, y);
+ }
+
+ private static Vector3f search(Vector3f ray, Vector3f position, int recursionCount) {
+ Vector3f ppos = World.player.getTransform().getPosition();
+ if(Math.abs(position.x-ppos.x) > 4 || Math.abs(position.y-ppos.y) > 4 || Math.abs(position.z-ppos.z) > 4 ) return null;
+ if(recursionCount > 200) return null;
+ int intX = Math.round(position.x);
+ int intY = Math.round(position.y+1);
+ int intZ = Math.round(position.z);
+ if(World.getBlock(intX, intY, intZ) != Cube.AIR && World.getBlock(intX, intY, intZ) != 17)
+ return new Vector3f(intX,intY,intZ);
+ placePos = new Vector3f(intX,intY,intZ);
+ return search(ray,new Vector3f(position.x+ray.x/4,position.y+ray.y/4,position.z+ray.z/4), recursionCount++);
+ }
+
+}
|