If you think the Android project clash_of_balls 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 (C) 2012-2013 Hans Hardmeier <hanshardmeier@gmail.com>
* Copyright (C) 2012-2013 Andrin Jenal//fromwww.java2s.com
* Copyright (C) 2012-2013 Beat Kng <beat-kueng@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/package com.sapos_aplastados.game.clash_of_balls.game;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.HandlerThread;
/**
* this thread gets the sensor data
* it can be calibrated to use a specific vector as zero vector
* this specific vector is the average over a certain time period
*
*/publicclass SensorThread implements SensorEventListener {
HandlerThread m_handler_thread;
privatestaticfinalfloat output_scaling = 0.5f;
private Vector3D m_calib_vec; //calibration vector
privatevolatile Vector3D m_cur_vec; //is accessed from different threads!
private Vector3D m_cur_vec_tmp;
privatefinal Vector3D m_z_axis = new Vector3D(0.f, 0.f, -1.f);
privatevolatileboolean m_bIs_calibrating;
Context m_context;
SensorManager m_sensor_manager;
public SensorThread(Context context) {
m_context = context;
}
publicvoid stopThread() {
if(m_sensor_manager!=null) m_sensor_manager.unregisterListener(this);
if(m_handler_thread!=null) m_handler_thread.quit();
}
publicvoid startThread() {
m_calib_vec = new Vector3D(0.f, 0.f, -1.f);
m_cur_vec = new Vector3D();
m_cur_vec_tmp = new Vector3D();
m_handler_thread = new HandlerThread("SensorThread");
m_handler_thread.start();
Handler handler = new Handler(m_handler_thread.getLooper());
m_sensor_manager=(SensorManager)m_context.getSystemService(
Context.SENSOR_SERVICE);
Sensor sensor = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
m_sensor_manager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_GAME, handler);
}
publicvoid calibrate() {
m_calib_vec.set(0.f, 0.f, -0.1f);
m_bIs_calibrating = true;
}
publicvoid stopCalibrate() {
if(!m_bIs_calibrating) return;
synchronized (this) {
m_calib_vec.normalize();
m_bIs_calibrating = false;
}
}
publicboolean isCalibrating() {
return m_bIs_calibrating;
}
private Vector3D m_tmp_vec_get1=new Vector3D();
private Vector3D m_tmp_vec_get2=new Vector3D();
publicvoid getCurrentVector(Vector result) {
//rotate & project vector to xy plane
float angle = m_calib_vec.angle(m_z_axis);
Vector3D v_n = m_tmp_vec_get1;
v_n.set(m_calib_vec);
v_n.cross(m_z_axis); //normal vector
Vector3D v = m_tmp_vec_get2;
v.set(m_cur_vec);
v.rotate(v_n, angle);
//rotate because we use portrait format, and another coord system
result.set(-v.y * output_scaling, v.x * output_scaling);
}
publicvoid onAccuracyChanged(Sensor sensor, int accuracy) {
}
publicvoid onSensorChanged(SensorEvent event) {
if(m_bIs_calibrating) {
synchronized (this) {
if(m_bIs_calibrating) {
m_calib_vec.add(-event.values[0], -event.values[1], -event.values[2]);
}
}
} else {
m_cur_vec_tmp.set(-event.values[0], -event.values[1], -event.values[2]);
Vector3D v = m_cur_vec;
m_cur_vec = m_cur_vec_tmp;
m_cur_vec_tmp = v;
}
}
}