如何使用 Android 罗盘方向来瞄准 opengl 相机?
我正在使用 libgdx 为 Android 开发一个基本的 3D 游戏,但在给定罗盘提供的三个旋转角度(方位角 - 绕 Z 旋转、滚动 - 绕 Y 旋转、俯仰 - 绕 X 旋转)的情况下,我很难正确定向相机。 。我通过以下代码取得了一些小小的成功,因为我可以按照我的预期正确地将虚拟相机瞄准 Z 轴和 X 轴。 (角度以度为单位[-180,180])
camera.direction.x = 0;
camera.direction.y = 0;
camera.direction.z = 1;
camera.up.x = -1;
camera.up.y = 0;
camera.up.z = 0;
camera.rotate(azimuth,0,0,1);
camera.rotate(roll,0,1,0);
camera.rotate(pitch,1,0,0);
我在这方面也取得了一些成功,但它没有确定相机的向上矢量方向。 (在此版本中角度已转换为弧度)
float x,y,z;
roll = (float) (roll + Math.PI/2.0);
x = (float) (Math.cos(azimuth) * Math.cos(roll));
y = (float) (Math.sin(azimuth) * Math.cos(roll));
z = (float) (Math.sin(roll));
Vector3 lookat = new Vector3(x,y,z);
camera.lookAt(lookat.x, lookat.y, lookat.z);
有人可以阐明如何从这三个角度正确定位虚拟相机吗?
另外,我试图让手机处于横向模式,使手机顶部位于左侧,底部位于右侧。因此,相机的默认方向(所有旋转均为 0,手机顶部朝北)是相机瞄准地面(正 Z),向上瞄准东(负 X)。
I'm using libgdx to develop a basic 3d game for android and I'm having difficulty properly orienting the camera given three rotation angles provided from the compass (azimuth - rotation about Z, roll - rotation about Y, pitch - rotation about X). I've had some slight success with the following code in that I can properly aim the virtual camera down the Z-axis and X-axis as I expected. (Angles are in degrees [-180,180])
camera.direction.x = 0;
camera.direction.y = 0;
camera.direction.z = 1;
camera.up.x = -1;
camera.up.y = 0;
camera.up.z = 0;
camera.rotate(azimuth,0,0,1);
camera.rotate(roll,0,1,0);
camera.rotate(pitch,1,0,0);
I've also had some success with this, but it does not orient the camera's up-vector. (Angles have been converted to radians in this version)
float x,y,z;
roll = (float) (roll + Math.PI/2.0);
x = (float) (Math.cos(azimuth) * Math.cos(roll));
y = (float) (Math.sin(azimuth) * Math.cos(roll));
z = (float) (Math.sin(roll));
Vector3 lookat = new Vector3(x,y,z);
camera.lookAt(lookat.x, lookat.y, lookat.z);
Can someone shed some light on how to properly orient the virtual camera from these three angles?
Also, I'm trying to have the phone be in landscape mode such that the top of the phone is to the left and the bottom is to the right. Hence the camera's default direction (all rotations are at 0, top of the phone is aimed north) be the camera aiming toward the ground (positive Z) with the up aiming east (negative X).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过编写一段时间的其他代码,我最终达到了尝试将渲染、模拟和输入分开的地步。因此,我提出了以下适合我的解决方案。我还没有严格测试它,但它似乎做了我想要的
(忽略相机胶卷)。在程序的 android 部分,我需要将方向设置为横向模式:
我创建了一个播放器类来存储偏航、俯仰、滚动和位置
然后当我根据输入更新播放器时,我执行以下操作:
请注意音调映射到 input.roll,而 roll 映射到 input.pitch。不知道为什么,但它对我有用。最后更新相机:
编辑:将相机胶卷添加到代码中。对于我和我的 Droid 2,滚动似乎只有 [-90,90] 范围内的值,因此如果您旋转超过 -90 或 90,则值开始变回 0。
By coding other things for a while, I eventually reached a point where I was trying to separate the rendering, simulation, and input. Because of that I have come up with the following solution that works for me. I haven't tested it rigorously, but it appears to do what I want
(ignoring camera roll).On the android part of the program, I needed to set the orientation to landscape mode:
I created a player class to store yaw, pitch, roll, and position
And then when I update the player based on input I do the following:
Note that pitch maps to input.roll and roll maps to input.pitch. Not sure why, but it works for me. Finally update the camera:
EDIT: added camera roll to the code. For me and my Droid 2, roll appears to only have values in [-90,90] such that if you rotate past -90 or 90 the values start changing back towards 0.