diff --git a/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java b/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java index f55bac4..ad4b63b 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java +++ b/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java @@ -37,6 +37,9 @@ import com.google.ar.core.Trackable; import com.google.ar.core.TrackingState; import com.google.ar.core.examples.java.common.framework.RWT.RWTRenderer; +import com.google.ar.core.examples.java.common.framework.gameMain.OvergroundActor; +import com.google.ar.core.examples.java.common.framework.model3D.BaseObject3D; +import com.google.ar.core.examples.java.common.framework.model3D.CollisionResult; import com.google.ar.core.examples.java.common.framework.model3D.ModelFactory; import com.google.ar.core.examples.java.common.framework.model3D.ModelFileFormatException; import com.google.ar.core.examples.java.common.framework.model3D.Object3D; @@ -48,6 +51,7 @@ import com.google.ar.core.examples.java.common.framework.physics.Ground; import com.google.ar.core.examples.java.common.framework.physics.PhysicalSystem; import com.google.ar.core.examples.java.common.framework.physics.Solid3D; +import com.google.ar.core.examples.java.common.framework.physics.Velocity3D; import com.google.ar.core.examples.java.common.helpers.CameraPermissionHelper; import com.google.ar.core.examples.java.common.helpers.DisplayRotationHelper; import com.google.ar.core.examples.java.common.helpers.FullScreenHelper; @@ -57,6 +61,9 @@ import com.google.ar.core.examples.java.common.java3d.Appearance; import com.google.ar.core.examples.java.common.java3d.Box; import com.google.ar.core.examples.java.common.java3d.Material; +import com.google.ar.core.examples.java.common.java3d.Transform3D; +import com.google.ar.core.examples.java.common.java3d.TransparencyAttributes; +import com.google.ar.core.examples.java.common.java3d.Vector3d; import com.google.ar.core.examples.java.common.rendering.BackgroundRenderer; import com.google.ar.core.examples.java.common.rendering.ObjectRenderer; import com.google.ar.core.examples.java.common.rendering.ObjectRenderer.BlendMode; @@ -71,6 +78,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; @@ -121,9 +131,20 @@ } } + static class SortablePlane { + final float distance; + final Plane plane; + + SortablePlane(float distance, Plane plane) { + this.distance = distance; + this.plane = plane; + } + } + private final ArrayList anchors = new ArrayList<>(); private Collection placedPlanes = new ArrayList<>(); + private Plane placedPlane = null; private float ambient = 0.3f; private float diffuse = 1.0f; @@ -156,7 +177,7 @@ installRequested = false; // For Radish - physicalSystem = new PhysicalSystem(); +// physicalSystem = new PhysicalSystem(); // try { // Object3D andy = ModelFactory.loadModel(getResources(), "models/andy.obj", null).createObject(); // andy.setPosition(new Position3D(0.0, 0.0, -1.0)); @@ -317,9 +338,10 @@ handleTap(frame, camera); // Physics - if (physicalSystem.objects.size() > 0) { - physicalSystem.motion(0, 1, Force3D.ZERO, physicalSystem.objects.get(0).getGravityCenter(), ground); - } + universe.update(10); +// if (physicalSystem.objects.size() > 0) { +// physicalSystem.motion(0, 10, Force3D.ZERO, physicalSystem.objects.get(0).getGravityCenter(), ground); +// } // If frame is ready, render camera preview image to the GL surface. // backgroundRenderer.draw(frame); @@ -363,19 +385,32 @@ } // Visualize planes. - Collection currentPlanes = session.getAllTrackables(Plane.class); - for (Plane plane : currentPlanes) { - if (!placedPlanes.contains(plane)) { - placedPlanes.add(plane); - Box rect = new Box(plane.getExtentX() / 2, 0.1f, plane.getExtentZ() / 2, null); + Collection allPlanes = session.getAllTrackables(Plane.class); + for (Plane plane : allPlanes) { + if (placedPlane == null) { + placedPlane = plane; + Appearance ap = new Appearance(); + ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); + Box rect = new Box(plane.getExtentX() / 2, 0.005f, plane.getExtentZ() / 2, ap); Object3D rectObj = new Object3D("plane", rect); Pose p = plane.getCenterPose(); rectObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); - rectObj.apply(new Quaternion3D((double)p.qx(), (double)p.qy(), (double)p.qz(), (double)p.qw()), false); - if (ground == null) { - ground = new Ground(rectObj); - universe.place(ground); - } + rectObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); + ground = new Ground(rectObj); + universe.place(ground); + } + } + Collection updatedPlanes = frame.getUpdatedTrackables(Plane.class); + for(Plane plane : updatedPlanes) { + if (plane == placedPlane) { + Appearance ap = new Appearance(); + ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); + Box rect = new Box(plane.getExtentX() / 2, 0.005f, plane.getExtentZ() / 2, ap); + Object3D rectObj = new Object3D("plane", rect); + Pose p = plane.getCenterPose(); + rectObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); + rectObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); + ground.updateBody(rectObj); } } @@ -453,14 +488,36 @@ try { Object3D andy = ModelFactory.loadModel(getResources(), "models/andy.obj", null).createObject(); andy.setPosition(new Position3D(px, py + 0.2, pz)); - Solid3D andyObj = new Solid3D(andy); + OvergroundActor andyObj = new OvergroundActor(andy, null) { + public void onIntersect(CollisionResult cr, long interval) { + // めり込んだら(めり込んだ面の法線方向に)押し戻す + Vector3d back = (Vector3d) cr.normal.clone(); + back.scale(cr.length * 2.0); + body.apply(new Position3D(body.getPosition3D().add(back)), false); + + // 速度の面の法線方向の成分を 0 にする + Vector3d v = (Vector3d) ((Solid3D)body).getVelocity().getVector3d().clone(); + double d = v.dot(cr.normal); + v.scaleAdd(-d * 1.5, cr.normal, v); + body.apply(new Velocity3D(v), false); + } + }; +// Solid3D andyObj = new Solid3D(andy); universe.place(andyObj); - physicalSystem.add(andyObj); +// physicalSystem.add(andyObj); } catch (IOException e) { e.printStackTrace(); } catch (ModelFileFormatException e) { e.printStackTrace(); } +// Box b1 = new Box(1.0f, 1.0f, 1.0f, null); +// Object3D obj1 = new Object3D("box", b1); +// obj1.apply(new Position3D(px , py + 0.3, pz), false); +// Solid3D diceObj = new Solid3D(obj1); +// diceObj.scale(0.01, 0.01, 0.01); +// diceObj.setMass(250); +// universe.place(diceObj); +// physicalSystem.add(diceObj); anchors.add(anchor); break; } @@ -477,4 +534,17 @@ } return false; } + + public static float calculateDistanceToPlane(Pose planePose, Pose cameraPose) { + float[] normal = new float[3]; + float cameraX = cameraPose.tx(); + float cameraY = cameraPose.ty(); + float cameraZ = cameraPose.tz(); + // Get transformed Y axis of plane's coordinate system. + planePose.getTransformedAxis(1, 1.0f, normal, 0); + // Compute dot product of plane's normal with vector from camera to plane center. + return (cameraX - planePose.tx()) * normal[0] + + (cameraY - planePose.ty()) * normal[1] + + (cameraZ - planePose.tz()) * normal[2]; + } }