Get angle of elevation?

前端 未结 2 1886
面向向阳花
面向向阳花 2021-02-03 16:26

From the accelerometer, is it possible to get the angle of elevation? For those of you who don\'t know, the angle of elevation is:

2条回答
  •  别跟我提以往
    2021-02-03 16:59

    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

    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.

提交回复
热议问题