Android 方向作为转向 - 滚动破坏俯仰?
我正在开发一个项目,其中包含一个 Android 应用程序,用于 控制/转向。
- 速度:当您向前/向后倾斜手机(倾斜)时,它会模拟产生气体和破裂。
- 方向:当您向左/右倾斜手机(滚动)时,它会模拟向左和向右转向。
我已经编写了一些看起来运行良好的代码。但当我仔细观察时,我发现有些值表现得很奇怪。
当我向前/向后倾斜手机来处理速度时,它工作得很好,我得到了预期的速度和方向值。但是,当我向左/右倾斜手机来处理方向时,它似乎会破坏一些值。当它向左/右倾斜时,不仅会改变方向值(滚动),还会影响速度值(俯仰)。
有关额外信息:
- Android 2.2 编程 设备
- 是 Google Nexus One
- 纵向握住设备
我用来读取传感器值的最相关代码如下:
public void onSensorChanged(SensorEvent sensorEvent)
{
synchronized (this)
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
float azimuth = sensorEvent.values[0]; // azimuth rotation around the z-axis
float pitch = sensorEvent.values[1]; // pitch rotation around the x-axis
float roll = sensorEvent.values[2]; // roll rotation around the y-axis
System.out.println("pitch: " + pitch);
System.out.println("roll: " + roll);
System.out.println("--------------------");
// Convert the sensor values to the actual speed and direction values
float speed = (pitch * 2.222f) + 200;
float direction = roll * -2.222f;
因此,当我运行代码时,我会查看打印的值。当向左/右倾斜设备时,似乎也会影响俯仰值。怎么会?当“滚动”时,如何获得纯音高值?因此,向左/向右倾斜手机不会影响/破坏音高值。
I am working on a project which includes an Android application which is used for
controlling/steering.
- Speed: When you tilt the phone forward/backwards (pitch) it simulates giving gas and breaking.
- Direction: When you tilt the phone left/right (roll) it simulates steering to the left and right.
I have already written some code which seemed to work fine. But when I took a closer look, I found that some values are acting weird.
When I tilt the phone forward/backward to handle the speed it works perfect I get the expected speed and direction values. But when I tilt the phone to the left/right to handle the direction it seems to corrupt some values. When it is tilting to the left/right that doesn't only change the direction value (roll) but it also affects the speed value (pitch).
For extra information:
- Programming for Android 2.2
- Device is an Google Nexus One
- Holding the device in portrait
The most relevant code I use to read the sensor values is as follows:
public void onSensorChanged(SensorEvent sensorEvent)
{
synchronized (this)
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
float azimuth = sensorEvent.values[0]; // azimuth rotation around the z-axis
float pitch = sensorEvent.values[1]; // pitch rotation around the x-axis
float roll = sensorEvent.values[2]; // roll rotation around the y-axis
System.out.println("pitch: " + pitch);
System.out.println("roll: " + roll);
System.out.println("--------------------");
// Convert the sensor values to the actual speed and direction values
float speed = (pitch * 2.222f) + 200;
float direction = roll * -2.222f;
So when I run the code, and I look at the printed values. When tilting the device left/right, it seems to affect the pitch value as well. How come? And how can I get the pure pitch value, when 'roll'-ing? So that tilting the phone to the left/right doesn't affect/corrupt the pitch value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以阅读万向节锁。以前也被我咬过。
You could read up on Gimbal lock. That's bitten me before.