在SceneKit问题中检测冲突 [英] Detecting collisions in SceneKit issue

查看:55
本文介绍了在SceneKit问题中检测冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检测碰撞时遇到一些问题.我想在控制台中看到检测到冲突.

I have some issues with detection of collisions. I want to see in the console that collision is detected.

生成节点时,我会在控制台中看到它们具有什么样的categoryBitMask,因此肯定没有问题.CollisionMask和contactBitMask也已设置.

When nodes are generated I see in console what kind of categoryBitMask they have, so there is must be no problem. CollisionMask and contactBitMask are set as well.

因此对象之间可以毫无问题地进行交互.

So objects interact with each other with no problem.

如果我更换口罩,它们将停止彼此交互,因此它们会互相穿过.所以我可以说口罩设置正确.我使用辅助结构设置的蒙版:

If I change masks they stop interacting with each other so they go through each other. So I can say that mask set properly. Masks I set with the 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 物理,另一个是 .static.

One body is .dynamic physicsBody other is .static.

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

My ViewController class implement SCNPhysicsContactDelegate protocol and in viewDidLoad I write:

sceneView.scene.physicsWorld.contactDelegate = self

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

After that I try to use method but it is not even called:

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)甚至根本没有被调用.

After that I press on screen and see cubes are coming and hit plane surface but there is no contact detection at all. Delegate method physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) even is not called at all.

我想念什么?

谢谢!

推荐答案

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

After multiple hour debug challenge I found that I rewrite scene after I set delegate.

原是:

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

应该是:

    let scene = SCNScene()
    sceneView.scene = scene

    sceneView.scene.physicsWorld.contactDelegate = self

对于我来说,这可以解决问题.希望有人可以节省几个小时:)

For my case this fixes the problem. Hope someone will save hours with this :)

这篇关于在SceneKit问题中检测冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆