如何获得攻丝锚的参考?

时间:2018-12-30 22:02:13

标签: android arcore sceneform

我正在使用Sceneform开发一个应用程序,该程序可实现跳棋游戏。在成功定位了运动场和检查器之后,我遇到了一个问题:我无法获得任何参考,无法将这些棋子从一个锚点移动到另一个锚点。

我如何参考棋盘格来实现动作?

我已经尝试了他们在https://developers.google.com/ar/reference上的建议,但都没有成功,我同时搜索了ARCore API和Sceneform API。我也尝试过使用手势处理程序,但是什么也没做。

在我用来插入棋盘的代码段以及要附加棋子的锚点下面:

arFragment.setOnTapArPlaneListener(
            (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {

    [....]

    // Create the anchor plane
    float s = 0f;
    for (int i = 0; i < 8; i++) {
        float l = 0f;
        for (int k = 0; k < 8; k++) {
            boardMatrix[i][k] = generateAnchor(anchor, plane, -0.175f + s, -0.180f + l);
            l += 0.051f;
        }
        s += 0.050f;
    }

    // Place 3D Model
    placeObject(arFragment, anchor, chessboard);
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 3; j++) {
            if ((j == 0 || j == 2) && i % 2 == 1) placeObject(arFragment, boardMatrix[i][j], pawn);
            if (j == 1 && i % 2 == 0) placeObject(arFragment, boardMatrix[i][j], pawn);
        }
        for (int j = 7; j > 4; j--) {
            if ((j == 7 || j == 5) && i % 2 == 0) placeObject(arFragment, boardMatrix[i][j], pawn);
            if (j == 6 && i % 2 == 1) placeObject(arFragment, boardMatrix[i][j], pawn);
        }
     }

     [....]

}

还附加了一段代码,用于在棋子上方执行水龙头后尝试获取锚点的位置:

private int[] selected(Anchor anchor) {

    int[] position = new int[2];

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (boardMatrix[i][j].getPose() == anchor.getPose())
                position[0] = i;
            position[1] = j;
            Log.d("Tag", "Selected: " + position[0] + " , " + position[1]);
            return position;
        }
    }

    return null;
}

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();

            Collection<Anchor> collection = trackable.getAnchors();
            currentSelected = (Anchor) collection.toArray()[0];
            boardPosition = selected(currentSelected);
            Log.d("Tap", "Index of position: " + boardPosition[0] + ", " + boardPosition[1]);
       }
    }
}

我希望当我点击一个时会收到棋子(或锚点)的坐标,但是Sceneform不允许我处理手势。

0 个答案:

没有答案