Get angle of elevation?

痞子三分冷 提交于 2019-12-02 21:29:54

It is not really possible to get that "elevation angle" to the line of sight since you have no idea where the user is...

What you can do though is assume that the user orients the screen of the device straight towards his eyes.

With that assumption, you can use http://developer.android.com/reference/android/hardware/SensorManager.html#getRotationMatrix(float[], float[], float[], float[]) to get the orientation of the device and get the angle between the y axis and the (x,z) plane.

What it does is that it uses the accelerometer on the 3 axis to see what is the direction of the gravitation force.

This will work only if the device is stationary. If you need to handle it better with motion, you should use the gyroscope if it exists in the device.

Another thing is that depending on your application, you might want to look at some Augmented Reality frameworks if that's the kind of application you are looking into.

Edit : Here is some code I put together. The main function in my code is :

public void onSensorChanged(SensorEvent event) {
    int sensor = event.type;
    float[] values = event.values;
    int i;
    StringBuffer str=new StringBuffer();
    // do something with the sensor data
    TextView text = (TextView) findViewById(R.id.my_text);
    float[] R = new float[9]; // rotation matrix
    float[] magnetic = new float[3];
    float[] orientation = new float[3];

    magnetic[0]=0;
    magnetic[1]=1;
    magnetic[2]=0;

    str.append("From Sensor :\n");
    for(i=0;i<values.length; i++) {
        str.append(values[i]);
        str.append(", ");
    }

    SensorManager.getRotationMatrix(R, null, values, magnetic);
    SensorManager.getOrientation(R, orientation);


    str.append("\n\nGives :\n");
    for(i=0;i<orientation.length; i++) {
        str.append(orientation[i]);
        str.append(", ");
    }
    text.setText(str);
}

I did not try on a real device, only on the emulator and using the SensorSimulator.

If you want the whole source package with the couple of tools you would need for the sensorsimulator, email me, I have that all packaged.

Instead of making up the magnetic data, you can actually get those from the compass. What you are looking for is the pitch, that's in orientation[1], it varies between -pi/2 and pi/2.

Hope that helps.

When measuring movement there is generally no concept of orientation. A multi-axis accelerometer measuring static acceleration (acceleration due to gravity) could be used to calculate the angle relative the earth.

Most androids phones comes with a number of sensors such as gyroscope etc which can interfaced. See the javadocs for SensorManager and the various types of SensorEvents.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!