Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating a node in SceneKit

I'm struggling to understand multiple rotations of a node.

Firstly, I created and positioned a plane:

SCNPlane *plane = [SCNPlane planeWithWidth:10 height:10];
SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
planeNode.rotation = SCNVector4Make(1, 0, 0, (M_PI/2 * 3));
[scene.rootNode addChildNode:planeNode];

Then I positioned and set the direction of a spotlight node on this plane:

SCNLight *light = [[SCNLight alloc] init];
light.type = SCNLightTypeSpot;
light.spotInnerAngle = 70;
light.spotOuterAngle = 100;
light.castsShadow = YES;
lightNode = [SCNNode node];
lightNode.light = light;
lightNode.position = SCNVector3Make(4, 0, 0.5);
lightNode.rotation = SCNVector4Make(0, 1, 0, M_PI/2);
[planeNode addChildNode:lightNode];

Then I animate the rotation of the light node 90 degrees clockwise around the x-axis:

[SCNTransaction begin];
[SCNTransaction setAnimationDuration:2.0];

lightNode.rotation = SCNVector4Make(1, 0, 0, M_PI/2);

[SCNTransaction commit];

But I'm confused as to why the following rotates the light node back to the original position about the same axis:

[SCNTransaction begin];
[SCNTransaction setAnimationDuration:2.0];

lightNode.rotation = SCNVector4Make(0, 1, 0, M_PI/2);

[SCNTransaction commit];

To me this reads as we're rotating the node 90 degrees clockwise about the y-axis.

Can anyone explain why this works? Or, better yet, suggest a clearer method for rotating a node then returning it to its original position?

like image 754
bcl Avatar asked Dec 02 '22 14:12

bcl


1 Answers

I think I've resolved this by using eulerAngles which seems to work in a way to what I understand.

So I replaced:

lightNode.rotation = SCNVector4Make(0, 1, 0, M_PI/2);

With:

lightNode.eulerAngles = SCNVector3Make(0, M_PI/2, 0);

And likewise for the other rotations.

I have to admit I'm still very confused as to what the rotation method does but happy I have something I can work with now.

like image 145
bcl Avatar answered Dec 07 '22 01:12

bcl