Edo*_*Cal 5 collision nodes skphysicsbody swift arkit
我创建了两个节点:一个球体和一个盒子:
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?
Run Code Online (Sandbox Code Playgroud)
我想要两个节点或physicsBody进行交互,所以我为categoryBitMask和创建了一个类别contactTestBitMask:
struct CollisionCategory: OptionSet {
let rawValue: Int
static let box = CollisionCategory(rawValue: 1)
static let sphere = CollisionCategory(rawValue: 2)
}
Run Code Online (Sandbox Code Playgroud)
在这里,我将盒子节点设置为物理体:
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
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我在渲染函数中设置了球体节点,您可以在视图中移动:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将协议添加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
}
Run Code Online (Sandbox Code Playgroud)
然后我添加了这个功能:
func physicsWorld(_ world: SCNPhysicsWorld, didEnd contact: SCNPhysicsContact) {
print("Collision!")
}
Run Code Online (Sandbox Code Playgroud)
这就是发生的事情.
当两个节点碰撞时没有任何反应,所以我不知道这两个物体是否在接触.问题可能是关于.kinematic,.static还是关于函数render()?我在ARKit中逐步跟踪了有关碰撞的不同教程:教程1,教程2.我不知道它为什么不像预期的那样工作.我的代码中有什么问题吗?
下载文件代码链接: https ://ufile.io/20sla
willRenderScene每次渲染场景时,每秒都会调用 uptown 60 次。由于每次都会重新创建物理体,因此可能会弄乱物理引擎确定碰撞。
尝试更改代码以在设置期间仅创建物理体一次。
| 归档时间: |
|
| 查看次数: |
188 次 |
| 最近记录: |