|
@@ -3,14 +3,24 @@ package xyz.tbvns.Inputs;
|
|
|
import com.badlogic.gdx.Gdx;
|
|
|
import com.badlogic.gdx.Input;
|
|
|
import com.badlogic.gdx.graphics.Camera;
|
|
|
+import com.badlogic.gdx.graphics.Color;
|
|
|
+import com.badlogic.gdx.graphics.g3d.Material;
|
|
|
+import com.badlogic.gdx.graphics.g3d.Model;
|
|
|
+import com.badlogic.gdx.graphics.g3d.ModelInstance;
|
|
|
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
|
|
|
+import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
|
|
|
+import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
|
|
|
+import com.badlogic.gdx.math.Intersector;
|
|
|
import com.badlogic.gdx.math.Octree;
|
|
|
import com.badlogic.gdx.math.Vector3;
|
|
|
+import com.badlogic.gdx.math.collision.BoundingBox;
|
|
|
import com.badlogic.gdx.math.collision.Ray;
|
|
|
import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
|
|
|
import xyz.tbvns.Editor.ModelsManager;
|
|
|
import xyz.tbvns.Main;
|
|
|
import xyz.tbvns.Physics.BulletManager;
|
|
|
+import xyz.tbvns.ui.UIManager;
|
|
|
+import xyz.tbvns.ui.Windows.ObjectEdit;
|
|
|
|
|
|
public class inputManager extends CameraInputController {
|
|
|
private float startX, startY;
|
|
@@ -69,23 +79,62 @@ public class inputManager extends CameraInputController {
|
|
|
|
|
|
@Override
|
|
|
public boolean touchDown(float x, float y, int pointer, int button) {
|
|
|
- x-=100;
|
|
|
- y+=100;
|
|
|
- Vector3 unprojected = Main.getCamera().unproject(new Vector3(x, y, 0));
|
|
|
- Vector3 pos = Main.getCamera().position;
|
|
|
-
|
|
|
- float x3 = unprojected.x + (unprojected.x - pos.x) * 10000;
|
|
|
- float y3 = unprojected.y + (unprojected.y - pos.y) * 10000;
|
|
|
- float z3 = unprojected.z + (unprojected.z - pos.z) * 10000;
|
|
|
-
|
|
|
- btCollisionObject object = BulletManager.raycast(pos, new Vector3(x3, y3, z3));
|
|
|
- if (object != null) {
|
|
|
- BulletManager.getModelCollisiontHashMap().forEach((k, v) -> {
|
|
|
- if (v == object) {
|
|
|
- ModelsManager.select(k);
|
|
|
- }
|
|
|
- });
|
|
|
+ x-=200;
|
|
|
+
|
|
|
+ Ray ray = Main.getCamera().getPickRay(x, y, 0, 200, Main.getCamera().viewportWidth, Main.getCamera().viewportHeight);
|
|
|
+
|
|
|
+ Vector3 rayStart = ray.origin;
|
|
|
+ Vector3 rayEnd = ray.origin.cpy().add(ray.direction.cpy().scl(1000f)); // Long ray to ensure it reaches objects
|
|
|
+
|
|
|
+ ray.set(rayStart, rayEnd);
|
|
|
+
|
|
|
+ int hit = 0;
|
|
|
+
|
|
|
+ for (ModelInstance model : ModelsManager.getLoadedModels()) {
|
|
|
+ BoundingBox boundingBox = new BoundingBox();
|
|
|
+ model.calculateBoundingBox(boundingBox);
|
|
|
+ Vector3 pos = model.transform.getTranslation(Vector3.Zero);
|
|
|
+
|
|
|
+ Vector3 min = new Vector3(pos.x + boundingBox.min.x, pos.y + boundingBox.min.y, pos.z + + boundingBox.min.z);
|
|
|
+ Vector3 max = new Vector3(pos.x + boundingBox.max.x, pos.y + boundingBox.max.y, pos.z + + boundingBox.max.z);
|
|
|
+
|
|
|
+ boundingBox.set(min, max);
|
|
|
+ if (Intersector.intersectRayBoundsFast(ray, boundingBox)) {
|
|
|
+ ModelsManager.select(model);
|
|
|
+ UIManager.getActiveUI().put(ObjectEdit.class, true);
|
|
|
+ hit++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hit == 0) {
|
|
|
+ ModelsManager.deselect();
|
|
|
+ UIManager.getActiveUI().put(ObjectEdit.class, false);
|
|
|
}
|
|
|
+// Uncomment for debug line for raycast
|
|
|
+// ModelBuilder modelBuilder = new ModelBuilder();
|
|
|
+// modelBuilder.begin();
|
|
|
+// MeshPartBuilder builder = modelBuilder.part("line", 1, 3, new Material());
|
|
|
+// builder.setColor(Color.RED);
|
|
|
+// builder.line(rayStart, rayEnd);
|
|
|
+// Model lineModel = modelBuilder.end();
|
|
|
+// Main.LineModel = lineModel;
|
|
|
+
|
|
|
+// This doesn't work for some reason.
|
|
|
+// TODO: Make this work
|
|
|
+
|
|
|
+// btCollisionObject object = BulletManager.raycast(rayStart, rayEnd);
|
|
|
+// if (object != null) {
|
|
|
+// BulletManager.getModelCollisiontHashMap().forEach((k, v) -> {
|
|
|
+// if (v == object) {
|
|
|
+// ModelsManager.select(k);
|
|
|
+// UIManager.getActiveUI().put(ObjectEdit.class, true);
|
|
|
+// }
|
|
|
+// });
|
|
|
+// } else {
|
|
|
+// if (ModelsManager.getSelectedModelInstance() != null) {
|
|
|
+// ModelsManager.deselect();
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
return super.touchDown(x, y, pointer, button);
|
|
|
}
|