Hey guys! I wanted to create a little app / game based off of the way the user is holding the phone (sort of like an augmented reality to help you understand what I am trying to do) and that from my research will depend largely on the compass (https://github.com/libgdx/libgdx/wiki/Compass) I also managed to find this page http://www.badlogicgames.com/wordpress/?p=1490 but the CompassTest is no longer in the libgdx project for some reason. I managed to find http://stackoverflow.com/questions/5274514/how-do-i-use-the-android-compass-orientation-to-aim-an-opengl-camera which shows sort of how to do what I am asking but a lot of it is left unexplained and I know the 3d part of libgdx has changed greatly since the time that the post was written. If this code is possibly helpful I would appreciate it if someone could explain it or point me in the direction to rotate a PerspectiveCamera based off the compass. Since it wasn’t for me this is what I came up with:
From my research and testing I have come up with this but it only seems to work if I want to use one axis at a time.
//azimuth example
float deltaAzimuth, lastAzimuth;
public void render(float delta){
//the actual drawing stuff is here
deltaAzimuth = (Gdx.input.getAzimuth() - lastAzimuth);
camera.rotate(camera.up, -deltaAzimuth);
camera.update();
lastAzimuth = Gdx.input.getAzimuth();
}
//roll example
float deltaRoll, lastRoll;
public void render(float delta){
deltaRoll = (Gdx.input.getRoll() - lastRoll);
camera.rotate(new Vector3(camera.up).crs(camera.direction).nor(), deltaRoll); //creates right vector
camera.update();
lastRoll = Gdx.input.getRoll();
}
If I choose one to use it seems to work very well. But when I combine both azimuth and roll it does not work so well… That may be because I am missing the pitch in this. I attempted to implement pitch with the same deltaPitch and lastPitch aproach but the values appear to be in the range of [-90, 90] rather then the azimuth and roll which have the range of [-180,180]. If I combine both roll and azimuth and I shake my android phone things sort of seem to spin around which is hard to describe. All I know is that shaking my phone should not cause this effect. I figured it may be a problem with the cross product calculation but so far no luck with anything. If you see anything apparent or have suggestions please let me know. Thanks!
p.s. Once I finish this I plan on documenting everything and releasing a public source code git repo since everything has been rather difficult for me to find and I would rather other people not to go through this