libGDX围绕对象旋转

时间:2014-11-09 23:57:13

标签: java 3d libgdx

在我的3d应用程序中,我希望有一个对象(例如一棵树)和我的相机来查看这个对象。然后,我希望相机围绕物体旋转一圈,同时一直看着树。想象一下,在树周围走动,同时不断改变你的角度,这样你仍然可以看着它。我知道这需要我的相机旋转和我的相机的翻译,但数学远远超出了我在学校教育的水平。谁能指出我正确的方向? rotationexample

1 个答案:

答案 0 :(得分:1)

这是一种非常简单的数学方法。首先,你需要一个常数来确定相机离树中心的距离(它所经过的圆形路径的半径)。此外,您需要一些变量来跟踪它围绕圆圈的角度。

static final float CAM_PATH_RADIUS = 5f;
static final float CAM_HEIGHT = 2f;
float camPathAngle = 0; 

现在,您可以将camPathAngle更改为0到360度的任何内容。 0度对应于圆上与树中心的世界X轴方向相同的位置。

在每个帧上,更新camPathAngle后,您可以执行此操作以更新相机位置。

void updateTreeCamera(){
    Vector3 camPosition = camera.getPosition();
    camPosition.set(CAM_PATH_RADIUS, CAM_HEIGHT, 0); //Move camera to default location on circle centered at origin
    camPosition.rotate(Vector3.Y, camPathAngle); //Rotate the position to the angle you want. Rotating this vector about the Y axis is like walking along the circle in a counter-clockwise direction.
    camPosition.add(treeCenterPosition); //translate the circle from origin to tree center
    camera.up.set(Vector3.Y); //Make sure camera is still upright, in case a previous calculation caused it to roll or pitch
    camera.lookAt(treeCenterPosition);
    camera.update(); //Register the changes to the camera position and direction
}

为了评论它,我这样做了。如果你链接命令,它实际上比上面更短:

void updateTreeCamera(){
    camera.getPosition().set(CAM_PATH_RADIUS, CAM_HEIGHT, 0)
        .rotate(Vector3.Y, camPathAngle).add(treeCenterPosition);
    camera.up.set(Vector3.Y); 
    camera.lookAt(treeCenterPosition);
    camera.update();
}