【发布时间】:2018-04-24 15:22:18
【问题描述】:
我是ARKit 的新手,我正在尝试制作一个相对简单的应用程序,您可以在其中创建一个“墙”,用户向它“扔”球,然后它会反弹回来。
我将墙创建为SCNPlane,用户将相机指向这样的位置:
private func createWall(withPosition position: SCNVector3) -> SCNNode {
let wallGeometry = SCNPlane(width: 0.3, height: 0.3)
wallGeometry.firstMaterial?.isDoubleSided = true
wallGeometry.firstMaterial?.diffuse.contents = UIColor.green.withAlphaComponent(0.7)
let parentNode = SCNNode(geometry: wallGeometry)
let (position, _) = cameraPosition()
parentNode.position = position
parentNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: wallGeometry, options: nil))
parentNode.physicsBody?.isAffectedByGravity = false
self.wall = parentNode
return parentNode
}
我用这个函数得到相机的方向和位置:cameraPosition():
func cameraPosition() -> (SCNVector3, SCNVector3) {
guard let frame = sceneView.session.currentFrame else { return (SCNVector3(0, 0, -1), (SCNVector3(0, 0, 0))) }
let matrix = SCNMatrix4(frame.camera.transform)
let direction = SCNVector3(-matrix.m31, -matrix.m32, -matrix.m33)
let location = SCNVector3(matrix.m41, matrix.m42, matrix.m43)
return ((location + direction), direction)
}
// Helper function
func +(left: SCNVector3, right: SCNVector3) -> SCNVector3 {
return SCNVector3(left.x + right.x, left.y + right.y, left.z + right.z)
}
我创建Ball() 的实例并像这样抛出它们:
let (position, direction) = cameraPosition()
// throw ball
let ball = Ball()
ball.position = position //SCNVector3(0,0,0)
sceneView.scene.rootNode.addChildNode(ball)
let impulseModifier = Float(10)
ball.physicsBody!.applyForce(SCNVector3(direction.x*impulseModifier, direction.y*impulseModifier, direction.z*impulseModifier), asImpulse: true)
球类:
class Ball: SCNNode {
override init() {
super.init()
let sphere = SCNSphere(radius: 0.05)
sphere.firstMaterial?.diffuse.contents = UIColor.red
self.geometry = sphere
self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry: sphere, options: nil))
}
}
问题在于,很多时候,球并没有真正撞到墙上,而是穿过它,就好像物理体不能正常工作一样。我注意到有时当我改变相机和墙壁之间的距离和角度时效果会更好,但就我尝试过的结果而言,结果从来都不是一致的。
我还尝试将墙的位置更改为:SCNVector3(0, 0, -1) 将其定位在距离世界原点 1 米深的位置,结果稍微好一些,但仍然不一致。
问题可能出在哪里,为什么?
提前致谢!
【问题讨论】:
-
你解决了这个问题吗?
-
@mike_t 不幸的是:/
-
这个问题可能是由于 Scenekit 中缺乏连续的碰撞检测。
标签: ios scenekit augmented-reality arkit