I have a small SceneKit app which recognizes an image and then places colored spheres on it, one in the center of the image and one on each of the four corners.
If I add the sphere nodes to the root node, the code that positions the balls in the right quadrants looks like this:
let lowerLeft = SCNVector3(x - width/2, y - height/2, z)
let lowerRight = SCNVector3(x + width/2, y - height/2, z)
let upperRight = SCNVector3(x + width/2, y + height/2, z)
let upperLeft = SCNVector3(x - width/2, y + height/2, z)
let center = SCNVector3(x, y, z)
Where x, y and z are set from the image anchor’s transform matrix.
The image is vertical, so this means that the z axis is pointing towards the camera, +y is pointing up, and +x is to the right. This is all in world coordinates, since the spheres are “siblings” of the image.
This situation is less than ideal because when you move the camera, the spheres move with it instead of staying with the image.
If I add the sphere nodes as children of the image node, the code to place the spheres looks like this:
let lowerLeft = SCNVector3(x - width/2, y, z + height/2)
let lowerRight = SCNVector3(x + width/2, y, z + height/2)
let upperRight = SCNVector3(x - width/2, y, z - height/2)
let upperLeft = SCNVector3(x + width/2, y, z - height/2)
let center = SCNVector3(x, y, z)
Now x, y and z are all set to zero, because our origin is now the origin of the anchor's node, which is located at the center of the image.
This keeps the spheres with the image when the camera moves. But the orientation of the axes (is that the plural of axis???) has changed. Now the y axis is pointing towards the camera, -z is pointing up, and -x is to the right. So the coordinate system has been rotated around x (to bring z to vertical) and then around z (to reverse the +/- of x).
What this means is that in order to do the calculations properly in a generic case, I’d need to figure out which way the axes are pointing and account for that. This seems wrong, like there should be some way of doing this where SceneKit has already taken that into account. But I have done a lot of Googling and have not been able to find one. I do see a lot of examples that say things like “to position a node 1 meter above another node, set its position to [0, 1, 0]”. But that still requires knowing that the +y axis is “up”, even though no-one mentions it when they say this.
This app also exhibits the same problem I wrote about in https://developer.apple.com/forums/thread/724778, which was a RealityKit app, but that is a separate issue.