I've got rotation matrix from TYPE_ROTATION_VECTOR vector values. and tried to use it to rotate an object in my app. Worked fine. But what I really want is to rotate an object opposite to the direction of device's movement. Say, device is rotated around x axes for 30 degrees, I need my object to rotate around x axes to -30 degrees. It has to have an effect as if the object is staying where it is irrespective of devices movement. To do that I inverted(transposed) the rotation vector. Now rotation around x and y axis are completely incorrect, it looks like they are swapped. when i rotate device around x, object is rotated around y, and vice versa. Piece of my code:
case Sensor.TYPE_ROTATION_VECTOR:
SensorManager.getRotationMatrixFromVector( mRotationMatrix , event.values);
Matrix.transposeM(mInvertedRotationMatrix, 0, mRotationMatrix, 0);
Quaternion quat = new Quaternion();
quat.fromRotationMatrix(mInvertedRotationMatrix);
mRenderer.setQuat(quat);
}
....
Quaternion quat = new Quaternion();
public void setQuat(Quaternion quat)
{
this.quat = quat;
mObjectGroup.setOrientation(quat);
}
mObjectGroup is the model that supposed to rotate when device is rotated
...
public void setOrientation(Quaternion quat) {
mOrientation.setAllFrom(quat);
}
...
mOrientation = new Quaternion();
...
public Quaternion() {
setIdentity();
mTmpVec1 = new Number3D();
mTmpVec2 = new Number3D();
mTmpVec3 = new Number3D();
}
public Quaternion setIdentity() {
w = 1;
x = 0;
y = 0;
z = 0;
return this;
}
...
public Number3D() {
this.x = 0;
this.y = 0;
this.z = 0;
}
public void setAllFrom(Quaternion other) {
this.w = other.w;
this.x = other.x;
this.y = other.y;
this.z = other.z;
}
EDIT: When I rotate the device along Y axis the object is rotated around Z axis and when the device is rotated along X axis the object is rotated around Y axis