5

So I've been poring over the SensorEvent documentation trying to get a handle on how to figure out what direction is north, relative to a given axis of the phone. I've drawn a little image that illustrates my conception of how the coordinate system works:

my conception of the coordinate system

So if the world coordinates are x, y and z, where Magnetic north is along z and y points towards the sky, and the coordinates of the phone are Px, Py and Pz, then I'd like to be able to calculate the projection of each vector onto the other.

It seems like SENSOR_TYPE_ROTATION_VECTOR might be the right thing to be looking at, but it seems like that's not giving me enough information to get all these projections. Am I supposed to normalize ROTATION_VECTOR and add it to the axis I care about, then pull out the components?

The other big single sensor seems to be SENSOR_TYPE_ORIENTATION, but again I'm not clear on what to do with these values. If I want to know the three projections of the real-world coordinate system onto Py, would I just rotate [0, 1, 0] along the given coordinates, like so:

// Assume here that I've received values
// and broken them out into 
// a = azimuth
// p = pitch
// r = roll

// Convert to radians
a = a*Math.Pi/180;
p = p*Math.PI/180;
r = r*Math.PI/180;

// Given that Py is initially 0, 1, 0, apply general rotation matrix:
float[] Py = new float[3];
Py[0] = -Math.cos(p)*Math.sin(a) + Math.sin(p)*Math.sin(r)*Math.cos(a);
Py[1] = Math.cos(p)*Math.cos(a) + Math.sin(p)*Math.sin(r)*Math.sin(a);
Py[2] = Math.sin(p)*Math.cos(a);

Where I just got those formulas from the formula for general rotations (since it's rotating a unit vector, you can just select the center column). I would think that the components of the variable Py would then be the projections of x, y and z onto Py, but do I have that backwards? Is it instead the projections of Py onto each of the three real-world axes?

Finally, I've noticed that there's a getRotationMatrixFromVector() option, which seems like it calculates these projections for you, but again I'm not sure if I have things completely backwards. If I want to know the three projections of x. y and z onto Py, do I get the 2nd column of the Rotation Matrix, or the 2nd row?

(Sorry for the very wordy version of what's probably a quite simple question, I figure it's better for future confused people to be very explicit about the coordinate system, which is my major point of confusion).

Paul
  • 9,014
  • 9
  • 41
  • 75
  • 2
    I can't remember exactly how that all worked but that sourcecode [here (SensorManager.getRotationMatrix()](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/hardware/SensorManager.java#1188) helped me once. – zapl Mar 19 '12 at 18:06

0 Answers0