0

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

Nazerke
  • 2,098
  • 7
  • 37
  • 57
  • Is your rotation matrix 3x3 or 4x4? I think you can only transpose a 3x3 to invert it (correct me if I'm wrong). – Roger Rowland Mar 14 '13 at 12:46
  • 4X4, it doesn't say anything about it in android's official developers page – Nazerke Mar 14 '13 at 14:26
  • Traspose should be fine on 4x4 if position part is (0,0,0).. Anyway if only your axis is swapped you might just be looking at the object from the wrong position. In general, you cant know if your device coordinate system is same as your applications. You might need to manually swap each axis of your "SensorManagers" matrix. Ether a line or a column of top left 3x3 matrix will represent 1 axis. – Matic Oblak Mar 15 '13 at 09:55
  • 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. Do you mean manually swapping inverted rotation matrix? It means that I may potentially get Y axis by manually changing the axis and I didn't have problems with Z axis, but how do I find X axis? – Nazerke Mar 18 '13 at 10:48

0 Answers0