1

In ARkit, I have two nodes such as bullet node and target node,

bullet node is below

  let body = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: bullet,  options: nil))
        
        body.isAffectedByGravity = false
        bullet.physicsBody = body
        bullet.physicsBody?.applyForce(SCNVector3(orientation.x*power, orientation.y*power, orientation.z*power), asImpulse: true)
        bullet.physicsBody?.categoryBitMask = BitMaskCategory.bullet.rawValue
        bullet.physicsBody?.contactTestBitMask = BitMaskCategory.target.rawValue
        bullet.physicsBody?.collisionBitMask = BitMaskCategory.target.rawValue

Target node is below

let boundingSphere = targetNode!.boundingSphere;
        let radius = boundingSphere.radius;
        let center = boundingSphere.center;
        let sphereShape = SCNSphere(radius: CGFloat(radius));
        var shape = SCNPhysicsShape(geometry: sphereShape, options: [SCNPhysicsShape.Option.scale : targetNodeScale * 1.1])
        targetNode?.physicsBody = SCNPhysicsBody(type: .dynamic,shape: shape)
        targetNode?.physicsBody?.isAffectedByGravity = false
        targetNode?.physicsBody?.categoryBitMask = BitMaskCategory.target.rawValue
        targetNode?.physicsBody?.contactTestBitMask = BitMaskCategory.bullet.rawValue | BitMaskCategory.target.rawValue
        targetNode?.physicsBody?.collisionBitMask = BitMaskCategory.target.rawValue | BitMaskCategory.bullet.rawValue
        targetNode?.setValue(0, forKey: "hitCount")

And I found when these two nodes hit each other, the below method was invoked twice almost at the same time consecutively but it should only happen once.

func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {

}

And sometimes it happened, some times didn't, so I assume might be there are some collision precision should be added, does anyone know how to do that part? Thanks

hui
  • 101
  • 8
  • Does this answer your question? [SpriteKit game crashes when two SKPhysicsContacts are detected](https://stackoverflow.com/questions/43058602/spritekit-game-crashes-when-two-skphysicscontacts-are-detected) – Steve Ives Dec 24 '21 at 09:38
  • This explains it. its expected. https://stackoverflow.com/questions/39424122/why-is-scenekits-physicsworld-didbegincontact-firing-multiple-times-for-a-singl – hasan Dec 24 '21 at 12:09
  • 1
    Thank you Steve and Hasan – hui Dec 28 '21 at 23:28

1 Answers1

1

didBegin(contact:) appears to be called for every point of contact between 2 nodes, so yes - it may get called more than once.

Depending upon what you do when they contact will determine how to code your contact logic to take this into account.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55