| |
---|
| | import com.google.ar.core.examples.java.common.framework.model3D.Object3D; |
---|
| | import com.google.ar.core.examples.java.common.framework.model3D.Position3D; |
---|
| | import com.google.ar.core.examples.java.common.framework.model3D.Quaternion3D; |
---|
| | import com.google.ar.core.examples.java.common.framework.model3D.Universe; |
---|
| | import com.google.ar.core.examples.java.common.framework.physics.Force3D; |
---|
| | 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; |
---|
| |
---|
| | messageSnackbarHelper.showMessage(this, SEARCHING_PLANE_MESSAGE); |
---|
| | } |
---|
| | |
---|
| | // Visualize planes. |
---|
| | // Collection<Plane> updatedPlanes = frame.getUpdatedTrackables(Plane.class); |
---|
| | // for (Plane plane : updatedPlanes) { |
---|
| | // if (placedPlane == null) { |
---|
| | // placedPlane = plane; |
---|
| | // Appearance ap = new Appearance(); |
---|
| | // ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); |
---|
| | // FloatBuffer vertices = plane.getPolygon(); |
---|
| | // vertices.position(0); |
---|
| | // TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); |
---|
| | // for (int i = 0; i < vertices.remaining() / 2; i++) { |
---|
| | // polygon.setCoordinate(i, new float[]{vertices.get(i * 2), 0.0f, vertices.get(i * 2 + 1)}); |
---|
| | // } |
---|
| | // polyObj = new Object3D("plane", new Shape3D(polygon, ap)); |
---|
| | // Pose p = plane.getCenterPose(); |
---|
| | // polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); |
---|
| | // polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); |
---|
| | // ground = new Ground(polyObj); |
---|
| | // universe.place(ground); |
---|
| | // } else if (plane.equals(placedPlane)) { |
---|
| | // Appearance ap = new Appearance(); |
---|
| | // ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); |
---|
| | // FloatBuffer vertices = plane.getPolygon(); |
---|
| | // vertices.position(0); |
---|
| | // TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); |
---|
| | // for (int i = 0; i < vertices.remaining() / 2; i++) { |
---|
| | // polygon.setCoordinate(i, new float[]{vertices.get(i * 2), 0.0f, vertices.get(i * 2 + 1)}); |
---|
| | // } |
---|
| | // polyObj = new Object3D("plane", new Shape3D(polygon, ap)); |
---|
| | // Pose p = plane.getCenterPose(); |
---|
| | // polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); |
---|
| | // polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); |
---|
| | // universe.displace(ground); |
---|
| | // ground = new Ground(polyObj); |
---|
| | // universe.place(ground); |
---|
| | // } |
---|
| | // } |
---|
| | Collection<Plane> updatedPlanes = frame.getUpdatedTrackables(Plane.class); |
---|
| | for (Plane plane : updatedPlanes) { |
---|
| | if (placedPlane == null) { |
---|
| | placedPlane = plane; |
---|
| | Appearance ap = new Appearance(); |
---|
| | ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0)); |
---|
| | FloatBuffer vertices = plane.getPolygon(); |
---|
| | vertices.position(0); |
---|
| | TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); |
---|
| | for (int i = 0; i < vertices.remaining() / 2; i++) { |
---|
| | polygon.setCoordinate(i, new float[]{vertices.get(i * 2), 0.0f, vertices.get(i * 2 + 1)}); |
---|
| | } |
---|
| | polyObj = new Object3D("plane", new Shape3D(polygon, ap)); |
---|
| | Pose p = plane.getCenterPose(); |
---|
| | polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); |
---|
| | polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); |
---|
| | ground = new Ground(polyObj); |
---|
| | universe.place(ground); |
---|
| | } else if (plane.equals(placedPlane)) { |
---|
| | Appearance ap = new Appearance(); |
---|
| | ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0)); |
---|
| | FloatBuffer vertices = plane.getPolygon(); |
---|
| | vertices.position(0); |
---|
| | TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); |
---|
| | for (int i = 0; i < vertices.remaining() / 2; i++) { |
---|
| | polygon.setCoordinate(i, new float[]{vertices.get(i * 2), 0.0f, vertices.get(i * 2 + 1)}); |
---|
| | } |
---|
| | polyObj = new Object3D("plane", new Shape3D(polygon, ap)); |
---|
| | Pose p = plane.getCenterPose(); |
---|
| | polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); |
---|
| | polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); |
---|
| | universe.displace(ground); |
---|
| | ground = new Ground(polyObj); |
---|
| | universe.place(ground); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| | // 以下は複数検出面に対応したコードだが、検出面が2個以上になるとちゃんと動かない? |
---|
| | // |
---|
| | Collection<Plane> updatedPlanes = frame.getUpdatedTrackables(Plane.class); |
---|
| | if (updatedPlanes != null && updatedPlanes.size() > 0) { |
---|
| | if (ground != null) universe.displace(ground); |
---|
| | ArrayList<Object3D> polygons = new ArrayList<>(); |
---|
| | for (Plane plane : updatedPlanes) { |
---|
| | if (plane.getSubsumedBy() == null) { |
---|
| | Appearance ap = new Appearance(); |
---|
| | ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); |
---|
| | FloatBuffer vertices = plane.getPolygon(); |
---|
| | vertices.position(0); |
---|
| | TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); |
---|
| | for (int j = 0; j < vertices.remaining() / 2; j++) { |
---|
| | polygon.setCoordinate(j, new float[]{vertices.get(j * 2), 0.0f, vertices.get(j * 2 + 1)}); |
---|
| | } |
---|
| | Object3D polyObj = new Object3D("plane" + (polygons.size() + 1), new Shape3D(polygon, ap)); |
---|
| | Pose p = plane.getCenterPose(); |
---|
| | polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); |
---|
| | polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); |
---|
| | polygons.add(polyObj); |
---|
| | } |
---|
| | } |
---|
| | ground = new Ground(new Object3D("ground", polygons.toArray(new Object3D[]{}))); |
---|
| | universe.place(ground); |
---|
| | } |
---|
| | // Collection<Plane> updatedPlanes = frame.getUpdatedTrackables(Plane.class); |
---|
| | // if (updatedPlanes != null && updatedPlanes.size() > 0) { |
---|
| | // if (ground != null) universe.displace(ground); |
---|
| | // ArrayList<Object3D> polygons = new ArrayList<>(); |
---|
| | // for (Plane plane : updatedPlanes) { |
---|
| | // if (plane.getSubsumedBy() == null) { |
---|
| | // Appearance ap = new Appearance(); |
---|
| | // ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); |
---|
| | // FloatBuffer vertices = plane.getPolygon(); |
---|
| | // vertices.position(0); |
---|
| | // TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); |
---|
| | // for (int j = 0; j < vertices.remaining() / 2; j++) { |
---|
| | // polygon.setCoordinate(j, new float[]{vertices.get(j * 2), 0.0f, vertices.get(j * 2 + 1)}); |
---|
| | // } |
---|
| | // Object3D polyObj = new Object3D("plane" + (polygons.size() + 1), new Shape3D(polygon, ap)); |
---|
| | // Pose p = plane.getCenterPose(); |
---|
| | // polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); |
---|
| | // polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); |
---|
| | // polygons.add(polyObj); |
---|
| | // } |
---|
| | // } |
---|
| | // ground = new Ground(new Object3D("ground", polygons.toArray(new Object3D[]{}))); |
---|
| | // universe.place(ground); |
---|
| | // } |
---|
| | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| | |
---|
| | |
---|
| | // planeRenderer.drawPlanes( |
---|
| |
---|
| | Appearance ap = new Appearance(); |
---|
| | Material m = new Material(); |
---|
| | ap.setMaterial(m); |
---|
| | Object3D andy = new Object3D("andy", new Sphere(0.05f, ap)); |
---|
| | // andy.setPosition(new Position3D(px, py + 0.2, pz)); |
---|
| | float viewPoint[] = camera.getDisplayOrientedPose().getTranslation(); |
---|
| | andy.setPosition(new Position3D(viewPoint[0], viewPoint[1], viewPoint[2])); |
---|
| | 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); |
---|
| | back.scale(cr.length); |
---|
| | 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); |
---|
| | v.scaleAdd(-d * 1.35, cr.normal, v); |
---|
| | //摩擦 |
---|
| | double u = 0.8; |
---|
| | v.x = v.x * u; |
---|
| | v.z = v.z * u; |
---|
| | body.apply(new Velocity3D(v), false); |
---|
| | } |
---|
| | }; |
---|
| | float viewLine[] = camera.getDisplayOrientedPose().getZAxis(); |
---|
| | andyObj.setVelocity(new Velocity3D(-viewLine[0], -viewLine[1], -1.7 * viewLine[2])); |
---|
| | // Solid3D andyObj = new Solid3D(andy); |
---|
| | //オブジェクトを飛ばす強さ |
---|
| | double power = 1.7; |
---|
| | andyObj.setVelocity(new Velocity3D(-viewLine[0] * power, -viewLine[1], -viewLine[2] * power)); |
---|
| | universe.place(andyObj); |
---|
| | // physicalSystem.add(andyObj); |
---|
| | // } catch (IOException e) { |
---|
| | // e.printStackTrace(); |
---|
| | // } catch (ModelFileFormatException e) { |
---|
| | // e.printStackTrace(); |
---|
| |
---|
| | |