如何在场景套件中从方向应用SCNVector3力/脉冲?



在 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

经过大量谷歌搜索后弄清楚了。为了将脉冲矢量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)

最新更新