如何解决ARKit 和 .dae 文件,对象之间的奇怪空间
在我使用 ARKit 和 sceneKit 的学习项目中,我在使用 .dae 文件时遇到了一个奇怪的行为。
我使用以下方法创建了 2 个对象,一个立方体和一个平面(都具有物理特性):
func loadBox() {
let ballGeometry = SCNBox(width: 0.2,height: 0.2,length: 0.2,chamferRadius: 0.02)
let mat = SCNMaterial()
mat.diffuse.contents = UIImage(named: "ball")
ballGeometry.materials = [mat]
//Physic
let physSchape = SCNPhysicsShape(geometry: ballGeometry,options: nil)
let ballPhysic = SCNPhysicsBody(type: .dynamic,shape: physSchape)
ballPhysic.mass = 100
ballPhysic.friction = 0.8
ballPhysic.isAffectedByGravity = true
let nodeBall = SCNNode(geometry: ballGeometry)
nodeBall.name = "ball"
nodeBall.physicsBody = ballPhysic
box = nodeBall
}
func loadPlane(){
let geometry = SCNPlane(width: 1,height: 1)
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "texture")
geometry.materials = [material]
// physics
let physSchape = SCNPhysicsShape(geometry: geometry,options: nil)
let planePhysic = SCNPhysicsBody(type: .static,shape: physSchape)
planePhysic.restitution = 0.0
planePhysic.friction = 1.0
planePhysic.isAffectedByGravity = true
let nodo = SCNNode(geometry: geometry)
nodo.eulerAngles.x = -.pi / 2
nodo.physicsBody = planePhysic
plane = nodo
}
当我将盒子放在场景中的平面上时,它们正确定位并相互接触,如下图所示:
问题来了:
如果我使用从互联网下载的模型作为飞机的 .dae 文件,当将其插入场景时,它会在盒子和飞机之间创建一个奇怪的空间(见图)
我用来加载 scn 文件的方法。
// load the elicopterBase
func loadElicopterBase(){
guard let scene = SCNScene(named: "Model.scnassets/base.scn") else {fatalError()}
guard let nodo = scene.rootNode.childNode(withName: "Plane",recursively: true) else {fatalError()}
// physics
let physSchape = SCNPhysicsShape(node: nodo,shape: physSchape)
planePhysic.restitution = 0.0
planePhysic.friction = 1.0
planePhysic.isAffectedByGravity = true
nodo.physicsBody = planePhysic
nodo.name = "base"
DispatchQueue.main.async {
self.eliporto = nodo
}
}
看起来 .dae 文件周围有某种不可见的 boundingBox。 我可以寻找什么或在哪里找到解决方案。
这里是 Xcode 中 .dae 文件的一些图片
解决方法
像这样制作“concavePolyhedron”类型的地板平面:
let body = SCNPhysicsBody(type: .static,shape: SCNPhysicsShape(geometry: yourGeometry,options: [SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron]))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。