问题描述
我正在尝试在y轴上旋转SCNNode,以便我的节点(箭头)指向正确的方向.我想制作一个使用ARKit的导航应用程序;每个箭头都需要指向我收到的路线的下一个位置.路线的检索工作正常.
I'm trying to rotate an SCNNode on the y-axis so my node (an arrow) points in the right direction. I want to make a navigation app that uses ARKit; each arrow needs to point to the next location of the route I have received. The retrieval of the route works properly.
将节点添加到场景的rootNode.在某个点上,我遍历了一系列节点以将它们放置在场景中,并将缩放应用于节点(基于距离),所有步骤均正确完成.但是,当我应用旋转时,这没有效果.旋转是通过修改节点的旋转属性来完成的:
The nodes are added to the rootNode of the scene. At some point I traverse an array of nodes to position them in the scene and apply scaling to the node (based on the distance), which is all done correctly. However, when I apply rotation, this has no effect.The rotation is done by modifying the rotation property of the node:
directionNode.rotation = SCNVector4(x:0, y:1:, z:0, w:Float(bearing))
这没有理想的效果.我也尝试使用runAction方法,也没有效果:
This does not have the desired effect.I also tried to use the runAction method, also no effect:
directionNode.runAction(SCNAction.rotateBy(x: 0, y: CGFloat(bearing), z: 0, duration: 0))
这是定位和缩放节点的代码.
Here's the code that positions and scales the nodes.
for i in 0...(directionNodes.count - 1) {
let directionNode = directionNodes[i]
let translation = MatrixHelper.transformMatrix(for: matrix_identity_float4x4, originLocation: startingLocation, location: directionNode.location)
let position = SCNVector3.positionFromTransform(translation)
let distance = directionNode.location.distance(from: startingLocation)
DispatchQueue.main.async {
let scale = 100 / Float(distance)
directionNode.scale = SCNVector3(x: scale, y: scale, z: scale)
directionNode.anchor = ARAnchor(transform: translation)
directionNode.position = position
if (i < (self.directionNodes.count - 1)) {
// Apply rotation to the arrow
let successiveStepLocation = self.directionNodes[i + 1].location!
let bearing = directionNode.location.bearingToLocationRadian(successiveStepLocation)
// rotate
directionNode.rotation = SCNVector4(x:0, y:1:, z:0, w:Float(bearing))
}
}
}
这些都包装在SCNTransaction中.
This is all wrapped in a SCNTransaction.
谁能告诉我为什么节点旋转不起作用?任何帮助将不胜感激.
Can anyone tell me why the rotation of the node is not working? Any help is greatly appreciated.
:我忘了提到我尝试旋转的3D箭头对象始终指向同一方向.即使我四处走动",箭头始终(在我的情况下)始终指向左侧.也许这有助于解决我的问题...
: I forgot to mention that the 3D arrow object that I'm trying to rotate always points in the same direction. Even when I 'walk around' it the arrow always points (in my case) to the left.Maybe this helps solving my issue...
推荐答案
directionNode.geometry?.firstMaterial?.isDoubleSided = true
您需要使用以弧度表示的值(有效范围为-6.28
至+6.28
).
You need to use values expressed in Radians (effective range is -6.28
to +6.28
).
它是这样工作的:
myNode.rotation = SCNVector4(x: 0, y: -1, z: 0, w: (.pi * 3) / 2)
...以及当您使用SCNAction时(也不要忘记分配持续时间):
...and when you're using SCNAction, too (and don't forget to assign a duration):
let action = SCNAction.repeatForever(SCNAction.rotate(by: -.pi,
around: SCNVector3(0,1,0),
duration: 1.75))
myNode.runAction(action)
...以及当您使用SCNTransaction时:
...and when you're using SCNTransaction, too:
let previousTransform = myNode.transform
let rotate = SCNMatrix4MakeRotation(0, 1, 0, (-Float.pi/2))
SCNTransaction.begin()
SCNTransaction.animationDuration = 3.75
myNode.transform = SCNMatrix4Mult(rotate, previousTransform)
SCNTransaction.commit()
这篇关于无法在ARKit中旋转SCNNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!