SceneKit物理在ARKit中无法正常工作
问题描述:
我是0123,的新手,我试图制作一个相对简单的应用程序,在其中创建“墙”并且用户“向其抛掷”球,并且它会反弹回来。SceneKit物理在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还不幸:/ – Eilon