我正在使用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).
解决方法:
通过一段时间的编码,我最终达到了试图分离渲染,模拟和输入的地步.因此,我想出了以下适用于我的解决方案.我没有对其进行严格的测试,但是它似乎可以满足我的要求(忽略相机胶卷).
在程序的android部分,我需要将方向设置为横向模式:
<activity android:name=".MySuperAwesomeApplication"
android:label="@string/app_name"
android:screenOrientation="landscape">
>
我创建了一个播放器类来存储偏航,俯仰,滚动和位置
public class Player {
public final Vector3 position = new Vector3(0,1.5f,0);
/** Angle left or right of the vertical */
public float yaw = 0.0f;
/** Angle above or below the horizon */
public float pitch = 0.0f;
/** Angle about the direction as defined by yaw and pitch */
public float roll = 0.0f;
}
然后,当我根据输入更新播放器时,请执行以下操作:
player.yaw = -Gdx.input.getAzimuth();
player.pitch = -Gdx.input.getRoll()-90;
player.roll = -Gdx.input.getPitch();
请注意,音高映射到input.roll,而滚动映射到input.pitch.不知道为什么,但是对我有用.最后更新相机:
camera.direction.x = 0;
camera.direction.y = 0;
camera.direction.z = 1;
camera.up.x = 0;
camera.up.y = 1;
camera.up.z = 0;
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 0;
camera.update();
// The world up vector is <0,1,0>
camera.rotate(player.yaw,0,1,0);
Vector3 pivot = camera.direction.cpy().crs(camera.up);
camera.rotate(player.pitch, pivot.x,pivot.y,pivot.z);
camera.rotate(player.roll, camera.direction.x, camera.direction.y, camera.direction.z);
camera.translate(player.position.x, player.position.y, player.position.z);
camera.update();
编辑:将相机胶卷添加到代码中.对于我和我的Droid 2,roll似乎仅具有[-90,90]值,因此,如果旋转超过-90或90,则值开始变回0.