围绕球体移动摄像机

6
我正在尝试围绕我的世界中的模型以球形运动移动我的相机。我看到了将球坐标(rho,theta,phi)转换为笛卡尔坐标(x,y,z),但我不确定如何设置这个。这是我迄今为止尝试过的,但它没有持续地绕着模型旋转。它到达某个点后,旋转似乎会反转。
初始化theta和phi:
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的方法。

非常感谢任何帮助!

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接