unity – Unity3D – Dealing with Angles (-360,360)

I’m currently working with Oculus integration trying to export headset (and eventually controller) position and rotation data to a CSV for later use in Blender. My issue comes from the way Unity (or Blender) handles rotations. In Unity, 0 = 360, whereas in Blender 0 != 360.

My current solution is to grab the current rotation and subtract it from the rotation in the frame before. I then combine the difference with a running sum (Vector3 for x, y, and z sums). If any sum is greater than 360, I add a rotation to a separate Vector3 that tracks the number of complete rotations for each axis. I subtract rotations for sums less than -360. This way I can calculate the final rotation (x for example) using 360*numRots.x + xrot to get the overall rotation.

Unfortunately my code doesn’t properly store the running total correctly; it always matches the current rotation. For example, if I run the program, the current rotation might be (270, 23, -9) and the running total SHOULD be (0, 0, 0) (plus or minus some degrees) but the running total always matches the current rotation.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;

public class Mocap : MonoBehaviour
{

    public GameObject cam;
    static string path = "Assets/test.txt";
    StreamWriter writer;
    private Vector3 rotationSum; // running sums of x, y, z rotations
    private Vector3 numRots; // total number of rotations per axis
    private Vector3 prevRot; // previous rotation values

    void Start()
    {
        writer = new StreamWriter(path, append: false);
        rotationSum = new Vector3(0, 0, 0);
        numRots = new Vector3(0, 0, 0);
    }

    void Update()
    {
        // get camera pos and rot
        double xpos = cam.transform.position.x;
        double ypos = cam.transform.position.y;
        double zpos = cam.transform.position.z;
        float xrot = cam.transform.rotation.eulerAngles.x;
        float yrot = cam.transform.rotation.eulerAngles.y;
        float zrot = cam.transform.rotation.eulerAngles.z;
        Vector3 curr = new Vector3(xrot, yrot, zrot);

        Vector3 tempDiff = curr - prevRot;
        rotationSum += tempDiff;

        // check x
        if(rotationSum.x >= 360)
        {
            rotationSum.x -= 360;
            numRots.x += 1;
        } else if(rotationSum.x <= -360)
        {
            rotationSum.x += 360;
            numRots.x -= 1;
        }

        // check y
        if (rotationSum.y >= 360)
        {
            rotationSum.y -= 360;
            numRots.y += 1;
        }
        else if (rotationSum.y <= -360)
        {
            rotationSum.y += 360;
            numRots.y -= 1;
        }

        // check z
        if (rotationSum.z >= 360)
        {
            rotationSum.z -= 360;
            numRots.z += 1;
        }
        else if (rotationSum.z <= -360)
        {
            rotationSum.z += 360;
            numRots.z -= 1;
        }

        // final rotation
        xrot = 360 * numRots.x + xrot;
        yrot = 360 * numRots.y + yrot;
        zrot = 360 * numRots.z + zrot;

        // update previous values
        prevRot = curr;

        string filetext = xpos + ", " + ypos + ", " + zpos + ", " + xrot + ", " + yrot + ", " + zrot;

        this.writer.WriteLine(filetext);

    }
}