swift:将 y 轴朝向 3-d 空间中的另一个点

swift: orient y-axis toward another point in 3-d space(swift:将 y 轴朝向 3-d 空间中的另一个点)

本文介绍了swift:将 y 轴朝向 3-d 空间中的另一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose you have two points in 3-D space. Call the first o for origin and the other t for target. The rotation axes of each are alligned with the world/parent coordinate system (and each other). Place a third point r coincident with the origin, same position and rotation.

如何在 Swift 中旋转 r 使其 y 轴指向 t?如果指向 z 轴更容易,我会改用它.其他两个轴的最终方向对我的需求来说并不重要.

How, in Swift, can you rotate r such that its y-axis points at t? If pointing the z-axis is easier, I'll take that instead. The resulting orientation of the other two axes is immaterial for my needs.

我已经进行了许多与此相关的讨论,但没有一个令人满意.我从阅读和经验中了解到,欧拉角可能不是要走的路.我们没有在微积分中讨论这个问题,反正那是 50 年前的事了.

I've been through many discussions related to this but none satisfy. I have learned, from reading and experience, that Euler angles is probably not the way to go. We didn't cover this in calculus and that was 50 years ago anyway.

推荐答案

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

Got it! Incredibly simple when you add a container node. The following seems to work for any positions in any quadrants.

// 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 圆柱体和胶囊的方向.

I needed this to replace lookAt constraints which cannot, easily anyway, be archived with a node tree. I'm pointing the y-axis because that's how SCN cylinders and capsules are directed.

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

If anyone knows how to obviate the container node please do tell. Everytime I try to apply sequential rotations to a single node, the last overwrites the previous one. I haven't the knowledge to formulate a rotation expression to do it in one shot.

这篇关于swift:将 y 轴朝向 3-d 空间中的另一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:swift:将 y 轴朝向 3-d 空间中的另一个点

基础教程推荐