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 positionfunc 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!")}
这就是发生的事情.
当两个节点碰撞时没有任何反应,所以我不知道这两个物体是否在接触.问题可能是关于.kinematic,.static还是关于函数render()?
我在ARKit:Tutorial 1,Tutorial 2中逐步跟踪了有关碰撞的不同教程.
我不知道它为什么不像预期的那样工作.
我的代码中有什么问题吗?
下载文件代码链接:https://ufile.io/20sla
解决方法 每次渲染场景时,willRenderScene都会被称为uptown 60次.因为你每次重建物理体时都会重新发现物理引擎确定碰撞.尝试将代码更改为仅在设置期间创建物理主体一次.
总结以上是内存溢出为你收集整理的swift – 未检测到两个节点之间的冲突ARKit全部内容,希望文章能够帮你解决swift – 未检测到两个节点之间的冲突ARKit所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)