你如何使用Android中的加速度值特定轴计算旋转速度加速度、如何使用、速度、Android

2023-09-06 08:45:14 作者:不忘初心、方得始终↖

我开发一个简单的游戏,涉及一个字符只沿Y轴移动的上下。

I am developing a simple game which involves a character moving up and down only along the Y axis.

目前我使用加速度计读数改变字符的Y速度。游戏工作正常,但最大的问题是,你必须保持设备水平,以便正确地玩游戏。

Currently I am using the accelerometer readings to change the Y velocity of the character. The game works fine but the biggest problem is that you have to keep the device horizontal in order to play the game properly.

我真正想要的是改变人物Ÿ速度只有当旋转沿Y轴的利率调整。我需要能够变化的这个速率翻译成字符的Y速度。以这种方式,都不会有问题的装置被多少倾斜,并且用户可以在正常保持装置玩游戏。

What I really want is to change the characters Y velocity only when there is a change in the rate of rotation along the Y axis. I need to be able to translate this rate of change into the Y velocity of the character. In this way it will not matter how much the device is tilted and the user can play the game while holding the device normally.

由于加速度计是必须在每一个设备,因此旧设备可以运行我的比赛我希望能够通过计算从加速度计检索的数据变化的这个速度。

Since the accelerometer is mandatory in every device, therefore older devices can run my game I want to be able to calculate this rate of change using the data retrieved from the accelerometer.

我发现这个链接这也解释了如何从加速度计数据得到俯仰和横滚。我用确切的code和与此来了,

I found this link which explains how to get pitch and roll from accelerometer data. I used the exact code and came up with this,

final double alpha = 0.5;
    double fXg = 0;
    double fYg = 0;
    double fZg = 0;
    fXg = game.getInput().getAccelX() * alpha + (fXg * (1.0 - alpha));
    fYg = game.getInput().getAccelY() * alpha + (fYg * (1.0 - alpha));
    fZg = game.getInput().getAccelZ() * alpha + (fZg * (1.0 - alpha));
    double pitch = (Math.atan2(-fYg, fZg) * 180.0) / Math.PI;
    double roll = (Math.atan2(fXg, Math.sqrt(fYg * fYg + fZg * fZg)) * 180.0) / Math.PI;
    pitch = (pitch >= 0) ? (180 - pitch) : (-pitch - 180);

通过这块code的我无法掌握如何计算的变化率。

With this piece of code I am unable grasp how to calculate the RATE of change.

我在正确的方向前进这个或这与我想要的东西完全不同?

Am I going in the right direction with this or is this completely different from what I want?

也是它更好的,如果我只是用,而不是依靠加速度计,陀螺仪?

Also is it better if I just use the gyroscope instead of relying on the accelerometer?

感谢名单提前。

推荐答案

有两部分这个答案。 (1)读什么传感器数据。 (2)如何获得你想要的运动变化的信息。

There are two parts to this answer. (1) What sensor data to read. (2) How to get the motion change info that you want.

(1)什么传感器数据读取

您会从Android的软件衍生运动传感器(比仅从加速度计),因为它们结合信号形成的加速度计,磁力计,和(如果可用)陀螺仪。运动传感器也可以正确已知偏见和使用卡尔曼滤波器来消除噪声。陀螺仪更精确,并在检测比其它两个传感器的旋转变化更快,但它们只检测的变化大于某个阈值更快。它们不检测绝对位置或取向。

You'll get much better data from Android's software-derived Motion Sensors (than from just the accelerometers) since they combine signals form the accelerometers, magnetometers, and (if available) gyroscopes. The motion sensors might also correct for known biases and use a Kalman filter to remove noise. Gyros are more accurate and faster at detecting rotational changes than the other two sensors, but they only detect changes faster than some threshold. They don't detect absolute position or orientation.

在旋转传感器是Android的有关设备的旋转最好的信息软件衍生的指示。我的记忆是它包括如果可用的陀螺仪的信号,否则它只是使用的加速​​度计(测量线性力包括重力)和磁强计(测量地球磁场内的方向)。

The "rotation sensor" is a software-derived indication of Android's best information about the device's rotation. My recollection is that it includes the gyroscope signals if available, else it just uses the accelerometers (to measure linear forces including gravity) and magnetometers (to measure the orientation within the earth's magnetic field).

警告:本款运动传感器指南中说,一些棘手的:

Caveat: This paragraph in the Motion Sensors Guide says something tricky:

在Android开源项目(AOSP)提供了三种基于软件  运动传感器:重力传感器,线性加速度传感器,和一个  旋转矢量传感器。这些传感器在Android 4.0的更新和  现在使用的装置的陀螺仪(除了其他传感器),以改善  稳定性和性能。 ......所有这三种传感器的依靠  陀螺仪:如果一个装置不具有一个陀螺仪,这些传感器不  显示出来,并没有可供使用。

The Android Open Source Project (AOSP) provides three software-based motion sensors: a gravity sensor, a linear acceleration sensor, and a rotation vector sensor. These sensors were updated in Android 4.0 and now use a device's gyroscope (in addition to other sensors) to improve stability and performance. ... All three of these sensors rely on a gyroscope: if a device does not have a gyroscope, these sensors do not show up and are not available for use.

我的认为的这意味着,在Android 4.0及更高版本,这三个基于软件的传感器只在有陀螺仪,是虽然他们总是在早期版本的Andr​​oid的可用设备可用。做各种设备上一些快速测试,以找出,并请报告回到这里。

I think that means that on Android 4.0 and later, these three software-based sensors are only available on devices that have gyros, while they're always available on earlier versions of Android. Do some quick tests on various devices to find out, and please report back here.

注意:它帮​​助的校准磁强计的在一个大的数字8字形状移动设备几个周期执行此操作。磁力计硬件将检测这一点,跟踪它的措施,并调整其校准参数。值的范围是什么

Note: It helps to calibrate the magnetometers. Do this by moving the device in a large figure-8 shape for several cycles. The magnetometer hardware will detect this, track what range of values that it measures, then adjust its calibration parameters.

(2)如何获得运动变化的信息,你想

要控制自己的游戏角色,可以阅读的旋转矢量传感器。绕X轴减去初始X轴的旋转的旋转应表明多少设备已经倾斜向上或向下相对于时捕获的初始旋转值

To control your game character, consider reading the Rotation Vector Sensor. The rotation about the X-axis minus the initial X-axis rotation should indicate how much the device has been tilted up or down relative to when you captured the initial rotation value.

或者你可以使用加速度传感器来衡量有多少用户移动设备上下沿设备的Y轴。 Y轴指向到屏幕的顶部。在本指南页展示了如何使用高通滤波器减去重力的作用。

Or you can use Acceleration Sensor to gauge how much the user has moved the device up and down along the device's Y-axis. The Y-axis points up to the top of the screen. The code sample on this Guide page shows how to use a high-pass filter to subtract out the force of gravity.

我真正想要的是,只有当改变字符ÿ速度  有在沿Y轴的旋转速度的变化。

What I really want is to change the characters Y velocity only when there is a change in the rate of rotation along the Y axis.

这将测量的速度有多快一(没多少)倾斜手机左,右(不是上下)。如果您需要在价值变动的数学的背景,看到汗学院上的衍生物短期讲学的[微积分,没有资金]和积分的。维基百科也应该是有帮助的。总之,通过减去参考值计算的值的变化,计算变化率(例如,计算从速度加速度)减去以规则的时间间隔采样的previous值,并计算从加速度的积分(例如,计算速度)由采样值相加。

That would measure how fast (not how much) one tilts the phone left and right (not up and down). If you need some background on the mathematics of change in a value, see the Khan Academy for short lectures on derivatives [in calculus, not finance] and integrals. Wikipedia should also be helpful. In short, compute a value's change by subtracting its reference value, compute the rate of change (e.g. compute acceleration from velocity) by subtracting the previous value sampled at a regular time interval, and compute the integral (e.g. compute velocity from acceleration) by summing the sampled values.

又见传感器API参考和的传感器事件的参考。

 
精彩推荐
图片推荐