在球体周围移动相机

时间:2012-12-25 23:50:19

标签: java android opengl-es 3d libgdx

我正试图在我的世界中围绕模型以球形运动移动相机。我已经看到将球面坐标(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();

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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:5)

我最近做了这样的事情。 This website帮助我了解了我需要做的事情。

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

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);
相关问题