Ramprasad
Ramprasad

Reputation: 8071

ARCore how to add Virtual object as soon as horizontal plane detected

In ARCore sample,How to add Virtual object as soon as horizontal plane detected.I tried following code but not works,

// Check if we detected at least one plane. If so, hide the loading message.
      if (messageSnackbar != null) {
        for (Plane plane : session.getAllTrackables(Plane.class)) {
          if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING
                  && plane.getTrackingState() == TrackingState.TRACKING) {
            hideLoadingMessage();
            //frame.hitTest(plane.getExtentX(),plane.getExtentZ());

            long downTime = SystemClock.uptimeMillis();
            long eventTime = SystemClock.uptimeMillis() + 100;

            int metaState = 0;
            MotionEvent motionEvent = MotionEvent.obtain(
                    downTime,
                    eventTime,
                    MotionEvent.ACTION_DOWN,
                    plane.getCenterPose().qx(),
                    plane.getCenterPose().qy(),
                    metaState
            );
            //surfaceView.dispatchTouchEvent(motionEvent);
            onSingleTap(motionEvent);
            break;
          }
        }
      }

Upvotes: 0

Views: 1071

Answers (2)

peresisUser
peresisUser

Reputation: 1686

try this in onDrawFrame():

inside this loop:

 for (Plane plane : session.getAllTrackables(Plane.class)) {
      if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING
          && plane.getTrackingState() == TrackingState.TRACKING) {
        break;
      }
    }

add this:

 for (Plane plane : session.getAllTrackables(Plane.class)) {
      if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING
          && plane.getTrackingState() == TrackingState.TRACKING) {

          if (newAnchor == null) {
            newAnchor = plane.createAnchor(plane.getCenterPose());
          }

        break;
      }

make sure newAnchor is a member variable you don't add more anchors each call to onDrawFrame().

Upvotes: 1

Alon S
Alon S

Reputation: 195

As far as I remember you should change TrackingState.TRACKING to TrackingState.NEW

It's not documented yet.

Upvotes: 0

Related Questions