3

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")
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
GDog
  • 117
  • 1
  • 8

2 Answers2

0

To start with, both avatarCategory and obstacleCategory are 0, because : UInt32 = 0*1 << 1 = 0, so let’s fix that:

let avatarCategory: UInt32 = 1 << 0
let obstacleCategory: UInt32 = 1 << 1

Now contactTestBitMask represents the object(s) you want to be notified about contacts with, so you need to change that:

avatar.physicsBody?.contactTestBitMask = obstacleCategory

and

obstacle.physicsBody?.contactTestBitMask = avatarCategor

Try that for now

Edit: my step-by-step guide for collisions and contacts: https://stackoverflow.com/a/51041474/1430420

And a guide to collision and contactTest bit masks: https://stackoverflow.com/a/40596890/1430420

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • hmm.. but it still doesn't seem to work :// Do I have to put some code in the AppDelegate or somewhere else in the PList? – GDog Jul 13 '18 at 22:11
  • @GDog Can you try the step-by-step guide linked to in the answer? – Steve Ives Jul 14 '18 at 08:12
  • @GDog You’re initialising ‘obstacle‘ as an empty ‘SKShapeNode‘ and its physicsBody is also empty - that might be your problem. – Steve Ives Jul 14 '18 at 08:24
0

It's not working because you need to set size of your SKPhysicsBody() Example circle: SKPhysicsBody(circleOfRadius: 20)

zdravko zdravkin
  • 2,090
  • 19
  • 21