I wanted to have a collision detection between the avatar and the obstacle, so whenever something collides, it should print "collision", but even that doesn't work, so the problem is, that it doesn't detect any collision. And if it would collide, it should differentiate between the player and the obstacle.
class GameScene: SKScene, SKPhysicsContactDelegate {
let avatar = SKShapeNode(circleOfRadius: 20)
let avatarCategory: UInt32 = 0*1 << 0
let obstacleCategory: UInt32 = 0*1 << 1
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
createAvatar()
spawnObstacles()
}
func createAvatar() {
avatar.name = "avatarNode"
avatar.physicsBody = SKPhysicsBody()
avatar.physicsBody?.categoryBitMask = avatarCategory
avatar.physicsBody?.contactTestBitMask = avatarCategory
avatar.physicsBody?.collisionBitMask = 0
avatar.physicsBody?.usesPreciseCollisionDetection = true
avatar.physicsBody?.affectedByGravity = false
avatar.zPosition = 2
addChild(avatar)
}
func createRandomObstacle() {
let obstacle = SKShapeNode()
obstacle.name = "obstacleNode"
obstacle.physicsBody = SKPhysicsBody()
obstacle.physicsBody?.categoryBitMask = obstacleCategory
obstacle.physicsBody?.contactTestBitMask = obstacleCategory
obstacle.physicsBody?.collisionBitMask = 0
obstacle.physicsBody?.usesPreciseCollisionDetection = true
obstacle.physicsBody?.affectedByGravity = false
obstacle.zPosition = 2
addChild(obstacle)
}
func didBegin(_ contact: SKPhysicsContact) {
print("collision")
}