Android Sensors for OpenGL

前端 未结 2 553
不思量自难忘°
不思量自难忘° 2021-01-06 05:11

I want to get the android sensors to work with opengl to rotate the opengl\'s camera to wherever the phone is pointing to.

To elaborate: if the player is looking at

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-06 05:54

    You're supplying null for the inclination matrix - that's not correct.

    SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomag);
    

    There are lots of examples on how to use the SensorManager's getRotationMatrix(...).

    float[] R = new float[16];
    float[] I = new float[16];
    
    if (SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticValues)) {
        float[] anglesInRadians = new float[3];
        SensorManager.getOrientation(R, anglesInRadians);
        ...
    }
    

    Also "rotate the opengl's camera to wherever the phone is pointing to" is rather ambiguous. For instance, if you meant some sort of a augmented reality approach, then you should be mapping AXIS_X to AXIS_Z. Note that remapping might not even be necessary, e.g. when you already fix your activity to landscape mode. More details here.

    Some example codes involving sensor data and OpenGL ES:

    • CompassActivity
    • How to use onSensorChanged sensor data in combination with OpenGL

提交回复
热议问题