didBegin is not called

Hello,


My goal is that "hit" will be output, but without changing the positions of `card` and `anotherCard`. They should touch each other, but not move. However didBegin is not called.


Struct:


struct physicBodyCharacters {
       
        static let cardNumber = 00000001 //1
        static let anotherCardNumber = 00000010 //2
        static let nobodyNumber = 00000100 //4
    }


in viewDidLoad():


gameScene2.physicsWorld.gravity = CGVector(dx: 0, dy: -9.81)
        gameScene2.physicsWorld.contactDelegate = self


First Node:


card = SKSpriteNode(texture: cardTexture)
        card.position = CGPoint(x: gameScene2.size.width / 2 + 150, y: 95)
        card.zPosition = 3
        card.setScale(1)
        card.physicsBody = SKPhysicsBody(texture: cardTexture, size: card.size)
        card.physicsBody?.affectedByGravity = false
        card.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.cardNumber)
        card.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
        card.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.anotherCardNumber)


Second Node:


anotherCard = SKSpriteNode(texture: anotherCardTexture)
        anotherCard.position = CGPoint(x: 31 , y: 532)
        anotherCard.zPosition = 2
        anotherCard.setScale(1)
        anotherCard.physicsBody = SKPhysicsBody(texture: anotherCardTexture, size: battlefieldCard0.size)
        anotherCard.physicsBody?.affectedByGravity = false
        anotherCard.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
        anotherCard.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
        anotherCard.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.cardNumber)


didBegin() Function:


func didBegin(_ contact: SKPhysicsContact) {
        print("contact")
        let contanctMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
   
        switch contanctMask
        {
        case UInt32(physicBodyCharacters.cardNumber) | UInt32(physicBodyCharacters.anotherCardNumber):
            print("hit")
        default:
            break
        }
    }


For each answer I am very grateful.

Replies

Do you move the cards ? Do you see "contact" printed ?


I'm not sure the contact is detected if the cards statically overlap.

I move the cards using...

override func touchesBegan()


but "contact" is unfortunately not outputed.

I think the ContactTestBitMask on both the cards have to be the same. I think the contact test bit mask is more of a layer, all objects on that layer send a notification to your code. Otherwise they just interact with each other without notifying you.