Swift Scenekit - Multiple Twists
I have a problem with repeatedly rotating a node. I am working on a rolling ball game, and although I can rotate the ball along one axis or two axes by the same amount, I cannot rotate at partial angles.
Example:
// Roll right 90 -
SCNNode.pivot = SCNMatrix4MakeRotation(Float(M_PI_2), 0, 1, 0)
// Roll right 180 -
SCNNode.pivot = SCNMatrix4MakeRotation(Float(M_PI_2) * 2, 0, 1, 0)
// Roll up 90 -
SCNNode.pivot = SCNMatrix4MakeRotation(Float(M_PI_2), 1, 0, 0)
// Roll up & right 90 -
SCNNode.pivot = SCNMatrix4MakeRotation(Float(M_PI_2), 1, 1, 0)
All that will work, however, if I need to roll the ball to the right 180 and 90, I'm stuck.
Even if there was a way to add vectors together that would make me.
Any help is greatly appreciated.
+3
source to share
1 answer
To combine the effects of rotation matrices , use matrix multiplication.
To do this in SceneKit, you can:
- Create separate rotation matrices and multiply them together using
SCNMatrix4Mult
. - Apply rotation directly to the existing matrix using
SCNMatrix4Rotate
. (This is equivalent to theSCNMatrix4MakeRotation
+ optionSCNMatrix4Mult
, it just combines these steps into one function call.)
If the order of transformations is important to your application, remember that the order of matrix multiplication is the reverse of the order of the transform .
+5
source to share