Android Open Source - study-android-fallballgame Sensor Value






From Project

Back to project page study-android-fallballgame.

License

The source code is released under:

Apache License

If you think the Android project study-android-fallballgame 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 jp.hym.falingballgamge;
/*  w  w  w.  java2  s  .c o m*/
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.Surface;

public class SensorValue{

  //public boolean isDiffDigree;

    private static final int MATRIX_SIZE = 16;
    float[]  inR = new float[MATRIX_SIZE];
    float[] outR = new float[MATRIX_SIZE];
    float[]    I = new float[MATRIX_SIZE];
    public float[] orientationValues   = new float[3];
    float[] orientationValuesBB   = new float[3];
    public float[] magneticValues      = new float[3];
    public float[] accelerometerValues = new float[3];

    final int displayOrientation;

    public SensorValue(int displayOrientation){
        this.displayOrientation = displayOrientation;
    }



    public void onSensorChanged(SensorEvent event){
      //????????????????????
      //??????????????????????Nexus???????????????????????????????????????
    //if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) return;

      switch (event.sensor.getType()) {
          case Sensor.TYPE_MAGNETIC_FIELD:
              magneticValues = event.values.clone();
              //Log.d("tets15", "mag");
              break;
          case Sensor.TYPE_ACCELEROMETER:
              accelerometerValues = event.values.clone();
              //Log.d("tets15", "acc");
              break;
      }

      if (magneticValues != null && accelerometerValues != null) {

          SensorManager.getRotationMatrix(inR, I, accelerometerValues, magneticValues);

//          //Activity?????????????????????????????????????????????????????
//          if(isDiffDigree){
//            SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X, SensorManager.AXIS_Y, outR);
//          }else{
//            SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X, outR);
//          }
//          SensorManager.remapCoordinateSystem(inR,  SensorManager.AXIS_X, SensorManager.AXIS_Y, outR);
          
          
          
          ////
          

//          // ????????????????????(???????????????????????)
//          Display disp = this.getWindowManager().getDefaultDisplay();
//          // ???????????????????API????8?????????????????
//          int dispDir = disp.getRotation();
//   
//          // ?????????????????????????????????????
//          if (dispDir == Surface.ROTATION_0) {
//              SensorManager.getOrientation(outR, orientationValuesBB);
//   
//              // ????????????
//          } else {
//   
//              float[] outR = new float[16];
//   
//              // 90???
//              if (dispDir == Surface.ROTATION_90) {
//                  SensorManager.remapCoordinateSystem(
//                          rotate, SensorManager.AXIS_Y,SensorManager.AXIS_MINUS_X, outR);
//                  // 180???
//              } else if (dispDir == Surface.ROTATION_180) {
//                  float[] outR2 = new float[16];
//   
//                  SensorManager.remapCoordinateSystem(
//                          rotate, SensorManager.AXIS_Y,SensorManager.AXIS_MINUS_X, outR2);
//                  SensorManager.remapCoordinateSystem(
//                          outR2, SensorManager.AXIS_Y,SensorManager.AXIS_MINUS_X, outR);
//                  // 270???
//              } else if (dispDir == Surface.ROTATION_270) {
//                  SensorManager.remapCoordinateSystem(
//                          outR, SensorManager.AXIS_MINUS_Y,SensorManager.AXIS_MINUS_X, outR);
//              }
//              SensorManager.getOrientation(outR, orientationValuesBB);
//          }
          

            if(displayOrientation == Surface.ROTATION_0) {
                SensorManager.remapCoordinateSystem(inR,  SensorManager.AXIS_X, SensorManager.AXIS_Y, outR);
            }
            else if(displayOrientation == Surface.ROTATION_90) {
                SensorManager.remapCoordinateSystem(inR,  SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, outR);
            }
            else if(displayOrientation == Surface.ROTATION_180) {
                SensorManager.remapCoordinateSystem(inR,  SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, outR);
            }
            else if(displayOrientation == Surface.ROTATION_270) {
                SensorManager.remapCoordinateSystem(inR,  SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, outR);
            }

            SensorManager.getOrientation(outR, orientationValuesBB);


      //???????????????
      float[] subst = new float[3];
      subst[0] = Math.abs( orientationValuesBB[0] - orientationValues[0] );
      subst[1] = Math.abs( orientationValuesBB[1] - orientationValues[1] );
      subst[2] = Math.abs( orientationValuesBB[2] - orientationValues[2] );
//      float subdist_s = subst[0] * subst[0] + subst[1] * subst[1] + subst[2] * subst[2];

      for(int i=0;i<3;i++){
        final float rad1 = (float)Math.toRadians(1);
        float w = 0.1f;
        if( subst[i] > rad1 * 45 ) w = 1.0f;
//        else if( subdist_s > rad1 * rad1 * 60 ) w = 0.2f;
//        else if( subdist_s > rad1 * rad1 *  5 ) w = 0.3f;
//        else w = 0.1f;
        orientationValues[i] = orientationValuesBB[i] * w + orientationValues[i] * (1.0f-w);
        }
//      orientationValues[0] = orientationValuesBB[0] * w + orientationValues[0] * (1.0f-w);
//      orientationValues[1] = orientationValuesBB[1] * w + orientationValues[1] * (1.0f-w);
//      orientationValues[2] = orientationValuesBB[2] * w + orientationValues[2] * (1.0f-w);

//      final float overv = (float)Math.toRadians(5);
//      float subdist = subst[0] * subst[0] + subst[1] * subst[1] + subst[2] * subst[2];
//      float w = 1;
////      if( subdist > overv*overv ){
////        orientationValues[0] += subst[0] * w;
////        orientationValues[1] += subst[1] * w;
////        orientationValues[2] += subst[2] * w;
////      }
//
      /*
          Log.d("test8", String.format("Orientation: z=%.1f, x=%.1f, Y=%.1f",
                  Math.toDegrees(orientationValues[0]),  //Z?????,azimuth
                  Math.toDegrees(orientationValues[1]),  //X?????,pitch
                  Math.toDegrees(orientationValues[2])  //Y?????,roll
            
            ));
          */
      }

    }


    public float getRoll(){
      return orientationValues[2];
    }
    public float getPitch(){
      return orientationValues[1];
    }
    public float getAzimuth(){
      return orientationValues[0];
    }
    public float getRollDegree(){
      return (float) Math.toDegrees(getRoll());
    }
    public float getPitchDegree(){
      return (float) Math.toDegrees(getPitch());
    }
    public float getAzimuthDegree(){
      return (float) Math.toDegrees(getAzimuth());
    }
}




Java Source Code List

jp.hym.falingballgamge.GameSurface.java
jp.hym.falingballgamge.GoalActivity.java
jp.hym.falingballgamge.HolderCallBack.java
jp.hym.falingballgamge.InclinationSensor.java
jp.hym.falingballgamge.MainActivity.java
jp.hym.falingballgamge.SensorValue.java