gpt4 book ai didi

rotation - 应用旋转后如何将 SCNNode 沿其指向的方向移动

转载 作者:行者123 更新时间:2023-12-02 22:09:00 25 4
gpt4 key购买 nike

我的困境是这样的:我有一艘宇宙飞船位于恒星和行星之间的太空中的某个地方。相机被添加为 spaceshipNode 的子节点,并且您始终看飞船的背面(上面提高了几个单位)。我使用 CoreMotion 来旋转宇宙飞船,如下所示:

    func startMonitoringMotion() {
self.motionManager?.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: { (data, error) in
guard let data = data else { return }

let attitude: CMAttitude = data.attitude
self.ship.eulerAngles = SCNVector3Make(Float(attitude.roll - M_PI_2), Float(attitude.yaw), Float(attitude.pitch))
})
}

并且轮换按预期进行。

现在我想将飞船朝它所面向的方向移动,但我不知道该怎么做。我尝试过不同的方法,但都失败了。

几天来我无数次搜索这个论坛,但没有成功。我希望有人能为我(和我的宇宙飞船)指明正确的方向。

提前谢谢您。

最佳答案

我发现最简单的方法是获取节点的 worldTransform 属性的第三行,该属性对应于其 z 向前轴。

func getZForward(node: SCNNode) -> SCNVector3 {
return SCNVector3(node.worldTransform.m31, node.worldTransform.m32, node.worldTransform.m33)
}

ship.position += getZForward(ship) * speed // nb scalar multiply requires overload of * func
// if node has a physics body, you might need to use the presentationNode, eg:
// getZForward(ship.presentationNode)
// though you probably don't want to be directly modifying the position of a node with a physics body

请参阅此处的讨论 Getting direction that SCNNode is facing

iOS 11 更新

iOS 11 添加了方便的功能来获取节点的方向。在本例中,worldForward 属性就是您想要的属性。此外,SCNNode 上返回 SCNVector 和矩阵类型的所有属性现在都有返回 simd 类型的版本。由于 simd 已经具有算术运算符的重载,因此您不再需要为 SCNVectorSCNMatrix 类型添加算术覆盖集。

所以我们可以去掉上面的 getZForward 方法,只需要一行:

ship.simdPosition += ship.simdWorldFront * speed

关于rotation - 应用旋转后如何将 SCNNode 沿其指向的方向移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com