swift:将 y 轴指向 3-d 中的另一个点 space

swift: orient y-axis toward another point in 3-d space

假设您在 3-D 中有两个点 space。调用第一个 o 作为起点,另一个 t 作为目标。每个旋转轴都与 world/parent 坐标系(以及彼此)对齐。放置第三个点 r 与原点重合,相同的位置和旋转。

如何在 Swift 中旋转 r 使其 y 轴指向 t?如果指向 z 轴更容易,我会改用它。其他两个轴的最终方向对我的需要无关紧要。

我经历过很多与此相关的讨论,但 none 满意。我从阅读和经验中了解到,欧拉角可能不是可行的方法。我们没有在微积分中涵盖这一点,那是 50 年前的事了。

知道了!添加容器节点时非常简单。以下内容似乎适用于任何象限中的任何职位。

// pointAt_c is a container node located at, and child of, the originNode
// pointAtNode is its child, position coincident with pointAt_c (and originNode)

// get deltas (positions of target relative to origin)
let dx = targetNode.position.x - originNode.position.x
let dy = targetNode.position.y - originNode.position.y
let dz = targetNode.position.z - originNode.position.z

// rotate container node about y-axis (pointAtNode rotated with it)
let y_angle = atan2(dx, dz)
pointAt_c.rotation = SCNVector4(0.0, 1.0, 0.0, y_angle)

// now rotate the pointAtNode about its z-axis
let dz_dx = sqrt((dz * dz) + (dx * dx))

// (due to rotation the adjacent side of this angle is now a hypotenuse)
let x_angle = atan2(dz_dx, dy)
pointAtNode.rotation =  SCNVector4(1.0, 0.0, 0.0, x_angle)

我需要这个来替换 lookAt 约束,无论如何,它不能轻易地用节点树存档。我指向 y 轴,因为那是 SCN 圆柱体和胶囊体的方向。

如果有人知道如何避免容器节点,请告知。每次我尝试将顺序旋转应用于单个节点时,最后一个会覆盖前一个。我没有知识来制定一个旋转表达式来一次性完成。