About Y rotation correction in FocusSquare.updateTransform of ARKitSampe

I thought simply setting self.rotation = SCNVector4(0, 1, 0, camera.eulerAngles.y) is enough, but not really good enough in reality. I don't know why it is not.


Nor do I undertand why the sample code does a good job. Especially why yaw is calculated using atan2f(camera.transform.columns.0.x, camera.transform.columns.1.x)?


Can someone explain the underlying math to me? Thanks.


// Correct y rotation of camera square
if let camera = camera {
    let tilt = abs(camera.eulerAngles.x)
    let threshold1: Float = .pi / 2 * 0.65
    let threshold2: Float = .pi / 2 * 0.75
    let yaw = atan2f(camera.transform.columns.0.x, camera.transform.columns.1.x)
    var angle: Float = 0
   
    switch tilt {
    case 0..<threshold1:
        angle = camera.eulerAngles.y
       
    case threshold1..<threshold2:
        let relativeInRange = abs((tilt - threshold1) / (threshold2 - threshold1))
        let normalizedY = normalize(camera.eulerAngles.y, forMinimalRotationTo: yaw)
        angle = normalizedY * (1 - relativeInRange) + yaw * relativeInRange
       
    default:
        angle = yaw
    }
   
    self.rotation = SCNVector4(0, 1, 0, angle)
}

Simply camera can face to anywhere but not to focusSquare when we want focusSquare always faces to camera

I still don't get it. Can you elaborate?

Your look direction / camera direction is actually the beam go in / out the middle point of your screen (everything shown in the middle point also lays right in the your look / camera beam). Your FocusSquare may be not shown in the middle point but somewhere around. It means your camera is not faced directly to that square (but to something next to it). You cannot copy camera eulerAngles.y for that square.


There is an exception, when that square is in the middle or so close to the middle point (tilt in 0...threshold1), you may consider they are faced to each other and now you may copy that angle (angle = camera.eulerAngles.y). Otherwise you should calculate the right angle to turn the square to camera.

Thanks! I think I'm getting close.

So how does yaw in the code fix this issue? Why its value is calculated this way: atan2f(camera.transform.columns.0.x, camera.transform.columns.1.x)? (I believe this question is very closedly related: https://forums.developer.apple.com/thread/86482).

About Y rotation correction in FocusSquare.updateTransform of ARKitSampe
 
 
Q