Reputation: 21
My SCNNode
is a compass that I want to rotate towards north.
compassNode = scene?.rootNode.childNodes[0]
I am successfully getting heading data and converting it to radians and storing it in a variable called angle.
angle = newHeading.trueHeading.toRadians
I have tried three methods now:
rotate = SCNAction.rotate(by: CGFloat(angle), around: SCNVector3(0, 1, 0), duration: 0.5)
compassNode?.runAction(rotate)
and
compassNode?.rotation = SCNVector4Make(0, 1, 0, Float(angle))
and
let oldTransform = compassNode?.transform
let newTransform = SCNMatrix4MakeRotation(Float(angle), 0, 1, 0)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
compassNode?.transform = SCNMatrix4Mult(newTransform, oldTransform!)
SCNTransaction.commit()
None of them makes any noticeable transformation to the compass. It appears to be still.
Upvotes: 0
Views: 282
Reputation: 1213
Running out of comment characters... Sry - have to put it in answer section.
Convert 90 degrees to radians, rotateTo that value. Rotating models can be confusing in 3D (for me anyway), sometimes it's easier to just use an SCNBox at first - it will have the correct rotation by default provided you're looking down -Z if you didn't change the camera view. You can turn on the the 3D outlines and sometimes that helps provide a better visual OR just texture map the box with different colors so that Red is the top, Green is front or whatever.
Then you can reshape the box, rotated it with different angles until you get the hang of rotateTo, rotateBy, rotateAround. I'm not a math guru, so unfortunately trial and error for me sometimes, but once I understand what's happening, then I can put the model in and know that my math works - then if I have to do some work on the model, such as pivot point or need to change the model - set Z-UP or whatever (this post: 59766878), then I'm focused on one thing at a time and not having a combination of problems to figure out.
I've also used lookAt and placed various (hidden) nodes at particular positions, so that I can see what X,Y,Z rotations look like at various points in a game and from different view points.
Upvotes: 0