问题
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...
回答1:
Many thanks for the answer, helped me a lot in understanding the different ways of rotation.
However, it turns out there was a constraint on the node like this:
let billboardConstraint = SCNBillboardConstraint()
billboardConstraint.freeAxes = SCNBillboardAxis.Y
constraints = [billboardConstraint]
Commenting this code made the rotation work properly. Pretty dumb...
According to the documentation a SCNBillboardConstraint makes the object's z-axis always point towards the pointOfView
node. But I do not quite understand the freeAxes property here. The default type is all
, so it never rotates with respect to the point of view. So if I only set the Y constraint type, I can only apply rotation on the X and Z axes? Or am I wrong here?
回答2:
Update: I reckon the problem is in
singleSided
shader you're using. You see the invisible side of your model. Just enabledoubleSided
material for your geometry:
directionNode.geometry?.firstMaterial?.isDoubleSided = true
You need to use values expressed in Radians (effective range is -6.28
to +6.28
).
It's important in what direction your object is rotated >>
CW
orCCW
. You can't see thesingle-sided
geometry (at a certain angle) at a positive angle of rotation+.pi
.
It works this way:
myNode.rotation = SCNVector4(x: 0, y: -1, z: 0, w: (.pi * 3) / 2)
...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)
...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()
来源:https://stackoverflow.com/questions/55972326/unable-to-rotate-an-scnnode-in-arkit