Calculate Y-axis angle between World Origin and ARCamera

Viewed 248

First of all, I feel like this question is pretty simple and that I am just lacking some mathematical knowledge, it feels like a formula would be able to give me the answer, however I cannot find it anywhere.

I am trying to figure out a way to calculate the Y-axis angle between the ARCamera and the World Origin.

I thought at first that I could use a SCNNode and its eulerAngles property as followed:

let node = SCNNode()
node.transform = sceneView.pointOfView!.camera.worldTransform
print(node.eulerAngles.y) // Angle given is always between [-π/2, π/2]

It is explained in a bit more detail here: ScenKit eulerAngles strange values during rotation

I also know that the node has a rotation property that is a non-normalized quaternion and that a rotation on the Y-axis could also be done by rotating X-axis and Z-axis at the same time.

Is there is a way to get the angle from the world origin to the -z of the camera using the transform, rotation, or orientation properties?

It seems like what I am looking for would be a swift implementation of the Unity Vector3.SignedAngle function: https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html

    Vector3 targetDir = target.position - transform.position;
    Vector3 forward = transform.forward;
    float angle = Vector3.SignedAngle(targetDir, forward, Vector3.up);
    if (angle < -5.0F)
        print("turn left");
    else if (angle > 5.0F)
        print("turn right");
    else
        print("forward");

So if anybody has an idea on how to implement it, please let me know.

0 Answers
Related