下图显示了一个旋转框,应在 X 轴和 Z 轴上水平移动。 Y 应该不受影响,以简化场景。这个盒子也可能是相机的 SCNNode,所以我想此时投影没有意义。
假设我们想要沿着红色箭头的方向移动盒子。如何使用 SceneKit 来实现这一点?
红色箭头表示盒子的-Z 方向。它还向我们表明它与相机的投影或显示为网格的深灰色线的全局轴不平行。
我的最后一种方法是平移矩阵和旋转矩阵的乘积,从而产生新的变换矩阵。那么我是否必须将当前的变换添加到新的变换中?
If yes,其中是用于添加矩阵的 SceneKit 函数,例如SCNMatrix4Mult
用于乘法还是我必须自己使用 Metal 编写它?
If no,我在矩阵计算中遗漏了什么?
我不想利用GLKit
.
所以我的理解是你想沿着它的方向移动盒子节点自己的X轴(不是它的父母 X 轴)。并且由于 Box Node 是旋转的,因此它的 X 轴与其父级的 X 轴不对齐,因此在两个坐标系之间转换平移时会遇到问题。
节点层次结构是
parentNode
|
|----boxNode // rotated around Y (vertical) axis
使用变换矩阵
移动 boxNodeits own X axis
// First let's get the current boxNode transformation matrix
SCNMatrix4 boxTransform = boxNode.transform;
// Let's make a new matrix for translation +2 along X axis
SCNMatrix4 xTranslation = SCNMatrix4MakeTranslation(2, 0, 0);
// Combine the two matrices, THE ORDER MATTERS !
// if you swap the parameters you will move it in parent's coord system
SCNMatrix4 newTransform = SCNMatrix4Mult(xTranslation, boxTransform);
// Allply the newly generated transform
boxNode.transform = newTransform;
请注意:矩阵相乘时顺序很重要
另外一个选择:
使用SCNNode坐标转换函数,对我来说看起来更直接
// Get the boxNode current position in parent's coord system
SCNVector3 positionInParent = boxNode.position;
// Convert that coordinate to boxNode's own coord system
SCNVector3 positionInSelf = [boxNode convertPosition:positionInParent fromNode:parentNode];
// Translate along own X axis by +2 points
positionInSelf = SCNVector3Make(positionInSelf.x + 2,
positionInSelf.y,
positionInSelf.z);
// Convert that back to parent's coord system
positionInParent = [parentNode convertPosition: positionInSelf fromNode:boxNode];
// Apply the new position
boxNode.position = positionInParent;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)