Android Open Source - zomblind acelerometro






From Project

Back to project page zomblind.

License

The source code is released under:

Apache License

If you think the Android project zomblind 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

/*******************************************************************************
 * Copyright 2013 Antonio Fern?ndez Ares (antares.es@gmail.com)
 * /* w ww . j av a2s  .  c om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package antares.zomblind.in;

import android.content.Context;
import android.hardware.SensorEvent;
import antares.zomblind.ZomblindActivity;

public class Acelerometro {

  // Variables de sensores: Aceler?metro
  float last_update = 0, last_movement = 0;
  float prevX = 0, prevY = 0, prevZ = 0;
  float curX = 0, curY = 0, curZ = 0;
  
  long current_time;
  public short X = 0, Y = 0, Z = 0;
  boolean instanciado = false;
  
  //Degradaci?n: Duraci?n del movimiento en el tiempo. A m?s degradaci?n menor duraci?n. Mayor siempre de 0.
  //double degradacion = 1.05;
  double degradacion = 1.02;
  
  //Amplitud: Como de grande es el m?vimiento.
  int amplitud_flojo = 100;
  int amplitud_fuerte = 200;
  
  //Umbrales
  int umbral_flojo = 50;
  int umbral_medio = 100;
  int umbral_fuerte = 150;

  // Sensibilidad del sensor de golpe fuerte. Cuanto "mayor" valor, mayor movimiento se necesitar? para detectar un cambio
  float min_movement_fuerte =0.0000004f; // 2f ;//1E-6f;
  
  // Sensibilidad del sensor de golpe normal. Cuanto "mayor" valor, mayor movimiento se necesitar? para detectar un cambio
  float min_movement_flojo = 0.0000003f; // 2f ;//1E-6f; //0.0000003f 

  //Contexto
  private ZomblindActivity _z;
  
  public Acelerometro(Context ctx){
    _z = (ZomblindActivity) ctx;
  }
  
  public Acelerometro() {

  }

  public Acelerometro(float[] values, long current_time) {

    curX = values[0];
    curY = values[1];
    curZ = values[2];

    this.current_time = current_time;

    if (prevX == 0 && prevY == 0 && prevZ == 0) {
      last_update = current_time;
      last_movement = current_time;
      prevX = curX;
      prevY = curY;
      prevZ = curZ;
    }
  }

  public void update(SensorEvent event) {
    
    synchronized (this) {
    
    curX = event.values[0];
    curY = event.values[1];
    curZ = event.values[2];

    this.current_time = event.timestamp;

    if (instanciado==false) {
      last_update = current_time;
      last_movement = current_time;

      prevX = curX;
      prevY = curY;
      prevZ = curZ;
      
      instanciado=true;
    }

    long time_difference = (long) (current_time - last_update);
    
    if (time_difference > 0) {
      float movement_X = Math.abs(curX - prevX) / time_difference;
      boolean direcion_X = curX > prevX ? true : false;

      if (movement_X > min_movement_fuerte) {
        X = (short) (direcion_X == true ? amplitud_fuerte : -amplitud_fuerte);
      }else if (movement_X > min_movement_flojo) {
        X = (short) (direcion_X == true ? amplitud_flojo : -amplitud_flojo);
      } else {
        X = (short) (X/degradacion);
      }

      float movement_Y = Math.abs(curY - prevY) / time_difference;
      boolean direcion_Y = curX > prevY ? true : false;

      if (movement_Y > min_movement_fuerte) {
        Y = (short) (direcion_Y == true ? amplitud_fuerte : -amplitud_fuerte);
      }else if (movement_Y > min_movement_flojo) {
        Y = (short) (direcion_Y == true ? amplitud_flojo : -amplitud_flojo);
      } else {
        Y = (short) (Y/degradacion);
      }
      float movement_Z = Math.abs(curZ - prevZ) / time_difference;
      boolean direcion_Z = curX > prevZ ? true : false;

      if (movement_Z > min_movement_fuerte) {
        Z = (short) (direcion_Z == true ? amplitud_fuerte : -amplitud_fuerte);
      }else if (movement_Z > min_movement_flojo) {
        Z = (short) (direcion_Z == true ? amplitud_flojo : -amplitud_flojo);
      } else {
        Z = (short) (Z/degradacion);
      }

      prevX = curX;
      prevY = curY;
      prevZ = curZ;
      last_update = current_time;
    }
    }

  }
  
  public String toString(){
    return X+":"+Y+":"+Z;
    
  }
  
  public boolean golpe_izquierda(){
    return X>umbral_flojo;
  }
  
  public boolean golpe_izquierda_fuerte(){
    return X>umbral_fuerte;
  }
  
  public boolean golpe_derecha(){
    return X<-umbral_flojo;
  }
  
  public boolean golpe_derecha_fuerte(){
    return X<-umbral_fuerte;
  }
  
  public boolean golpe_frente(){
    return Z>umbral_flojo;
  }
  
  public boolean golpe_frente_fuerte(){
    return Z>umbral_fuerte;
  }
  
  public boolean recargar(){
    return Y>umbral_flojo;
  }
  
  public boolean lanzar(){
    return Y<-umbral_medio;
  }
  
  public boolean esquivar(){
    return Z<-umbral_medio;
  }
  
  public boolean isgolpeando(){
    return (X>umbral_flojo||X<-umbral_flojo||Z<-umbral_flojo);
  }

}




Java Source Code List

antares.zomblind.ZomblindActivity.java
antares.zomblind.core.Jugador.java
antares.zomblind.core.Nucleo.java
antares.zomblind.core.levels.L000_menu.java
antares.zomblind.core.levels.NivelInfo.java
antares.zomblind.core.levels.checker.GolpeFrontal.java
antares.zomblind.core.levels.checker.Todos.java
antares.zomblind.core.levels.checker._Chequeador.java
antares.zomblind.core.levels.conditions.SinZombies.java
antares.zomblind.core.levels.conditions._Condicion.java
antares.zomblind.core.levels.generate.AleatorioFlojos.java
antares.zomblind.core.levels.generate.AleatorioTodos.java
antares.zomblind.core.levels.generate._Generador.java
antares.zomblind.core.levels.instancia.java
antares.zomblind.core.npcs.NpcData.java
antares.zomblind.core.npcs.NpcLista.java
antares.zomblind.core.npcs.npc.java
antares.zomblind.core.objetos.ArmaData.java
antares.zomblind.core.objetos.ArmaLista.java
antares.zomblind.core.objetos.Arma.java
antares.zomblind.core.objetos.Municion.java
antares.zomblind.core.opciones.java
antares.zomblind.in.acelerometro.java
antares.zomblind.in.orientacion.java
antares.zomblind.in.pantalla.java
antares.zomblind.in.teclado.java
antares.zomblind.out.debug.java
antares.zomblind.out.habladora.java
antares.zomblind.out.interfaz.java
antares.zomblind.out.vibrador.java