swift: orient y-axis toward another point in 3-d space(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 空间中的另一个点
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01