1

I'm currently trying to build an AR Chess app and I'm having trouble getting the movement of the pieces working.

I would like to be able to tap on a chess piece, then the legal moves it can make on the chess board will be highlighted and it will move to whichever square the user tapped on.

Pic of the chess board design and nodes: https://gyazo.com/2a88f9cda3f127301ed9b4a44f8be047

What I would like to implement: https://i.stack.imgur.com/oAr5K.jpg

Would greatly appreciate any suggestions on how to get this working.

Thanks!

ViewController Code:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the view's delegate
        sceneView.delegate = self

        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true

        // Add lighting to the scene
        sceneView.autoenablesDefaultLighting = true
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration to track an external image
        let configuration = ARImageTrackingConfiguration()

        // Image detection
        // Reference which group to find the image to detect in the Assets folder e.g. "Detection Card"
        if let imageDetect = ARReferenceImage.referenceImages(inGroupNamed: "Detection Card", bundle: Bundle.main) {
            // Sets image tracking properties to the image in the referenced group
            configuration.trackingImages = imageDetect
            // Amount of images to be tracked
            configuration.maximumNumberOfTrackedImages = 1
        }

        // Run the view's session
        sceneView.session.run(configuration)
    }

    // Run when horizontal surface is detected and display 3D object onto image
    // ARAnchor - tells a certain point in world space is relevant to your app, makes virtual content appear "attached" to some real-world point of interest
    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode {
        // Creates 3D object
        let obj = SCNNode()
        // Check if image detected through camera is an ARImageAnchor - which contains position and orientation data about the image detected in the session
        if let imageAnchor = anchor as? ARImageAnchor {
            // Set dimensions of the horizontal plane to be displayed onto the image to be the same as the image uploaded
            let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
            // Display mild transparent layer onto detected image
            // This is to ensure image detection works by display a faint layer on the image
            plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.2)
            // Set geometry shape of the plane
            let planeNode = SCNNode(geometry: plane)
            // Flip vertical plane to horizontal plane
            planeNode.eulerAngles.x = -Float.pi / 2
            obj.addChildNode(planeNode)

            // Initialise chess scene
            if let chessBoardSCN = SCNScene(named: "art.scnassets/chess.scn") {
                // If there is a first in the scene file
                if let chessNodes = chessBoardSCN.rootNode.childNodes.first {
                    // Displays chessboard upright
                    chessNodes.eulerAngles.x = Float.pi / 2
                    // Adds chessboard to the overall 3D scene
                    obj.addChildNode(chessNodes)
                }
            }

        }

        return obj

    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Pause the view's session
        sceneView.session.pause()
    }

}
munjyong
  • 55
  • 3
  • 9

2 Answers2

1

You will need to add gestures on to your view and use the ARSceneViews hitTest method to detect what the gesture is touching in your scene. You can then update the positions based on the movement from the gestures.

Here is a question that deals with roughly the same requirement of dragging nodes around.

Placing, Dragging and Removing SCNNodes in ARKit

Alan S
  • 594
  • 3
  • 13
  • I have added a tap gesture and logged into the console if a node is tapped, but the thing I'm having trouble with now is getting it to detect individual pieces and not the whole chess board. I linked a gyazo image in my post of how I structured my pieces and tiles. How would you recommend I get hit detection on the individual pieces from here? Thanks in advance! Appreciate you taking the time to respond to my post – munjyong Nov 26 '18 at 14:40
  • You will need to place some type of identifier on each of the SCNNodes, including the chessboard. When you do the hitTest, if you retrieve a node, you can check on what the name of this piece is and then move accordingly based on that. imo, you might have to subclass the SCNNode class and add some properties such as it's current chessboard position, type of piece, etc... It might help making identification easier. Also in the other question, I had done it by just checking on the name of the node. Also, no problem. AR was tough for me to learn too and sometimes it's hard getting help. – Alan S Nov 26 '18 at 15:15
1

First, you need to add a gesture recognizer for tap into your viewDidLoad, like this:

 let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
 myScnView.addGestureRecognizer(tapGesture)

Then realize the handler function:

    @objc
    func handleTap(_ gestureRecognize: UIGestureRecognizer) {
       // HERE YOU NEED TO DETECT THE TAP
       // check what nodes are tapped

        let location = gestureRecognize.location(in: myScnView)
        let hitResults = myScnView.hitTest(location, options: [:])

        // check that we clicked on at least one object
        if hitResults.count > 0 {
            // retrieved the first clicked object
            let tappedPiece = hitResults[0].node

            // HERE YOU CAN SHOW POSSIBLE MOVES
            //Ex. showPossibleMoves(for: tappedPiece) 
        }

    }

Now, to show the possible moves, you need to identify all quadrants and your node position on the chessboard.

To do this, you can assign a name or a number, or a combination of letter and number, or moreover a combination of numbers. (I suggest combination of number, like row 1 column 1, like a matrix).

let's take my suggestion, so you can name each quadrant 1.1 1.2 ... 2.1 2.2 and so on.

Now, to detect where your piece is, you can check contact with the PhysicsContactDelegate.

Now you have the tappedPiece and the place where it is, so you have to define the rule for the pieces, for example:

let rules = ["tower":"cross"] //add the others

N.B You can choose what you want to define the rules.

Let's take my suggestion for good, now you should create the function to highlight:

 func highlight(quadrant: SCNNode){
   quadrant.geometry?.firstMaterial?.emission.contents = UIColor.yellow
 }

Finally the showPossibleMoves(for: tappedPiece) could be something this:

func showPossibleMoves(for piece: SCNNode){

let pieceType = piece.name //You have to give the name as you did into your rules variable
 //ex. if you have rules like ["tower":"cross"] you have to set all towers name to "tower"

let rule = rules[pieceType]

switch rule{

case "cross":

      //you have to highlight all nodes on the right, left, above and bottom
      // you can achieve this by selecting the start point and increase it
      //assuming you named your quadrants like 1.1 1.2 or 11 12 13 ecc...
      let startRow = Int(startQuadrant.name.first)
      let startColumn = Int(startQuadrant.name.last)

      //Now loop the highlight on right
      for column in startColumn+1...MAX_COLUMN-1{
         let quadrant = myScnView.scene.rootNode.childNode(withName:"\(startRow).\(column)" , recursively: true)
         // call highlight function
          highlight(quadrant: quadrant)
      }

      //Now loop for above quadrants
      for row in startRow+1...MAX_ROW-1{
         let quadrant = myScnView.scene.rootNode.childNode(withName:"\(row).\(startColumn)" , recursively: true)
         // call highlight function
          highlight(quadrant: quadrant)
      }

      //DO THE SAME FOR ALL DIRECTIONS
}

// ADD ALL CASES, like bishop movements "diagonals" and so on

}

NOTE: In the handlerTap function you have to check what you're tapping, for example, to check if you're tapping on a quadrant after selecting a piece (you want to move you're piece) you can check a boolean value and the name of the selected node

  //assuming you have set the boolean value after selecting a piece

  if pieceSelected && node.name != "tower"{

      //HERE YOU CAN MOVE YOUR PIECE

  }
Andrew21111
  • 868
  • 8
  • 17
  • Hi, firstly thank you so much for your thorough response, I appreciate it must've taken a while to write everything in that reply. I still have a couple of questions: I'm thinking of implementing the quadrants as the names of the squares in the official chess rules e.g. A1 or H8... but I'm having trouble understanding the what you mean by defining the rules e.g. "let rules = ["tower":"cross"]" - I'm not very familiar of the notation in the square brackets, what does that mean? – munjyong Nov 26 '18 at 14:16
  • Also, idk if you viewed the gyazo snapshot of my project so far but I have already individually named each tile and piece to the corresponding square it starts on, each piece and tile are 3d models which I converted and imported into my project as a .scn file so that I can use it in my project. How would you recommend I maneuver and links the objects to my code as I think this is the concept I'm really struggling to grasp. – munjyong Nov 26 '18 at 14:27
  • Sorry for bugging you so much, but I've spent all day trying to figure out how to move the chess pieces and I'm getting nowhere so far. – munjyong Nov 26 '18 at 18:18
  • Hi, unfortunately I don't have much time, but never be sorry for asking. This -> "let rules = ["tower":"cross"]" is a dictionary (or HashMap) a key-value data structure. I suggested it only to switch piece rule case, but you can avoid this by switching rule by piece. For your second comment, I don't understand what do you mean? If you need to link 3D model to a variable, you can use sceneView.scene.rootNode.childWithName. By using A1 ecc.. the logic is the same as I said. However, If you have quadrant and piece, you can make piece do an action using SCNAction – Andrew21111 Nov 27 '18 at 17:02