Accelerometer Issues....

Put your problem here if it does not fit any of the other categories.

Accelerometer Issues....

Postby DrDanger » Tue Feb 14, 2012 4:29 pm

Hi.

I am currently playing a bit around developing some android apps. I tried to implement a real simple game. I think everybody knows that kind of game. Your Device is a board and there is a ball on top of this board. If you move your device the ball will move.
I hope you got what I mean :)

Well its working fine, but only if I have my device flat on the ground. If I lift it up and hold it in front of me, the ball rolls "down". Thats what its supposed to do.

But it would be cool if I could implement it that I can calibrate the device (while holding it in front of me) and then just move the device to control the ball. Currently you can only "play" this game while holding the device it flat on the ground. I want to be able to play it in all positions.

So here is my code so far:
Can someone give me a hint how I can realize this? Thanks in advance!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class SensorTestActivity extends Activity implements SensorEventListener{
  2.        
  3.         private SensorManager mSensorManager;
  4.  
  5.        
  6.         private Point point = new Point(0,0);
  7.          View v;
  8.          TextView accView;
  9.          TextView orientView;
  10.          
  11.          private boolean calibrated = false;
  12.          
  13.     /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.        
  18.         LinearLayout layout = new LinearLayout(this);
  19.         layout.setOrientation(LinearLayout.VERTICAL);
  20.  
  21.         accView = new TextView(this);
  22.         layout.addView(accView);
  23.        
  24.         orientView = new TextView(this);
  25.         layout.addView(orientView);
  26.        
  27.        
  28.         v = new View(this){
  29.                         @Override
  30.                         protected void onDraw(Canvas canvas) {
  31.                                 // TODO Auto-generated method stub
  32.                                 super.onDraw(canvas);
  33.                                
  34.                                 if (point.x == 0){
  35.                                         point = new Point(canvas.getWidth()/2,canvas.getHeight()/2);
  36.                                 }
  37.                                 Paint p = new Paint();
  38.                                 p.setColor(Color.RED);
  39.                                 p.setStyle(Style.FILL_AND_STROKE);
  40.                                 canvas.drawCircle(point.x, point.y, 20, p);
  41.                                
  42.                         }
  43.         };
  44.        
  45.        
  46.         layout.addView(v);
  47.         setContentView(layout);
  48.        
  49.        
  50.         mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  51.  
  52.    
  53.        
  54.         boolean b = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  55.                 SensorManager.SENSOR_DELAY_GAME);
  56.    
  57.         Log.d("TEST","listener geadded: " + b);
  58.        
  59.     }
  60.    
  61.     @Override
  62.     protected void onResume()
  63.     {
  64.         super.onResume();
  65.      
  66.         mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  67.                 SensorManager.SENSOR_DELAY_GAME);
  68.        
  69.  
  70.     }
  71.  
  72.     @Override
  73.     protected void onStop()
  74.     {
  75.         // Unregister the listener
  76.         mSensorManager.unregisterListener(this);
  77.         super.onStop();
  78.     }
  79.  
  80.         @Override
  81.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  82.                 Log.d("TEST","changed");
  83.                
  84.         }
  85.  
  86.        
  87.        
  88.         float xoffset, yoffset ,zoffset;
  89.        
  90.         @Override
  91.         public void onSensorChanged(SensorEvent event) {
  92.                
  93.                 if (v != null){
  94.                  float x = event.values[0];
  95.                  float y = event.values[1];
  96.                  float z = event.values[2];
  97.                  point.x-=x;
  98.                  point.y+=y;
  99.                  Log.d("VALUES", "x: " + x + " y:" + y + " z: " +z);
  100.                  v.invalidate();
  101.                 }
  102.         }
  103.          
  104.        
  105. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
DrDanger
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 14, 2012 4:20 pm

Top

Re: Accelerometer Issues....

Postby Phyll » Wed Feb 15, 2012 10:48 pm

Hi DrDanger,

If I'm understanding this right. You wish to play the game in a manor that defies gravity, that is, now the ball can operate in the y,z plain instead of the x,y. Now when you hold the phone in front of you when seated, the ball would be able to "roll" up and down on the phone, but would appear to operate just as it does when it is parallel to the ground. Is that the idea?

If that is indeed the case, then I guess you would have to just interchange the axis appropriately. You would swap the values of x and z coming in from the sensors and I think it would be done. You could probably even play it flat on your back looking up if you reversed the z input.

If that's not what you are after please try explaining it again.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Accelerometer Issues....

Postby DrDanger » Fri Feb 17, 2012 11:04 am

Hi Phyll.

Thanks for your answer. I had to think about that for a while. I think you got what I was looking for. But just switching the values does not seem to solve my problem.
I googled a lot more and found a very similar question at stackoverflow:
http://stackoverflow.com/questions/4340 ... in-android

Thats exactly what I want to realize. I just don't get it how to calculate with these rotation matrices. Can you (or somepone else) give me another hint?

Thanks!
DrDanger
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 14, 2012 4:20 pm

Re: Accelerometer Issues....

Postby Phyll » Fri Feb 17, 2012 1:43 pm

Hi DrDanger,

That is exactly the same thing isn't it.

Well I didn't really know there was a matrix to govern the sensor outputs. That in essence does "switch" the values if you reset the rotation matrix in different orientations. Its probably the preferred way to do it.

What they are talking about doing is holding the device in the position it will be used in and then resetting the matrix to the values you get in that position. Now the device screen in relation to the sensors is the same as it was before the "calibration" so I don't think you need to do anything special with the sensor outputs. Does that sound reaonable?

The answers in that post are beyond my ability to grasp with just a cursory reading. Matrix math not being one of my strong points anyway. But if you have any specific questions about some part of it let me know and maybe I can be of help.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests