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.
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))
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))
}
}
}
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.
[EDIT]: 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...