Reputation: 528
I am trying to achieve collision detection between a SKShapeNode
(a circlular node) and SKShapeNode
(a square node), just like the app "Snake VS Blocks".
Below is my code snippet of my node creations
private func addSnake() {
snake = SKShapeNode(circleOfRadius: Constants.snakeRadius)
snake.fillColor = Constants.snakeColor
let snakeXPosition = (Helper.getScreenWidth() / 2.0) - Constants.snakeRadius
snake.position = CGPoint(x: snakeXPosition, y: size.height * 0.3)
snake.physicsBody = SKPhysicsBody(rectangleOf: snake.frame.size)
snake.physicsBody?.isDynamic = false
snake.physicsBody?.categoryBitMask = PhysicsCategory.snake
snake.physicsBody?.contactTestBitMask = PhysicsCategory.block
snake.physicsBody?.collisionBitMask = PhysicsCategory.none
snake.name = "Snake"
addChild(snake)
}
func addBlocks() {
for index in 0...(blockCount-1) {
let hasBlock = getRandomBool()
if hasBlock {
let block = getBlock(for: index)
block.fillColor = .red
addChild(block)
let actionMove = getBlockMoveAction(for: block, atIndex: index)
let actionMoveDone = SKAction.removeFromParent()
block.run(SKAction.sequence([actionMove, actionMoveDone]))
}
}
}
func getBlockMoveAction(for block: SKShapeNode, atIndex: Int) -> SKAction {
return SKAction.move(to: CGPoint(x: CGFloat(atIndex) * width, y: -block.frame.size.height),
duration: TimeInterval(blockMovementDuration))
}
func getBlock(for index: Int) -> SKShapeNode {
let rect = CGRect(x: 0, y: 0, width: width, height: width)
let block = SKShapeNode(rect: rect, cornerRadius: Constants.blockCornerRadius)
block.name = "Block"
block.position = CGPoint(x: CGFloat(index) * width, y: (Helper.getScreenHeight() + width))
block.physicsBody = SKPhysicsBody(rectangleOf: block.frame.size)
block.physicsBody?.isDynamic = false
block.physicsBody?.categoryBitMask = PhysicsCategory.block
block.physicsBody?.contactTestBitMask = PhysicsCategory.snake
block.physicsBody?.collisionBitMask = PhysicsCategory.none
return block
}
func getRandomBool() -> Bool {
return arc4random_uniform(2) == 0
}
Below is the image of the outcome of above code:
My first problem is, I don't know why, but when I enabled, skview to show physics,
view.showsPhysics = true
i found out my view is not in correct frame
Secondly, I have confirmed to contact delegate as well
physicsWorld.contactDelegate = self
But my
didBegin(_ contact: SKPhysicsContact)
is not getting triggered
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA //snake
secondBody = contact.bodyB //block
} else {
firstBody = contact.bodyB //block
secondBody = contact.bodyA //snake
}
if ((firstBody.categoryBitMask & PhysicsCategory.snake != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.block != 0)) {
if let snake = firstBody.node as? SKShapeNode,
let block = secondBody.node as? SKShapeNode {
snakeDidCollideWithBlock(snake: snake, block: block)
}
}
}
func snakeDidCollideWithBlock(snake: SKShapeNode, block: SKShapeNode) {
print("Hit")
block.removeFromParent()
}
}
I went through other questions in stackoverflow searching for similar problem, but didnt find any solution.
Upvotes: 0
Views: 217
Reputation: 11531
Either one or both "isDynamic" should be true. Otherwise, they will behave like a wall and never participate in a physical simulation.
Upvotes: 2