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, 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()? 下载文件代码链接:https://ufile.io/20sla 解决方法
每次渲染场景时,willRenderScene都会被称为uptown 60次.因为你每次重建物理体时都会重新发现物理引擎确定碰撞.
尝试将代码更改为仅在设置期间创建物理主体一次. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

