Rotate a rotation matrix about what would be the pitch Euler angle - android

I've got my 3D scene's camera rotating as desired, but to do it I converted to Euler angles using SensorManager.getOrientation(rotationMatrixIn, eulerAnglesOut); so I could add 90 to the pitch before recalculating a rotation matrix from the modified Euler angles. I'm adding 90 to the pitch so my rotation can be used for a cubemap where Y is up when the phone is held perpendicular to the ground.
I'm using Euler angles because this made it intuitive to rotate the pitch by 90 degrees. But I'm suffering from gimbal lock now when the phone is held near vertical and the pitch approaches -90 or 90 degrees. The view rapidly rotates 180 degrees (in the roll and azimuth axes) when it gets close to these angles. This does not occur when I use the unmodified rotation matrix, but then I don't have the orientation corrections I need.
I'm having trouble wrapping my head around the math needed to skip the Euler angles conversion and directly rotate the rotation matrix by 90 degrees about what would be the X axis in Euler angles and invert the roll. I have access to various math classes from a library (Matrix and Quaternion) which can be multiplied by each other and convert to and from Eulers.
I want to rotate about what would be the X axis. Is there a way to do this without introducing gimbal lock?
To clarify: the Euler angles I'm talking about are in -Z, X, Y order, where X is the pitch.

As #meowgoesthedog stated in a deleted comment, the rotation matrix should be pre-multiplied by a matrix that holds a 90 degree rotation about the X axis.
The rotation matrix from Android's sensors converts device orientation to world orientation, so pre-multiplying it by some other matrix is like applying a rotation to the device first before converting the coordinate system.
I was also tripped up by Android's row-major matrix ordering. The matrix must be inverted to get it into OpenGL's column-major ordering. My Euler angles worked OK because they were hiding this inconsistency.

Related

Android Rotation Vector sensor and Rotation Matrix

I'm new in the rotation matrices field and I'm trying to understand the behave of the Rotation Vector Sensor. I can't understand the relation between yaw, pith and roll with the values returned by the Rotation Vector Sensor.
I checked the source code of the SensorManager.getRotationMatrixFromVector() here and the Android documentation. The Rotation vector sensor returns the following values:
values[0]: x*sin(θ/2)
values[1]: y*sin(θ/2)
values[2]: z*sin(θ/2)
values[3]: cos(θ/2)
values[4]: estimated heading Accuracy (in radians) (-1 if unavailable)
From the documentation: "the device has rotated through an angle θ around an axis (x, y, z)". So my dubt now is, what is θ? And what are x,y and z?
Following this tutorial from the OpenGL documentation, to rotate a vector V I need the pith, roll and yaw values with which I build the rotation matrix. At the end I have to multiply this matrix by V to rotate the vector. If I'm right, this matrix is different from the one related to the values vector obtained using SensorManager.getRotationMatrixFromVector(). Am I right? So what represents that matrix?

How can I get horizontal rotation angle whatever device orientation?

I can get horizontal rotation angle by calculating the roll value (according to the definition of iOS Device Motion) when the device is portrait.
The x, y, z-axis of the mobile device:
But when the device is in landscape, y-axis is horizontal and x-axis is vertical. How can I get the angle? The pitch value is not correct. I have tried to exchange x and y in quaternion but not worked.
And more, how can I get the angle when the device is in the middle of portrait and landscape, for example, you tilt the device 30 degrees about z-axis?
Is there a unified quaternion or rotational matrix to calculate the angle whatever device orientation?
Probably you want to get angle of some device "direction". For example if you want to get angle of Z axis projected on ground, do it exactly.
Transform the Z(0,0,1) vector into current device rotation.
You have direction of "ZinWorld" vector in some world frame.
According to world frame orientation , project "ZinWorld" into ground plane. For example , if in default world frame Z is pointing to UP , then just take X Y components of "ZinWorld".
Take atan2(X, Y) to get proper angle.

Set sprite rotation based on heading

I want to set a 2D sprite's rotation so it faces the direction it's moving in. Currently I hooked the accelerometer to the sprite's linear velocity and when I tilt my device it doesn't rotate, only moves. I am running AndEngine on Android.
I want to calculate x+/x-/y+/y- to receive a value in rotation degrees.
atan2(y,x) should do the trick.
So if angle=0 is in positive x direction,
angle = Math.atan2(y_velocity, x_velocity);
gives you the angle you have to rotate.
Figured it out eventually, to achieve this I did the following:
float radians=(float)Math.atan2(-acceleration.x, acceleration.y); //No Idea why I had to invert x axiz but it wouldn't work without it being done
float degrees=(float)Math.toDegrees(radians)+90; //Had to rotate my sprite by 90 degrees
radians=(float)Math.toRadians(degrees);
sprite.setTransform(sprite.getWorldCenter(), radians);

gimbal lock, can't solve by quaternion

When device is rotated by some amount, a simple cube has to rotate by the same amount but in the opposite direction. For example, the cube has to rotate to 45 degrees to the left if the device is rotated 45 degrees to the right. Or when pitch is 30 degrees, the cube has to rotate -30 degrees around X axis. When the yaw is 10 degrees, the cube has to rotate -10 degrees around Z axis. I've used .getRotationMatrixFromVector followed by getOrientation like so:
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(
mRotationMatrix , event.values);
SensorManager.getOrientation(mRotationMatrix, orientationVals);
azimuthVal = (Math.round((Math.toDegrees(orientationVals[0]))*100.0)/100.0);
pitchVal= (Math.round((Math.toDegrees(orientationVals[1]))*100.0)/100.0);
rollVal = (Math.round((Math.toDegrees(orientationVals[2]))*100.0)/100.0);}
But the problem with it change in pitch affects roll and vice versa and as a result when device is rotated around X axis, the pinch value changes->roll changes -> the cube rotates not only around X but also around Y, when I don't need that.
I've looked around the internet and many refer to Quaternions as a solution but how can I apply quaternions to my specific application, as I need to know amount of degrees device is rotated by along an axes.
Gimbal lock happens when you want to extract (Euler) rotation angles from the rotation matrix, basically at some specific rotation we loose a degree of freedom in the equation between rotation matrix components and rotation angles and the actual rotation angles are not recoverable,
So in your code it may happen at :
SensorManager.getOrientation(mRotationMatrix, orientationVals);
You should somehow solve the issue before extracting the rotation angles,
this could be done by modifying the quaternion's components as is explained here:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/

Cube rotation issue in opengl android

I want to rotate the cube when the user is touching it. To rotate it,is quite simple but actual problem is if the user is tilting it then always it should stop in particuler face(cube has 6 faces and i mean that only one face should be visible at one time). Please give your suggetion if anyone worked on that.
In the case of a cube this is simple: Face normals are the cartesian axes. So one looks straigt on a face if you constrain the rotations around the cartesian axes (X, Y, Z) and the rotation angles are multiple of pi/2 = 90°.
So in your code when the user stops interacting, set the rotation angles to the next multiple of 90°
fmod(round(angle/90) * 90, 360); // degrees
fmod(round(angle/(pi/2)) * pi/2, 2*pi); // radians
Either do it hard, or animate it.
If your object is not a cube, but arbitrary, you need to find the additional rotation for the face to get perpendicular to the view axis. This angle is determined by acos( scalar_product(normalize(face_normal), normalize(view_axis)) ) the axis of rotation is given by cross_product(normalize(face_normal), normalize(view_axis))

Categories

Resources