lundi 22 février 2021

Quaternion: Generic Sensor API for the web VS Unity

I try to make an app that use the generic sensor api for control some games using a device in Unity Engine through internet. I managed to recover the quaternion that the web page returns to me with Unity... but i can't understand how it works! Using the sensor emulator inside Chrome seems that α, β and γ (aka Y, X and Z in that order) work fine individually but when you put a value of (for example) 90, 90 and 90 in all three axis the result is strange and does not coincide with what I would expect Ok with 0,0,0 Ok with 90, 0, 0 Ok with 90, 90, 0 Strange with 90, 90, 90 Strange with 0, 90, 90 Ok with 0, 0, 90

I am really confused by this behavior and i really appreciate if someone could help me solve this problem!

This is the code i use in the webpage to get the sensor data and create a TXT file with the Quaternion axis:

          function initSensor() {
          const options = { frequency: 60 , referenceFrame: 'screen'};
          sensor = new RelativeOrientationSensor(options);

          //Qui andiamo a usare i sensori e ne estraiamo i dati che ci servono
          sensor.onreading = () => {x = sensor.quaternion[0];
          y = sensor.quaternion[1];
          z = sensor.quaternion[2];
          w = sensor.quaternion[3];
          document.getElementById("consolex").innerHTML = x;
          document.getElementById("consoley").innerHTML = y;
          document.getElementById("consolez").innerHTML = z;
          document.getElementById("consolew").innerHTML = w;};
          sensor.onerror = (event) => {
            if (event.error.name == 'NotReadableError') {
              console.log("Sensor is not available.");
            }
          }
          //Facciamo partire i sensori
          sensor.start();
      }

      function saveFile()
       {
         var data = {};
          data = {'data': $("#consolex").text() +" "
          + $("#consoley").text()+" "
          + $("#consolez").text()+" "
          + $("#consolew").text()};
          $.ajax({
            type: "POST",
            url:"savefile.php",
            data:data,
            success:function(resp){
              console.log(resp);
            }
          });
       }

This is the code i use in unity to convert the text into a quaternion:

public Quaternion ConvertTextToQuaternion(string testo)
{
    Quaternion quat = Quaternion.identity;
    float x=0.0f, y=0.0f, z = 0.0f, w = 1.0f;
    string[] valori = testo.Split(' ');
    //Questa if cerca di convertire tutte le stringe ottenute in float...se c'è qualche problema in uno solo dei valori il quaternione identity verrà restituito
    if(float.TryParse(valori[0], out x) && float.TryParse(valori[1], out y) && float.TryParse(valori[2], out z) && float.TryParse(valori[3], out w))
    {
        quat = new Quaternion(
        x, //X
        z, //Y
        y, //Z
        w*-1); //W
    }
    print(quat+ " Euler:"+ quat.eulerAngles);
    return quat;
}



Aucun commentaire:

Enregistrer un commentaire