检测SceneKit问题中的冲突

问题描述 投票:0回答:1

我有一些检测碰撞的问题。我想在控制台中看到检测到碰撞。

当生成节点时,我在控制台中看到它们具有哪种类别的BlockMask,因此必须没有问题。还设置了CollisionMask和contactBitMask。

所以对象互相交互没有问题。

如果我更换面具,他们就会停止互相交流,这样他们就会互相通过。所以我可以说面具设置正确。我用helper struct设置的掩码:

struct BitMaskCategory: OptionSet {
    let rawValue: Int

    static let none    = BitMaskCategory(rawValue: 0 << 0) // 0 
    static let box     = BitMaskCategory(rawValue: 1 << 0) // 1
    static let plane   = BitMaskCategory(rawValue: 1 << 1) // 2
}

一个身体是.dynamic physicsBody另一个是.static。

我的ViewController类实现了SCNPhysicsContactDelegate协议,并在viewDidLoad中写道:

sceneView.scene.physicsWorld.contactDelegate = self

之后我尝试使用方法,但它甚至没有被调用:

func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
    if contact.nodeB.physicsBody?.contactTestBitMask == 1 {
        print("NodeB has mask = 1")
    } else {
        print("NodeB has mask != 1")
    }
}

之后,我按下屏幕,看到立方体即将到来并击中平面,但根本没有接触检测。委托方法physicsWorld(_ world:SCNPhysicsWorld,didBegin contact:SCNPhysicsContact)甚至根本不被调用。

我想念的是什么?

谢谢!

ios swift collision-detection scenekit
1个回答
3
投票

经过多个小时的调试挑战后,我发现在设置委托后我重写了场景。

是:

   sceneView.scene.physicsWorld.contactDelegate = self
    let scene = SCNScene()
    sceneView.scene = scene

应该:

    let scene = SCNScene()
    sceneView.scene = scene

    sceneView.scene.physicsWorld.contactDelegate = self

对于我的情况,这解决了问题。希望有人能用这节省时间:)

© www.soinside.com 2019 - 2024. All rights reserved.