Android Open Source - Gallery97 Movement Engine






From Project

Back to project page Gallery97.

License

The source code is released under:

GNU General Public License

If you think the Android project Gallery97 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 pl.kacprzak.klub97;
/*w w  w.j a v a  2  s  .  co m*/


public class MovementEngine {
  
  float[] acc = {0,0,0};
  float[] compass = {0,0,0};
  double dt = 0;
  
  float[] v = {0,0,0,0}; //0 - N, 1 - W, 2 - S, 3 - E

  int entries = 0;
  int direction = 0;
  float[] s = {0,0,0,0};
  float movementInterval = 0;
  float[] xy = {0,0};
  
  boolean goN = false;
  boolean goE = false;
  boolean goS = false;
  boolean goW = false;
  
  public MovementEngine(float mInv){
    histogramClear();
    movementInterval = mInv;
  }
  
  //called once per accelerator event
  public void inputAccReadings(float ax, float ay, float az, double deltaT){
    acc[0]=ax;
    acc[1]=ay;
    acc[2]=az;
    dt=deltaT;
  }
  
  //called once per orientation calculation
  public void inputCompass(float azimuth, float pith, float roll){
    compass[0]=azimuth;
    compass[1]=pith;
    compass[2]=roll;
  }
  
  //called once per event
  public void histogramFill(){

    //look in E direction
    if(Math.toDegrees(compass[0]) > -45 && Math.toDegrees(compass[0]) < 45){
      if(acc[2]>0) v[3] += acc[2]*dt;   
    }
    //look in N direction
    else if(Math.toDegrees(compass[0]) > 45 && Math.toDegrees(compass[0]) < 135){
      if(acc[2] > 0) v[0] += acc[2]*dt;
    }
    //look in S direction
    else if(Math.toDegrees(compass[0]) < -45 && Math.toDegrees(compass[0]) > -135){
      if(acc[2] > 0) v[2] += acc[2]*dt;
    }
    //look in W direction
    else{
      if(acc[2] > 0) v[1] += acc[2]*dt;
    }

    s[0] += v[0]*dt; //N
    s[1] += v[1]*dt; //W
    s[2] += v[2]*dt; //S
    s[3] += v[3]*dt; //E
    
    entries++;
    
    if(s[0] > 2f){
      s[0] = 0.0f;
      v[0] = 0.0f;
      goN = true;
    }
    
    if(s[1] > 2f){
      s[1] = 0.0f;
      v[1] = 0.0f;
      goW = true;
    }
    if(s[2] > 2f){
      s[2] = 0.0f;
      v[2] = 0.0f;
      goS = true;
      
    }
    if(s[3] > 2f){
      s[3] = 0.0f;
      v[3] = 0.0f;
      goE = true;
      
    }
    //Log.e("mylog", "mylog "+Math.toDegrees(compass[0])+" "+s[0]+" "+acc[2]);
    
  }
  
  //called once per move
  public float[] performCalculation(){

    if(goN) { xy[0] = 0; xy[1] = 2f; /*Log.e("mylog", "mylog go N");*/ }
    else if(goW) { xy[0] = 2f; xy[1] = 0; /*Log.v("mylog", "mylog go E");*/}
    else if(goS) { xy[0] = 0; xy[1] = -2f; /*Log.i("mylog", "mylog go S");*/}
    else if(goE) { xy[0] = -2f; xy[1] = 0; /*Log.d("mylog", "mylog go W");*/}
    else { xy[0] = 0; xy[1] = 0;}
    
    goN = false;
    goW = false;
    goS = false;
    goE = false;
    
    //Log.e("mylog", "mylog "+Math.toDegrees(compass[0])+" "+s[0]+" "+s[1]+" "+s[2]+" "+s[3]);
    //Log.e("mylog", "mylog "+entries+" "+xy[0]+" "+xy[1]);
    return xy;
  }
  
  public void histogramClear(){
    //v = new float[4];
    entries = 0;
  }
}




Java Source Code List

pl.kacprzak.klub97.AbstractScreen.java
pl.kacprzak.klub97.GdxApp.java
pl.kacprzak.klub97.InputListener.java
pl.kacprzak.klub97.LoadingScreen.java
pl.kacprzak.klub97.MieszkanieScreen.java
pl.kacprzak.klub97.MieszkanieStart.java
pl.kacprzak.klub97.MovementEngine.java