Android Open Source - CoreGame Android Orientation Handler






From Project

Back to project page CoreGame.

License

The source code is released under:

Copyright (c) 2011 Andrey Moiseev, http://o2genum.ru Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),...

If you think the Android project CoreGame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ru.o2genum.coregame.framework.impl;
//from  w  ww  .  ja v a  2  s  .  com
import android.hardware.*;
import android.content.Context;

public class AndroidOrientationHandler implements OrientationHandler,
      SensorEventListener
{
  SensorManager manager;
  Sensor sensor;
  float azimuth = 0.0F;

  public AndroidOrientationHandler(Context context)
  {
    manager = 
      (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
  }

  public void onAccuracyChanged(Sensor sensor, int accuracy)
  {
    // Nothing to do
  }

  public void onSensorChanged(SensorEvent event)
  {
    /* Now it works using accelerometer, not orientation
     * sensor. Because:
     *
     * Fivos Asimakop wrote:
     *
     * Try an alternative of the controls with the azimuth. In my
     * experience most devices lose their calibration easily and the azimuth
     * is not to be trusted. Especially after deprecating the orientation
     * sensor. How about using an accelerometer reading? Or the rotation
     * vector when android version is 2.3 and above?
     */

    azimuth = (float) (Math.atan2((double) event.values[1],
        (double) event.values[0]) / (Math.PI *2) * 360);
  }

  @Override
  public float getAzimuth()
  {
    return azimuth;
  }
}




Java Source Code List

ru.o2genum.coregame.framework.Audio.java
ru.o2genum.coregame.framework.FileIO.java
ru.o2genum.coregame.framework.Game.java
ru.o2genum.coregame.framework.Graphics.java
ru.o2genum.coregame.framework.Input.java
ru.o2genum.coregame.framework.Pool.java
ru.o2genum.coregame.framework.Screen.java
ru.o2genum.coregame.framework.Sound.java
ru.o2genum.coregame.framework.Vibration.java
ru.o2genum.coregame.framework.impl.AndroidAudio.java
ru.o2genum.coregame.framework.impl.AndroidFastRenderView.java
ru.o2genum.coregame.framework.impl.AndroidFileIO.java
ru.o2genum.coregame.framework.impl.AndroidGame.java
ru.o2genum.coregame.framework.impl.AndroidGraphics.java
ru.o2genum.coregame.framework.impl.AndroidInput.java
ru.o2genum.coregame.framework.impl.AndroidOrientationHandler.java
ru.o2genum.coregame.framework.impl.AndroidSound.java
ru.o2genum.coregame.framework.impl.AndroidVibration.java
ru.o2genum.coregame.framework.impl.KeyboardHandler.java
ru.o2genum.coregame.framework.impl.MultiTouchHandler.java
ru.o2genum.coregame.framework.impl.OrientationHandler.java
ru.o2genum.coregame.framework.impl.SingleTouchHandler.java
ru.o2genum.coregame.framework.impl.TouchHandler.java
ru.o2genum.coregame.game.Core.java
ru.o2genum.coregame.game.Dot.java
ru.o2genum.coregame.game.GameActivity.java
ru.o2genum.coregame.game.GameScreen.java
ru.o2genum.coregame.game.VectorF.java
ru.o2genum.coregame.game.World.java