Andriod's Motion Sensing Inputs!

I need to access android’s motion sensing capabilities in jme3 code, can any1 please suggest me how can i?

your gonna want an appstate for this eventually, as per the manual suggests, but to start off you can just implement SensorEventListener in the MainActivity.java file

1 Like

Hello @wezrule! Thank you for replying.

I’m completely new to this JME Library. I Startd playin with this since last 3 weeks.I’ve follwd Beginners tutorial and certainly came up with this.







I’ve zero experience with android coding.



The mobile version of the code looks like this:



public class MainActivity extends AndroidHarness{





public MainActivity(){

// Set the application class to run

appClass = “mygame.Main”;

// Try ConfigType.FASTEST; or ConfigType.LEGACY if you have problems

eglConfigType = ConfigType.BEST;

// Exit Dialog title & message

exitDialogTitle = “Exit?”;

exitDialogMessage = “Press Yes”;

// Enable verbose logging

eglConfigVerboseLogging = false;

// Choose screen orientation

screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

// Invert the MouseEvents X (default = true)

mouseEventsInvertX = true;

// Invert the MouseEvents Y (default = true)

mouseEventsInvertY = true;

}



}



I want to use accelerometer feature of android in this mini game below. so that i can run it on my android device.



On PC version i used keyboard inputs to control the board. and used sphere collision shape to roll the ball on the board.



i want to know if there is any short trick to send those accelerometer inputs to Main Class or if there is any pre implemented libraries for this.

Thank you

I’ve played around with the accelerometer before, heres some raw code i was using, cba to clean it up:



[java]

package com.mycompany.mygame;



import android.content.pm.ActivityInfo;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.util.Log;



import com.jme3.app.AndroidHarness;

import com.jme3.math.Quaternion;

import com.jme3.math.Vector3f;

import com.jme3.system.android.AndroidConfigChooser.ConfigType;

import java.util.concurrent.Callable;

import mygame.Main;



public class MainActivity extends AndroidHarness implements SensorEventListener {



private SensorManager sensorManager = null;

private float[] orientation = {0, 0, 0}, lastAcc = {0, 0, 0}, lastMag = {0, 0, 0}, rotation = {0, 0, 0, 0, 0, 0, 0, 0, 0}, outRotation = {0, 0, 0, 0, 0, 0, 0, 0, 0};

private Main main;

private final float FILTER_FACTOR = 0.8f;



@Override

protected void onResume() {

super.onResume();

// Register this class as a listener for the accelerometer sensor

sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

// …and the orientation sensor

sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);

}



@Override

protected void onStop() {

// Unregister the listener

sensorManager.unregisterListener(this);

super.onStop();

}



@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

}



public MainActivity() {



appClass = “mygame.Main1”;



eglConfigType = ConfigType.BEST;



exitDialogTitle = “Exit?”;

exitDialogMessage = “Press Yes”;

eglConfigVerboseLogging = false;



screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

}



@Override

public void onAccuracyChanged(Sensor arg0, int arg1) {

// TODO Auto-generated method stub

}



@Override

public void onSensorChanged(SensorEvent sensorEvent) {



// wait for app to be up before firing in sensors

if (main == null) {

return;

}



if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

lastAcc = sensorEvent.values.clone();



} else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {

lastMag = sensorEvent.values.clone();

}



if (lastAcc != null && lastMag != null) {



/* new code for low-pass filter, doesn’t seem to work /

// lastAcc[0] = FILTER_FACTOR * lastAcc[0] + (1 - FILTER_FACTOR) * sensorEvent.values[0];

// lastAcc[1] = FILTER_FACTOR * lastAcc[1] + (1 - FILTER_FACTOR) * sensorEvent.values[1];

// lastAcc[2] = FILTER_FACTOR * lastAcc[2] + (1 - FILTER_FACTOR) * sensorEvent.values[2];

/
end of new code */



SensorManager.getRotationMatrix(rotation, null, lastAcc, lastMag);



//check that thing about adjusting for landscape display :open_mouth:

// Correct if screen is in Landscape

SensorManager.remapCoordinateSystem(rotation, SensorManager.AXIS_X, SensorManager.AXIS_Z, outRotation);



SensorManager.getOrientation(rotation, orientation);



//twisting works as long as you don’t tilt too far (possibly euler angles screw up?)

Log.d(“0”, orientation[0] + “”); //0 is for fisting (not for my game)

Log.d(“1”, orientation[1] + “”); //1 is for tilting up and down

Log.d(“2”, orientation[2] + “”); //2 is for twisting (holding controller)



main.enqueue(new Callable() {



public Object call() throws Exception {

main.getGeom().setLocalRotation(new Quaternion().fromAngleAxis(orientation[2], Vector3f.UNIT_Y));

return null;

}

});

}



}

}

[/java]

2 Likes