6

我正在尝试围绕我的世界中的模型以球形运动方式移动我的相机。我已经看到将球坐标(rho,theta,phi)转换为笛卡尔坐标(x,y,z),但我不确定如何进行设置。到目前为止,这是我尝试过的,但它并没有持续围绕模型运行。它到达某个点,然后旋转似乎会自行反转。

初始化thetaphi

private float theta = 0.0f;
private float phi = 0.0f;

更新thetaphi每一帧:

// This should move the camera toward the upper-right continuously, correct?
theta = (theta+1.0f)%360;
phi = (phi+1.0f)%360;

转换thetaphi为相机的笛卡尔坐标:

camera.position.x = CAMERA_DISTANCE * (float)Math.sin(theta*MathHelper.PIOVER180) * (float)Math.cos(phi*MathHelper.PIOVER180);
camera.position.y = CAMERA_DISTANCE * (float)Math.sin(theta*MathHelper.PIOVER180) * (float)Math.sin(phi*MathHelper.PIOVER180);
camera.position.z = CAMERA_DISTANCE * (float)Math.cos(theta*MathHelper.PIOVER180);

然后更新相机查看点和视图矩阵:

camera.lookAt(0, 0, 0);
camera.update();

注意: 我在带有 libGDX 框架的 Android 上使用 Java,我正在尝试使用 2D 屏幕虚拟操纵杆来控制旋转,但我仍然需要找到一种方法将操纵杆映射到thetaphi.

任何帮助是极大的赞赏!

4

1 回答 1

5

我最近做了这样的事情。这个网站帮助我想象我需要做什么。

您需要做的是将您的本地操纵杆坐标(相对于它的中心)转换为俯仰和偏航值:

public float getPitch()
{
    return (position.X - center.X) * MathHelper.PIOVER180;
}

public float getYaw()
{
    return (position.Y - center.Y) * MathHelper.PIOVER180;
}

然后你可以使用四元数来表示它的旋转:

public void rotate(float pitch, float yaw, float roll)
{
    newQuat.setEulerAngles(-pitch, -yaw, roll);
    rotationQuat.mulLeft(newQuat);
}

然后您可以使用 libGDX 的内置rotate(quaternion)方法将四元数应用于相机的视图矩阵:

camera.view.rotate(rotationQuat);

// Remember to set the camera to "look at" your model
camera.lookAt(0, 0, 0);
于 2012-12-26T04:30:17.090 回答