Search code examples
androidheightmeasurementaccelerator

S.M.T.H. - height measurement (Android)


A while back a funny app called Send Me To Heaven were all over the news etc. It's objective is to throw your phone as high as possible and catch it, the app measures the thrown height. My question is how that might be coded in android? I presume the accelerator sensor is somehow involved? Some ideas would be great :) PS: I'm asking just for curiosity and of course for engendering sake - maybe, sometime, it could be handy to know how to implement something like this.


Solution

  • There is an interface SensorEventListener provided in android. This listener provides methods in which we can catch event from any sensor. The game you are talking about might have use this interface, got the readings when sensor was accelerometer as shown below and then displayed the max reading as score.

    @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
                return;
    
            switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
                break;
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
                break;
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
                break;
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
                break;
            }
            mSensorZ = event.values[2];
            mSensorTimeStamp = event.timestamp;
        }