加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 未检测到两个节点之间的冲突ARKit

发布时间:2020-12-14 04:56:43 所属栏目:百科 来源:网络整理
导读:我创建了两个节点:一个球体和一个盒子: var sphere = SCNNode(geometry: SCNSphere(radius: 0.005)) //I get the box node from scn file let boxScene = SCNScene(named: "art.scnassets/world.scn")! var boxNode: SCNNode? 我想要两个节点或physicsBody
我创建了两个节点:一个球体和一个盒子:

var sphere = SCNNode(geometry: SCNSphere(radius: 0.005))
    //I get the box node from scn file
    let boxScene = SCNScene(named: "art.scnassets/world.scn")!
    var boxNode: SCNNode?

我想要两个节点或physicsBody进行交互,所以我为categoryBitMask和contactTestBitMask创建了一个类别:

struct CollisionCategory: OptionSet {
    let rawValue: Int
    static let box = CollisionCategory(rawValue: 1)
    static let sphere = CollisionCategory(rawValue: 2)
}

在这里,我将盒子节点设置为物理体:

self.boxScene.rootNode.enumerateChildNodes { (node,_) in
    if node.name == "box" {
        boxNode = node
        let boxBodyShape = SCNPhysicsShape(geometry: SCNBox(width: 0.1,height: 0.1,length: 0.1,chamferRadius: 0.1),options: nil)
        let physicsBody = SCNPhysicsBody(type: .static,shape: boxBodyShape)
        boxNode!.physicsBody = physicsBody
        boxNode!.physicsBody?.categoryBitMask = CollisionCategory.box.rawValue
        boxNode!.physicsBody?.contactTestBitMask = CollisionCategory.sphere.rawValue
        boxNode!.physicsBody?.collisionBitMask = boxNode!.physicsBody!.contactTestBitMask
    }
}

在这里,我在渲染函数中设置了球体节点,您可以在视图中移动:

func setUpSphere() {
         let sphereBodySphere = SCNPhysicsShape(geometry: SCNSphere(radius: 0.005))
         let physicsBody = SCNPhysicsBody(type: .kinematic,shape: sphereBodySphere)
         sphere.physicsBody = physicsBody
         sphere.physicsBody?.categoryBitMask = CollisionCategory.sphere.rawValue
         sphere.physicsBody?.contactTestBitMask = CollisionCategory.box.rawValue
         sphere.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
         sphere.physicsBody?.collisionBitMask = sphere.physicsBody!.contactTestBitMask
         previousPoint = currentPosition 
}

///It Adds a sphere and changes his position
func renderer(_ renderer: SCNSceneRenderer,willRenderScene scene: SCNScene,atTime time: TimeInterval) {
    guard let pointOfView = sceneView.pointOfView else { return }
    let mat = pointOfView.transform
    let dir = SCNVector3(-1 * mat.m31,-1 * mat.m32,-1 * mat.m33)
    let currentPosition = pointOfView.position + (dir * 0.185)

    if buttonPressed {
        if let previousPoint = previousPoint {
            sphere.position = currentPosition
            sceneView.scene.rootNode.addChildNode(sphere)
        }
     }
}

我将协议SCNPhysicsContactDelegate添加到ViewController,
我在ViewDidLoad()中设置:

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.delegate = self
    sceneView.scene.physicsWorld.contactDelegate = self
    ///I correctly see the shapes of the sphere and the box physics            bodies using
    sceneView.debugOptions = .showPhysicsShapes
    createBox()
    setUpSphere()
    sceneView.scene = boxScene
    sceneView.scene.physicsWorld.contactDelegate = self
}

然后我添加了这个功能:

func physicsWorld(_ world: SCNPhysicsWorld,didEnd contact: SCNPhysicsContact) {
        print("Collision!")
}

enter image description here

这就是发生的事情.

当两个节点碰撞时没有任何反应,所以我不知道这两个物体是否在接触.问题可能是关于.kinematic,.static还是关于函数render()?
我在ARKit:Tutorial 1,Tutorial 2中逐步跟踪了有关碰撞的不同教程.
我不知道它为什么不像预期的那样工作.
我的代码中有什么问题吗?

下载文件代码链接:https://ufile.io/20sla

解决方法

每次渲染场景时,willRenderScene都会被称为uptown 60次.因为你每次重建物理体时都会重新发现物理引擎确定碰撞.

尝试将代码更改为仅在设置期间创建物理主体一次.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读