Frage

I'm interested in AR applications of mobile devices and naturally I would like to make better use of the compass.

The only issue I've been having to work against isn't how twitchy the compass is. (Angular Smoothing seems to solve this issue just fine) My main issue is that when the device is held Vertical the compass values start freaking out. Causing an on screen compass to flip about all over the place. I don't have a lot of experience with mobile application development so I'm not sure what would be causing this issue, if its a Unity issue or if its just a limitation of the digital compass. I know other apps do seem to be able to use the compass fine in any orientation, but this is all stupidly new to me.

I've definitely tried moving the phone in a figure of 8. The device I have to play around with is a Nexus 4.

using UnityEngine;
using System.Collections;

public class Compass : MonoBehaviour {

// Use this for initialization
void Start () {
    Input.location.Start ();
    Input.compass.enabled = true;
}

// Update is called once per frame
void Update ()
{
    var heading = Input.compass.trueHeading;
    transform.eulerAngles = new Vector3 (0, 0, heading);
}
}
War es hilfreich?

Lösung

Preamble :)

First of, I'm not an expert (unfortunately) in subjects that I will talk about. But still, I've decided to share my thoughts.

Theory

The problem can be generalized in the following way. You want to have some continuous function that takes a 3D vector (which is device orientation in your case) and returns another vector that is orthogonal to original vector. Theory says (see hairy ball theorem) that for some arguments that function will return zero vectors. In case when such a function is compass, zero vectors are returned when device is oriented vertical (and this fells quite natural if you have ever used an ordinary compass).

Practice

  1. Sometimes you want your app to tell which side of the world does phone back (rear camera) is pointing to.
  2. Or maybe even you want combined approach:

    • If the phone is oriented flat, show what is the phone's top pointing to.
    • If the phone is oriented vertical, show what is the phone's back pointing to.

In both cases you need to use gyroscope in addition to compass.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top