【问题标题】:SceneKit physics don't work properly in ARKitSceneKit 物理在 ARKit 中无法正常工作
【发布时间】: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


【解决方案1】:

将您的墙物理设置为运动学,以便球可以与之反应。

  • 静态对象不会与任何东西发生反应。

  • 运动对象与其他对象发生反应,但其他对象不影响它们。

  • 当然,动态意味着它可以双向工作。

【讨论】:

    【解决方案2】:

    这很可能是由于球太小,碰撞检测无法正常工作。尝试将 continuousCollisionDetectionThreshold 设置为球半径的一半。在这种情况下:

    ball.physicsBody?.continuousCollisionDetectionThreshold = 0.025
    

    【讨论】:

    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 2021-07-22
    • 2015-04-19
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    相关资源
    最近更新 更多