Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Tap on Point? #23

Open
rongguodong opened this issue May 14, 2018 · 1 comment
Open

Tap on Point? #23

rongguodong opened this issue May 14, 2018 · 1 comment
Labels
feature request New feature or request

Comments

@rongguodong
Copy link

We have BaseArFragment.OnTapArPlaneListener now, but missing BaseArFragment.OnTapArPointListener. So it is not easy to use Sceneform to reproduce the same app as hello_ar_java example.

Can you add a BaseArFragment.OnTapArPointListener, or more general BaseArFragment.OnTapArTrackableListener?

@claywilkinson
Copy link
Contributor

Thanks for the feedback! I'll pass it along as an enhancement request (it sounds like a great idea).

In the mean time, you can override the processing via setOnPeekTouchListener()

Declare your own gesture detector in the activity:

    private GestureDetector trackableGestureDetector;

In onCreate(), install your own onPeekTouchListener. This is called for each touch to possibly handle interactions with the TransformableNodes.

    arFragment.getArSceneView().getScene().setOnPeekTouchListener(this::handleOnTouch);
    this.trackableGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            public boolean onSingleTapUp(MotionEvent e) {
               onSingleTap(e);
                return true;
            }

        public boolean onDown(MotionEvent e) {
                return true;
            }
        });

Then implement handleOnTouch:

    private void handleOnTouch(HitTestResult hitTestResult, MotionEvent motionEvent) {
        // First call ArFragment's listener to handle TransformableNodes.
        arFragment.onPeekTouch(hitTestResult, motionEvent);

        // Check for touching a Sceneform node
        if (hitTestResult.getNode() != null) {
           return;
        }

        // Otherwise call gesture detector.
        trackableGestureDetector.onTouchEvent(motionEvent);
    }

Finally implement the onSingleTap() method called by the gesture dectector. In this method, call ARCore's frame.hittest() to get the hits on trackables.

    private void onSingleTap(MotionEvent motionEvent) {
        Frame frame = arFragment.getArSceneView().getArFrame();
        if (frame != null && motionEvent != null && frame.getCamera().getTrackingState() == TrackingState.TRACKING) {
            for (HitResult hit : frame.hitTest(motionEvent)) {
                Trackable trackable = hit.getTrackable();
                if (trackable instanceof Plane && ((Plane)trackable).isPoseInPolygon(hit.getHitPose())) {
                    Plane plane = (Plane)trackable;

                    // Handle plane hits.
                    break;
                } else if (trackable instanceof Point ) {
                    // Handle point hits
                    Point point = (Point) trackable;

                } else if (trackable instanceof AugmentedImage) {
                    // Handle image hits.
                    AugmentedImage image = (AugmentedImage) trackable;
                }
            }
        }
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants