gpt4 book ai didi

ios - 如何将相机指向 iOS 11 以下的 SCNVector3 点

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:13 27 4
gpt4 key购买 nike

我昨天才开始学习如何使用 SceneKit,所以我可能会弄错或不正确。我试图让我的 cameraNode 查看场景中的 SCNVector3 点。

我正在努力让低于 iOS 11.0 的人可以使用我的应用程序。但是,look(at:) 函数仅适用于 iOS 11.0+

这是我初始化相机的函数:

func initCamera() {
cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(5, 12, 10)
if #available(iOS 11.0, *) {
cameraNode.look(at: SCNVector3(0, 5, 0)) // Calculate the look angle
} else {
// How can I calculate the orientation? <-----------
}
print(cameraNode.rotation) // Prints: SCNVector4(x: -0.7600127, y: 0.62465125, z: 0.17941462, w: 0.7226559)
gameScene.rootNode.addChildNode(cameraNode)
}

SCNVector4(x: -0.7600127, y: 0.62465125, z: 0.17941462, w: 0.7226559) 的方向是 x: -43.5, y: 35.8, z: 10.3 ,我不明白w。 (此外,为什么不是 z = 0?我认为 z 是滚动...?)

以下是我为重现我认为的 Y 角 应该做的工作:

enter image description here

所以我计算出它是 63.4 度,但返回的旋转显示它应该是 35.8 度。是我的计算有问题,是我没有完全理解 SCNVector4,还是有其他方法可以做到这一点?

我看了Explaining in Detail the ScnVector4 method SCNVector4 是什么,但我仍然不太明白 w 是什么。它说 w 是“旋转角度”,我认为 X、Y 和 Z 的用途。


如有任何问题,欢迎提问!

最佳答案

虽然 @rickster 已经给出了节点属性的解释,但我已经想出了一种使用数学(三角函数)旋转节点以查看点的方法。

这是我的代码:

// Extension for Float
extension Float {
/// Convert degrees to radians
func asRadians() -> Float {
return self * Float.pi / 180
}
}

还有:

// Extension for SCNNode
extension SCNNode {
/// Look at a SCNVector3 point
func lookAt(_ point: SCNVector3) {
// Find change in positions
let changeX = self.position.x - point.x // Change in X position
let changeY = self.position.y - point.y // Change in Y position
let changeZ = self.position.z - point.z // Change in Z position

// Calculate the X and Y angles
let angleX = atan2(changeZ, changeY) * (changeZ > 0 ? -1 : 1)
let angleY = atan2(changeZ, changeX)

// Calculate the X and Y rotations
let xRot = Float(-90).asRadians() - angleX // X rotation
let yRot = Float(90).asRadians() - angleY // Y rotation
self.eulerAngles = SCNVector3(CGFloat(xRot), CGFloat(yRot), 0) // Rotate
}
}

然后您使用以下方法调用该函数:

cameraNode.lookAt(SCNVector3(0, 5, 0))

希望这对 future 的人们有所帮助!

关于ios - 如何将相机指向 iOS 11 以下的 SCNVector3 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52652654/

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