【问题标题】:How to apply SCNVector3 force/impulse from an orientation in scenekit?如何从场景包中的方向应用 SCNVector3 力/脉冲?
【发布时间】:2018-02-01 16:00:10
【问题描述】:

在 ARKit/SceneKit 中,当用户点击按钮时,我想对我的节点施加一个脉冲。我希望冲动来自当前用户的角度。这意味着节点将远离用户的视角。由于这段代码,我能够获得当前的方向/方向:

func getUserVector() -> (SCNVector3, SCNVector3) { // (direction, position)
    if let frame = self.sceneView.session.currentFrame {
        let mat = SCNMatrix4(frame.camera.transform) // 4x4 transform matrix describing camera in world space
        let dir = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33) // orientation of camera in world space
        let pos = SCNVector3(mat.m41, mat.m42, mat.m43) // location of camera in world space

        return (dir, pos)
    }
    return (SCNVector3(0, 0, -1), SCNVector3(0, 0, -0.2))
}

通过https://github.com/farice/ARShooter/blob/master/ARViewer/ViewController.swift#L191

我创建了一个任意的 SCNVector。它包含关于多高(Y 轴)、向左或向右多少以及应用于节点的向前多少的信息。

我想将我的 SCNVector3 转换/翻译为来自相机的方向/方向。

意思是,我有

let (direction, position) = self.getUserVector()
let force = SCNVector3(x: 1.67, y: 13.83, z: -18.3)

如何从direction 的位置/来源应用force

【问题讨论】:

    标签: ios swift 3d scenekit arkit


    【解决方案1】:

    经过大量谷歌搜索后想通了。为了将脉冲向量 3 转换为我需要的方向,我使用了这样的东西:

    let original = SCNVector3(x: 1.67, y: 13.83, z: -18.3)
    let force = simd_make_float4(original.x, original.y, original.z, 0)
    let rotatedForce = simd_mul(currentFrame.camera.transform, force)
    
    let vectorForce = SCNVector3(x:rotatedForce.x, y:rotatedForce.y, z:rotatedForce.z)
    node.physicsBody?.applyForce(vectorForce, asImpulse: true)
    

    【讨论】:

    • 我正在尝试相同的方法,但是如果我使用原始向量中的任何值,如果我只通过 z 位置,那么球节点不会从屏幕中心抛出,那么它会从中心抛出你知道相关的吗?
    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2015-04-28
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多