Home > Back-end >  Collision between two nodes happened twice at the same time
Collision between two nodes happened twice at the same time

Time:12-29

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

CodePudding user response:

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.

  • Related