【发布时间】:2014-11-09 23:57:13
【问题描述】:
在我的 3d 应用程序中,我希望有一个对象(例如一棵树)和我的相机来观察这个对象。然后,我希望相机围绕对象旋转一圈,同时一直看着树。想象一下绕着一棵树走,同时不断改变你的角度,这样你仍然在看它。我知道这需要我的相机旋转和我的相机的平移,但数学远远超出了我迄今为止在学校所教的水平。谁能指出我正确的方向?
【问题讨论】:
在我的 3d 应用程序中,我希望有一个对象(例如一棵树)和我的相机来观察这个对象。然后,我希望相机围绕对象旋转一圈,同时一直看着树。想象一下绕着一棵树走,同时不断改变你的角度,这样你仍然在看它。我知道这需要我的相机旋转和我的相机的平移,但数学远远超出了我迄今为止在学校所教的水平。谁能指出我正确的方向?
【问题讨论】:
这是一种非常简单的数学方法。首先,您需要一个常数来表示相机与树中心的距离(它所经过的圆形路径的半径)。此外,您需要一些变量来跟踪它围绕圆的角度。
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();
}
【讨论】: