patturik
patturik

Reputation: 135

How can I check for intersect with nodes in swift chess app

I'm developing a chess app, using Swift. It's going well, but I'm stuck at the part where I have to check if a piece's move has been blocked by another piece. I've tried to create a SKShapeNode line between the old and the new position of my moving piece, check if it intersects with any other pieces and if so, the move gets blocked. This actually works if I move my piece in a straight line (so horizontal, or vertical) but if I move it diagonally, it somehow doesn't work. I've read about SKShapeNode being a bit buggy, but it was the only way I could think of.

I'm guessing the line intersects with the frames of the SpriteNodes, considering that they are a bit bigger than the pieces itself, but I've even tried to make the chesspieces really small and it still doesn't work.

Hope it's a bit clear what I'm trying to do. Any suggestions on how to handle this would be greatly appreciated! Thanks for reading!

Here's a snippet of what I've tried :

    let path = CGMutablePath()
    path.move(to: CGPoint(x: oldX!, y: oldY!))
    path.addLine(to: CGPoint(x: newX!, y: newY!))
    let shape = SKShapeNode(path: path)
    scene?.addChild(shape)
    for piece in allPieces {
        if piece.intersects(shape) && piece != self.movableNode {
            print("Move intersects other pieces")
            moveAllowed = false
            shape.removeFromParent()
        }
    }

Upvotes: 0

Views: 97

Answers (1)

patturik
patturik

Reputation: 135

So, quick update: I managed to get it working although it's not a very graceful solution. What I did, was make a SKShapeNode, a circle of 1px radius. Whenever I finished a move, I created this SKShapeNode, created an SKaction for it and shoot it from the old position to the new position really fast. In the update function, I checked the position of this shape and checked if it intersected with another piece. If so, I would stop the move and reset the position of the moving piece.

So something like this :

func checkForObstruction() {
    // Fire off a shape to check for obstruction
    shape = SKShapeNode(circleOfRadius: 1)
    shape.alpha = 0
    shape.position = CGPoint (x: oldX!, y: oldY!)
    scene?.addChild(shape)
    var piecesOnSameSquare = 0
    let action = SKAction.move(to: CGPoint(x: newX!, y: newY!), duration: 0.05)
    shape.run(action)
}

then in update

override func update(_ currentTime: TimeInterval) {

    if moveTakingPlace {
        print(shape.position)
        for piece in allPieces {
            if piece.intersects(self.shape) && piece.position != movableNode?.position && piecesOnSameSquare == 0 && !draggedPiece.contains("Knight") {
                print("Intersection!")
                shape.removeFromParent()
                shape.removeAllActions()
                self.finalizeMove(moveAllowed: false)
                break
            } else if piece.position == CGPoint (x: newX!, y: newY!) {
                print("more pieces on target square")
              piecesOnSameSquare += 1
            }
        }
    }
}

I check for some other things in this update method, but this is basically it.. If anyone has any suggestions to make this better, I'm all ears. But for now this works :)

Upvotes: 0

Related Questions