Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera Rotation in SceneKit

I have posted a similar question to this here , but this is different in that it deals with eular angles.

Given the setup that I had in the other post. A simple board that is on the screen, and a camera that is looking at it, I want to rotate the camera. For simplicity I am doing the entire camera in code. For context, per the other question and answer I have established that the board runs long on the Z axis, Shorter on the X axis, and the height is the Y axis.

Adding this code to the scene we can see my board running on the Z axis. I have raised the camera up on the Y axis a little to get a better view. My end goal is to get the board running longways accross the camera.

SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 5, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, 0);

Gives Imgur

Great start. Then I try to rotate the camera so it is looking from the top, down on the board. My understanding it that I would need to rotate around the X-axis to accomplish this. I did this by trying the following.

SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, -M_PI/2);

The iOS documentation states that the rotation in eulars is set with (z, y, x). This did not work, however, and only gave a blank screen. I then started experimenting and found that rotation around the Z axis would get me in the right direction.

SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, 0, 0);

This rendered this screen. Imgur

This didn't make sense but I went ahead with it and eventually found that by also Rotating around the Y axis I could get my desired screen.

SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, -M_PI/2, 0);

The y rotation was also a little baffling because I would have expected to have to rotate around the Z axis. Imgur

While this code works I have a similar question to my other post. Why do I have to rotate around the Z axis instead of the X axis for my first rotation. I think I might just not understand eular angles that well.

Thanks in advance

Edit:

as @mnuages pointed out, the documentation online states that this vector is in (x,y,z). This is contrary to the header documentation which I would normally use as can be seen here Imgur

It would appear that this is just a bug/typo in Apple's code. Thanks for clearing it up.

like image 551
Kizik Studios Avatar asked Oct 19 '22 05:10

Kizik Studios


1 Answers

[Comment does not fit in a comment]

Please note that the documentation states that

The order of components in this vector matches the axes of rotation:

  • Pitch (the x component) is the rotation about the node’s x-axis.
  • Yaw (the y component) is the rotation about the node’s y-axis.
  • Roll (the z component) is the rotation about the node’s z-axis.
like image 178
mnuages Avatar answered Oct 30 '22 01:10

mnuages