Fixed accelerometer input

This commit is contained in:
pelya
2010-07-09 17:43:46 +03:00
parent e901c4971e
commit 048735ac4a
5 changed files with 82 additions and 11 deletions

View File

@@ -102,8 +102,10 @@ fi
AppFullNameUnderscored=`echo $AppFullName | sed 's/[.]/_/g'`
AppSharedLibrariesPath=/data/data/$AppFullName/lib
ScreenOrientation1=portrait
HorizontalOrientation=false
if [ "$ScreenOrientation" = "h" ] ; then
ScreenOrientation1=landscape
HorizontalOrientation=true
fi
AppDataDownloadUrl1="`echo $AppDataDownloadUrl | sed 's/[&]/%26/g'`"
if [ "$SdlVideoResize" = "y" ] ; then
@@ -153,6 +155,7 @@ cat project/src/Globals.java | \
sed "s^public static String DataDownloadUrl = \".*\";^public static String DataDownloadUrl = \"$AppDataDownloadUrl1\";^" | \
sed "s/public static boolean DownloadToSdcard = .*;/public static boolean DownloadToSdcard = $DownloadToSdcard1;/" | \
sed "s/public static boolean NeedDepthBuffer = .*;/public static boolean NeedDepthBuffer = $NeedDepthBuffer;/" | \
sed "s/public static boolean HorizontalOrientation = .*;/public static boolean HorizontalOrientation = $HorizontalOrientation;/" | \
sed "s%public static String ReadmeText = .*%public static String ReadmeText = \"$ReadmeText\".replace(\"^\",\"\\\n\");%" | \
sed "s/public LoadLibrary() .*/public LoadLibrary() { $LibrariesToLoad };/" > \
project/src/Globals.java.1

View File

@@ -88,12 +88,11 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeKey) ( JNIEnv* env, jobject thiz, jint
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobject thiz, jfloat accX, jfloat accY, jfloat accZ )
static void updateOrientation ( float accX, float accY, float accZ )
{
// TODO: use accelerometer as joystick, make this configurable
// Currenly it's used as cursor + Home/End keys
static const float dx = 1.0, dy = 1.0, dz = 1.0;
// Currenly it's used as cursor + KP7/KP9 keys
static const float dx = 0.2, dy = 0.2, dz = 0.2;
static float midX = 0, midY = 0, midZ = 0;
static int pressLeft = 0, pressRight = 0, pressUp = 0, pressDown = 0, pressR = 0, pressL = 0;
@@ -182,8 +181,66 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobjec
if( accY > midY - dy*2 )
midY = accY + dy*2;
if( accZ < midZ + dz )
{
if( !pressL )
{
pressL = 1;
SDL_SendKeyboardKey( SDL_PRESSED, SDL_SCANCODE_KP_7 );
}
}
else
{
if( pressL )
{
pressL = 0;
SDL_SendKeyboardKey( SDL_RELEASED, SDL_SCANCODE_KP_7 );
}
}
if( accZ < midZ + dz*2 )
midZ = accZ - dz*2;
if( accZ > midZ - dz )
{
if( !pressR )
{
pressR = 1;
SDL_SendKeyboardKey( SDL_PRESSED, SDL_SCANCODE_KP_9 );
}
}
else
{
if( pressR )
{
pressR = 0;
SDL_SendKeyboardKey( SDL_RELEASED, SDL_SCANCODE_KP_9 );
}
}
if( accZ > midZ - dz*2 )
midZ = accZ + dz*2;
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobject thiz, jfloat accPosX, jfloat accPosY, jfloat accPosZ )
{
// Calculate two angles from three coordinates - TODO: this is faulty!
//float accX = atan2f(-accPosX, sqrtf(accPosY*accPosY+accPosZ*accPosZ) * ( accPosY > 0 ? 1.0f : -1.0f ) ) * M_1_PI * 180.0f;
float normal = sqrt(accPosX*accPosX+accPosY*accPosY+accPosZ*accPosZ);
if(normal <= 0.0000001f)
normal = 1.0f;
float accX = accPosX/normal;
//float accY = atan2f(accPosZ, accPosY) * M_1_PI;
float accY = accPosY/normal;
updateOrientation (accX, accY, 0.0f);
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat accX, jfloat accY, jfloat accZ )
{
updateOrientation (accX, accY, accZ);
}
void ANDROID_InitOSKeymap()
{

View File

@@ -43,15 +43,22 @@ class AccelerometerReader implements SensorListener {
public synchronized void onSensorChanged(int sensor, float[] values) {
v[0] = values[0];
v[1] = values[1];
v[2] = values[2];
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
if( values.length >= 1 )
v[0] = values[0];
if( values.length >= 2 )
v[1] = values[1];
if( values.length >= 3 )
v[2] = values[2];
if( Globals.HorizontalOrientation )
{
v[0] = values[1];
v[1] = values[2];
v[2] = values[0];
}
nativeAccelerometer(v[0], v[1], v[2]);
}
if (sensor == SensorManager.SENSOR_ORIENTATION) {
nativeOrientation(v[0], v[1], v[2]);
}
}
@@ -68,6 +75,7 @@ class AccelerometerReader implements SensorListener {
};
private native void nativeAccelerometer(float accX, float accY, float accZ);
private native void nativeOrientation(float accX, float accY, float accZ);
}

View File

@@ -18,6 +18,9 @@ class Globals {
// Set this value to true if you're planning to render 3D using OpenGL - it eats some GFX resources, so disabled for 2D
public static boolean NeedDepthBuffer = false;
// Set this value to true if you're planning to render 3D using OpenGL - it eats some GFX resources, so disabled for 2D
public static boolean HorizontalOrientation = true;
// Readme text to be shown on download page
public static String ReadmeText = "^Use accelerometer to navigate menus and control ship^Press \"Menu\" to select menu and for secondary fire^Press \"Call\" or touch screen for primary fire^Press \"Volume Up/Down\" to cycle through weapons".replace("^","\n");

View File

@@ -49,7 +49,7 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer {
System.loadLibrary("sdl_main");
nativeInit(); // Calls main() and never returns, hehe - we'll call eglSwapBuffers() from native code
System.exit(0);
}
public int swapBuffers() // Called from native code, returns 1 on success, 0 when GL context lost (user put app to background)