如何以当前设备方向启用陀螺仪摄像头 [英] How to enable gyroscope camera at current device orientation

查看:296
本文介绍了如何以当前设备方向启用陀螺仪摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用陀螺仪控制的相机的onButtonClick事件,但是我希望它从相机的当前位置开始.当前,当启用陀螺仪时,它将摄像机移动到新位置(可能是设备当前的陀螺仪旋转),而不是将其放置在原处并从该点开始陀螺仪旋转.

I would like to enable gyro controlled camera onButtonClick event but I want it to start at the camera's current position. Currently when the gyro gets enabled it moves the camera off to a new position (probably the devices current gyro rotation) rather than leaving it where it is and gyro-ing from that point.

希望我有道理,但基本上我不希望用户注意到他们在游戏中看到的任何变化(即,陀螺仪控制的摄像头,但该用户不会注意到该变化).这是我正在使用的代码:

Hope I'm making sense, but basically I don't want the user to notice any change in what they are seeing in the game (ie. camera controlled by gyro but not that user would notice that change). Here's the code I'm using:

void Update () 
{
    Quaternion attitudeFix = new Quaternion (-gyro.attitude.x, -gyro.attitude.z, -gyro.attitude.y, gyro.attitude.w);
    Quaternion offsetRotation =  initialGyroRotation * attitudeFix;
    rotation = initialRotation * offsetRotation;
    transform.rotation = rotation;
}

public void EnableGyro() 
{
    initialGyroRotation = Input.gyro.attitude;
    initialRotation = transform.rotation;

    Debug.Log("initialRotation: " + initialRotation.ToString());
    Debug.Log("transform.rotation: " + transform.rotation.ToString());
    Debug.Log("initialGyroRotation: " + initialGyroRotation.ToString());
}

**

:这是一个屏幕,确切地显示了我希望用户将设备握在脸部(人像)前方并向北行驶时视图的外观.无论应用启动时设备的方向如何,这都是手机以纵向方向向北行驶时的外观(同样,当用户通过手机查看时).

Here's a screen of exactly how I want the view to look as the user is holding their device in front of their face (portrait) AND heading north. Regardless of the orientation of the device when the app starts, this is how it should look when heading north with phone in portrait orientation (again as the user is looking through the phone).

测试变得令人困惑,因此我将代码完全返回到您的解决方案所建议的方式.仍然存在一个小问题,但看来此脚本非常接近.主要问题是当我运行每个测试时,屏幕以奇怪的角度启动设备时,屏幕看起来不像上面的图片.启动应用程序时设备实际上是什么角度都没有关系,当指向北方和纵向时,它需要看起来像上面的屏幕.

EDIT 2: Tests were getting confusing so I put the code back to exactly how your solution suggests. There is still a slight problem, but it seems like this script is very close. The main problem is the screen doesn't look like the above pic when I run each test, starting the app with the device on strange angles. It really shouldn't matter what angle the device is when the app is started, it needs to look like the above screen when pointing north and portrait.

我需要做更多的测试,并且将在一个新的/干净的项目中这样做.

I need to do more tests, and will do so with a new/clean project.

推荐答案

您需要在AwakeStart函数中获取偏移相机的位置.在Update函数中,将该偏移值应用于陀螺仪传感器的值.

You need to get the offset camera position in the Awake or Start function. In the Update function, apply that offset value to the value from the gyro sensor.

您似乎已经知道应该使用偏移量,但是您这样做的方式不正确.令人困惑的部分是获取偏移量,该偏移量需要从陀螺仪传感器值中减去当前摄像机的旋转角度.

It looks like you already know offset should be used but you are not doing that the right way. The confusing part is getting the offset which requires subtracting the current camera rotation from the gyro sensor value.

要减去Quaternion,请乘以Inverse,而不仅仅是Quaternion:

To subtract a Quaternion multiply the Inverse not just the Quaternion:

Quaternion = Quaternion * Quaternion.Inverse

要添加Quaternion,请乘以Quaternion:

Quaternion = Quaternion * Quaternion.


这是您的代码的外观:


This is what your code should look like:

Quaternion offset;

void Awake()
{
    Input.gyro.enabled = true;
}

void Start()
{
    //Subtract Quaternion
    offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
}

void Update()
{
    GyroModifyCamera();
}

void GyroModifyCamera()
{
    //Apply offset
    transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}

private static Quaternion GyroToUnity(Quaternion q)
{
    return new Quaternion(q.x, q.y, -q.z, -q.w);
}

这篇关于如何以当前设备方向启用陀螺仪摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆