两个节点之间的碰撞同时发生两次

Collision between two nodes happened twice at the same time

在ARkit中,我有两个节点,例如子弹节点和目标节点,

项目符号节点在下方

  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

目标节点在下方

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")

我发现当这两个节点相互碰撞时,下面的方法几乎同时连续调用了两次,但它应该只发生一次。

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

}

有时会发生,有时不会,所以我认为可能应该添加一些碰撞精度,有人知道该怎么做吗?谢谢

didBegin(contact:) 似乎为 2 个节点之间的每个接触点调用,所以是的 - 它可能被调用不止一次。

根据您在他们联系时所做的事情,将决定如何对您的联系逻辑进行编码以考虑到这一点。