本文介绍了你如何使用Android中的加速度值特定轴计算旋转速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我开发一个简单的游戏,涉及一个字符只沿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的软件衍生和的。

这篇关于你如何使用Android中的加速度值特定轴计算旋转速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:38