【问题标题】:libGDX rotate about objectlibGDX 围绕对象旋转
【发布时间】:2014-11-09 23:57:13
【问题描述】:

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

【问题讨论】:

    标签: java 3d libgdx


    【解决方案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();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2017-02-10
      • 2014-12-28
      • 1970-01-01
      • 2015-01-06
      • 2017-07-14
      • 2013-05-21
      • 1970-01-01
      相关资源
      最近更新 更多