Android 方向传感器用于旋转 3D 立方体

发布于 2024-10-28 02:51:05 字数 325 浏览 1 评论 0原文

我正在尝试使用 Android 手机制作 3 自由度控制器,类似于 Wiimote。使用加速度计来识别控制器的方向(使用 getOrientation() 方法进行计算)

我正在通过使用这些值来旋转 PC 中 opengl 绘制的立方体来测试方向值。问题是,它似乎不起作用。如果手机旋转超过特定的旋转,立方体就会旋转到某个奇怪的方向。

在没有计算机图形学知识的情况下,我发现参考文献说在欧拉旋转中,3D对象的最终图形取决于每个轴上的旋转顺序。和问题有关系吗??如果是这样,正确的顺序是什么?当前的顺序是“偏航->俯仰->滚动”

我不认为这是因为所谓的校准问题,因为值变化很大。

I'm trying to make 3-dof controller using Android phone, similar to Wiimote. Uses Accelerometer for recognizing the controller's orientation (used getOrientation() method for calculation)

I'm testing the orientation values by using those values to rotate the cube drawn by opengl in PC. The problem is, it doesn't seem working. If the phone is rotated over the specific rotation, the cube is rotated to some weird direction.

Without knowledge of computer graphics, I found the reference saying that in Euler rotation, the final figure of 3D object depends on the order of rotation on each axes. Is it related to the problem?? If so, what is the correct order? Current order is "yaw->pitch->roll"

I don't think it's because of the so-called calibration issue, as the value changes are significant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-11-04 02:51:05

方向传感器已弃用。获取可靠传感器值的最佳方法是使用旋转矢量传感器。它是一种基于软件的传感器,从基于加速度计和磁力计硬件的传感器中获取数据。

旋转矢量将设备的方向表示为角度和轴的组合,其中设备绕轴(x、y 或 z)旋转了角度 θ。以下代码向您展示如何获取默认旋转矢量传感器的实例。请参阅 Android 开发网站中的有关此传感器的信息

这是如何使用旋转向量获取可靠值的示例:

public void onSensorChanged(SensorEvent event) {
       if(sensor.getType()==Sensor.TYPE_ROTATION_VECTOR){
           float[] rotationMatrix = new float[16];
           SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
           SensorManager.getOrientation(rotationMatrix, mOrientValues);
           for(int i=0; i<3; i++)
              mOrientValues[i]=(float)
                  Math.toDegrees(mOrientValues[i])+180.0f;//orientation in degrees
}

The Orientation sensor is deprecated. The best way to acquire reliable sensor values is using the rotation vector sensor. It is a software-based sensor that derives the data from the accelerometer and magnetometer hardware-based sensors.

The rotation vector represents the orientation of the device as a combination of an angle and an axis, in which the device has rotated through an angle θ around an axis (x, y, or z). The following code shows you how to get an instance of the default rotation vector sensor. See the info about this sensor in the Android Dev Site.

This is an example of how to use the rotation vector to get reliable values:

public void onSensorChanged(SensorEvent event) {
       if(sensor.getType()==Sensor.TYPE_ROTATION_VECTOR){
           float[] rotationMatrix = new float[16];
           SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
           SensorManager.getOrientation(rotationMatrix, mOrientValues);
           for(int i=0; i<3; i++)
              mOrientValues[i]=(float)
                  Math.toDegrees(mOrientValues[i])+180.0f;//orientation in degrees
}
明媚殇 2024-11-04 02:51:05

当手机接近垂直位置并且没有校准帮助时,我遇到了类似的问题 getOrientation() 返回奇怪的结果。

简单的解决方案是使用这个传感器:

manager = (SensorManager) context.getApplicationContext()
                                 .getSystemService(Context.SENSOR_SERVICE);
Sensor orientationSensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);`

显然这个传感器在较新的平台中已被弃用,但无论如何它都可以正常工作。

I had similar problem with getOrientation() returning weird results when phone approaches vertical position and no calibration helps.

The easy solution is to use this sensor:

manager = (SensorManager) context.getApplicationContext()
                                 .getSystemService(Context.SENSOR_SERVICE);
Sensor orientationSensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);`

Apparently this sensor is deprecated in newer platforms but it works just fine anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文