AR app using the compass to rotate the camera problem
in
Android Processing
•
2 years ago
Hi guys!
Any ideas anyone? Has anybody tried this? Maybe to use some other library?
The code I've been tweaking:
- // android_sensorData
- // Eric Pavey - 2010-10-10
- // http://www.akeric.com
- //
- // Query the phone's accelerometer and magnetic field data, display on screen.
- // Made with Android 2.1, Processing 1.2
-
- //-----------------------------------------------------------------------------------------
- // Imports required for sensor usage:
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorManager;
- import android.hardware.SensorEventListener;
- import android.os.Bundle;
- import android.view.WindowManager;
-
- //-----------------------------------------------------------------------------------------
- // Screen Data:
- float sw, sh;
- // Font Data:
- String[] fontList;
- PFont androidFont;
-
- // Setup variables for the SensorManager, the SensorEventListeners,
- // the Sensors, and the arrays to hold the resultant sensor values:
- SensorManager mSensorManager;
- MySensorEventListener accSensorEventListener;
- MySensorEventListener magSensorEventListener;
- Sensor acc_sensor;
- float[] acc_values;
- Sensor mag_sensor;
- float[] mag_values;
- float SmoothingConst = 0.05f;
-
- //-----------------------------------------------------------------------------------------
-
- void setup() {
- size(screenWidth, screenHeight, P3D);
- // Set this so the sketch won't reset as the phone is rotated:
- orientation(PORTRAIT);
- // Setup Fonts:
- fontList = PFont.list();
- androidFont = createFont(fontList[0], 16, true);
- textFont(androidFont);
- }
-
- //-----------------------------------------------------------------------------------------
-
- void draw() {
- camera(0, 0, 0, mag_values[0], mag_values[1], 0, 0.0, 1.0, 0.0);
- background(196, 236, 255);
- lights();
-
- beginShape();
- vertex(-400, 400,-100);
- vertex(-400, -400, -100);
- vertex(400,-400, -100);
- vertex(400,400,-100);
- endShape(CLOSE);
-
- }
-
- //-----------------------------------------------------------------------------------------
- // Override the parent (super) Activity class:
- // States onCreate(), onStart(), and onStop() aren't called by the sketch. Processing is entered at
- // the 'onResume()' state, and exits at the 'onPause()' state, so just override them:
-
- void onResume() {
- super.onResume();
- println("RESUMED! (Sketch Entered...)");
- // Build our SensorManager:
- mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
- // Build a SensorEventListener for each type of sensor:
- magSensorEventListener = new MySensorEventListener();
- accSensorEventListener = new MySensorEventListener();
- // Get each of our Sensors:
- acc_sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- mag_sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
- // Register the SensorEventListeners with their Sensor, and their SensorManager:
- mSensorManager.registerListener(accSensorEventListener, acc_sensor, SensorManager.SENSOR_DELAY_GAME);
- mSensorManager.registerListener(magSensorEventListener, mag_sensor, SensorManager.SENSOR_DELAY_GAME);
- }
-
- void onPause() {
- // Unregister all of our SensorEventListeners upon exit:
- mSensorManager.unregisterListener(accSensorEventListener);
- mSensorManager.unregisterListener(magSensorEventListener);
- println("PAUSED! (Sketch Exited...)");
- super.onPause();
- }
-
- //-----------------------------------------------------------------------------------------
-
- // Setup our SensorEventListener
- class MySensorEventListener implements SensorEventListener {
- void onSensorChanged(SensorEvent event) {
- int eventType = event.sensor.getType();
- if (eventType == Sensor.TYPE_ACCELEROMETER) {
- acc_values = LowPass( event.values, acc_values );
- }
- else if (eventType == Sensor.TYPE_MAGNETIC_FIELD) {
- mag_values = event.values;
- }
- }
- void onAccuracyChanged(Sensor sensor, int accuracy) {
- // do nuthin'...
- }
- }
-
- float[] LowPass (float[] input, float[] output) {
- if (output == null) return input;
-
- for ( int i=0; i<input.length; i++ ) {
- output[i] = output[i] + SmoothingConst * (input[i] - output[i]);
- }
- return output;
- }
-
- void onCreate(Bundle bundle) {
- super.onCreate(bundle);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
1