Java libGDX围绕对象旋转
在我的3d应用程序中,我希望有一个对象(例如一棵树),我的相机可以看到这个对象。然后,我想让相机绕着物体旋转一圈,同时一直看着树。想象一下绕着一棵树走,同时不断改变你的角度,这样你仍然在看着它。我知道这需要我的相机旋转和平移,但数学远远超出了我在学校里所学的水平。谁能给我指出正确的方向吗?Java libGDX围绕对象旋转,java,3d,libgdx,Java,3d,Libgdx,在我的3d应用程序中,我希望有一个对象(例如一棵树),我的相机可以看到这个对象。然后,我想让相机绕着物体旋转一圈,同时一直看着树。想象一下绕着一棵树走,同时不断改变你的角度,这样你仍然在看着它。我知道这需要我的相机旋转和平移,但数学远远超出了我在学校里所学的水平。谁能给我指出正确的方向吗? 这里有一种简单的数学方法。首先,您需要一个恒定的距离相机是从树的中心(半径的圆路径上旅行)。此外,还需要一些变量来跟踪它围绕圆的角度 static final float CAM_PATH_RADIUS =
这里有一种简单的数学方法。首先,您需要一个恒定的距离相机是从树的中心(半径的圆路径上旅行)。此外,还需要一些变量来跟踪它围绕圆的角度
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();
}