Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the numbers from Game Rotation Vector in Android?

I am working on an AR app that needs to move an image depending on device's position and orientation.

It seems that Game Rotation Vector should provide the necessary data to achieve this. However I cant seem to understand what the values that I get from GRV sensor show. For instance in order to reach the same value on the Z axis I have to rotate the device 720 degrees. This seems odd.

If I could somehow convert these numbers to angles from the reference frame of the device towards the x,y,z coordinates my problem would be solved.

I have googled this issue for days and didn't find any sensible information on the meaning of GRV coordinates, and how to use them.

TL:DR What do the numbers of the GRV sensor show? And how to convert them to angles?

like image 881
Mike G Avatar asked Jan 05 '23 13:01

Mike G


1 Answers

As the docs state, the GRV sensor gives back a 3D rotation vector. This is represented as three component numbers which make this up, given by:

  1. x axis (x * sin(θ/2))
  2. y axis (y * sin(θ/2))
  3. z axis (z * sin(θ/2))

This is confusing however. Each component is a rotation around that axis, so each angle (θ which is pronounced theta) is actually a different angle, which isn't clear at all.

Note also that when working with angles, especially in 3D, we generally use radians, not degrees, so theta is in radians. This looks like a good introductory explanation.

But the reason why it's given to us in the format is that it can easily be used in matrix rotations, especially as a quaternion. In fact, these are the first three components of a quaternion, the components which specify rotation. The 4th component specifies magnitude, i.e. how far away from the origin (0, 0) a point it. So a quaternion turns general rotation information into an actual point in space.

These are directly usable in OpenGL which is the Android (and the rest of the world's) 3D library of choice. Check this tutorial out for some OpenGL rotations info, this one for some general quaternion theory as applied to 3D programming in general, and this example by Google for Android which shows exactly how to use this information directly.

If you read the articles, you can see why you get it in this form and why it's called Game Rotation Vector - it's what's been used by 3D programmers for games for decades at this point.

TLDR; This example is excellent.

Edit - How to use this to show a 2D image which is rotated by this vector in 3D space.

In the example above, SensorManage.getRo‌tationMatrixFromVecto‌r converts the Game Rotation Vector into a rotation matrix which can be applied to rotate anything in 3D. To apply this rotation a 2D image, you have to think of the image in 3D, so it's actually a segment of a plane, like a sheet of paper. So you'd map your image, which in the jargon is called a texture, onto this plane segment.

Here is a tutorial on texturing cubes in OpenGL for Android with example code and an in depth discussion. From cubes it's a short step to a plane segment - it's just one face of a cube! In fact that's a good resource for getting to grips with OpenGL on Android, I'd recommend reading the previous and subsequent tutorial steps too.

As you mentioned translation also. Look at the onDrawFrame method in the Google code example. Note that there is a translation using gl.glTranslatef and then a rotation using gl.glMultMatrixf. This is how you translate and rotate.

It matters the order in which these operations are applied. Here's a fun way to experiment with that, check out Livecodelab, a live 3D sketch coding environment which runs inside your browser. In particular this tutorial encourages reflection on the ordering of operations. Obviously the command move is a translation.

like image 122
r3flss ExlUtr Avatar answered Jan 08 '23 01:01

r3flss ExlUtr