【问题标题】:Collision between two nodes happened twice at the same time两个节点之间的冲突同时发生了两次
【发布时间】:2022-01-24 05:50:51
【问题描述】:

在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) {

}

有时会发生,有时没有,所以我认为可能应该添加一些碰撞精度,有人知道如何做那部分吗?谢谢

【问题讨论】:

标签: xcode sprite-kit augmented-reality game-physics arkit


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多