Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 如何在SceneKit中移动旋转的SCNNode?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
下图显示一个旋转的框,应该在X和Z轴上水平移动. Y应该不受影响以简化方案.盒子也可以是相机的SCNNode,所以我猜这个投影在这一点上没有意义.

所以我们要说我们想要沿着红色箭头的方向移动盒子.如何使用SceneKit实现这一目标?

红色箭头表示方框的-Z方向.它还向我们展示了它与摄像机的投影或与网格显示为深灰色线条的全局轴不平行.

我的最后一种方法是平移矩阵和旋转矩阵的乘积,它产生一个新的变换矩阵.我是否必须将当前变换添加到新变换中?

如果是,那么SceneKit函数用于添加像SCNMatrix4Mult这样的矩阵用于乘法,或者我必须自己使用Metal编写它?

如果不是,我错过了矩阵计算?

我不想使用GLKit.

swift – 如何在SceneKit中移动旋转的SCNNode?

解决方法@H_673_22@
所以我的理解是你想要沿着它自己的X轴移动Box节点(而不是它的父X轴).并且因为Box节点被旋转,其X轴未与其父节点对齐,因此您在转换两个坐标系之间的平移时遇到问题.

节点层次结构是

parentNode
    |
    |----BoxNode // rotated around Y (vertical) axis

使用转换矩阵

沿自己的X轴移动BoxNode

// 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);

// 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;

大佬总结

以上是大佬教程为你收集整理的swift – 如何在SceneKit中移动旋转的SCNNode?全部内容,希望文章能够帮你解决swift – 如何在SceneKit中移动旋转的SCNNode?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。