Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 三维场景中的平移相机_Java_Opengl_3d_Camera_Panning - Fatal编程技术网

Java 三维场景中的平移相机

Java 三维场景中的平移相机,java,opengl,3d,camera,panning,Java,Opengl,3d,Camera,Panning,我正在写一个3D应用程序,我需要用鼠标光标平移相机。与许多3D应用程序一样,u按下鼠标滚轮并移动鼠标以平移相机 我所做的是计算鼠标位置(dx,dy)的变化来确定相机应该平移多少。要决定相机应该向哪个方向移动,请通过鼠标位置从相机的视点投射光线以获得方向向量。接下来,我使用叉积计算鼠标射线的垂直矢量,以确定相机应该在哪个轴上移动,并根据dx和dy相应地移动它 问题是: 当我观察方向0、0、-1(进入屏幕)时,我可以沿X轴和Y轴正确移动相机 当我观察方向0,-1,0(进入“地面”)时,我可以沿X轴正

我正在写一个3D应用程序,我需要用鼠标光标平移相机。与许多3D应用程序一样,u按下鼠标滚轮并移动鼠标以平移相机

我所做的是计算鼠标位置(dx,dy)的变化来确定相机应该平移多少。要决定相机应该向哪个方向移动,请通过鼠标位置从相机的视点投射光线以获得方向向量。接下来,我使用叉积计算鼠标射线的垂直矢量,以确定相机应该在哪个轴上移动,并根据dx和dy相应地移动它

问题是:

当我观察方向0、0、-1(进入屏幕)时,我可以沿X轴和Y轴正确移动相机

当我观察方向0,-1,0(进入“地面”)时,我可以沿X轴正确移动相机,但它会围绕Y轴抖动。抖动,如它正确地移动到某一点,然后减速,甚至进入相反的方向

另一个问题是,由于我只有二维鼠标位置dx和dy,我如何在Z轴上移动相机

下面是我使用的代码。也许我的逻辑错了,或者我在监督什么。我对向量和矩阵不太熟悉,只是在瞎摆弄它的同时学习

    //Calculate the change in mouse position
    float dx = (e.getX() - mouseX) * 0.02f;
    float dy = (e.getY() - mouseY) * 0.02f;

    //project the 2D mouse position to the 3D world position (mouse picking, raycasting) and normalise the vector to get a directional vector
    Vector3f direction = Maths.getRay(e.getX(), e.getY(), this).normalise();

    //Calculate the vector perpendicular to the mouse ray and the "up" vector
    Vector3f perpendicular = Vector3f.cross(direction, new Vector3f(0, 1, 0).normalise(); 

    //Move the camera along the axis
    Vector3f cameraPos = camera.getPosition();
    camera.setPosition(new Vector3f(cameraPos.x + (perpendicular.x * dx) , cameraPos.y + (perpendicular.z * dy), 0));

     //Cache the mouse position for the next iteration
     mouseX = e.getX();
     mouseY = e.getY();

注意:对于Z轴移动,您通常可以使用箭头键或滚轮沿轴前后移动,具体取决于按住键的时间或滚轮每次单击的设定量。这里有完整的3d图形教程。