Libgdx:移动旋转的透视相机 [英] Libgdx: Moving a rotated perspective camera

查看:163
本文介绍了Libgdx:移动旋转的透视相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可可视化环境地图的android应用,目前我正在使用libgdx绘制地图,就像用户应该能够缩放,旋转和移动地图的任何地图应用一样,

I am developing an android app which visualize the map of an environment and currently i am using libgdx to draw the map, also like any map application the user should be capable of zoom, rotate and moving the map,

我已经开发了一个GestureHandler类,该类实现了GestureListener接口并与PerspectiveCamera进行交互(因为将来我将使用3d组件):

I have developed a GestureHandler class which implements GestureListener interface and interacts with a PerspectiveCamera(since i will use 3d components in the future):

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    float tempX = (mapView.getCamera().position.x - deltaX * 0.5f);
    float tempY = (mapView.getCamera().position.y + deltaY * 0.5f);

    mapView.getCamera().position.set(
            MathUtils.lerp(mapView.getCamera().position.x, tempX, mapView.getCamera().fieldOfView / 100),
            MathUtils.lerp(mapView.getCamera().position.y, tempY, mapView.getCamera().fieldOfView / 100),
            mapView.getCamera().position.z);
    mapView.getCamera().update();
    return false;
}


float initialDistance = 0;
float initialAngle = 0;
float distance = 0;

private void zoom(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2)
{
    initialDistance = initialPointer1.dst(initialPointer2);
    float iDeltaX = initialPointer2.x - initialPointer1.x;
    float iDeltaY = initialPointer2.y - initialPointer1.y;
    initialAngle = (float)Math.atan2((double)iDeltaY,(double)iDeltaX) * MathUtils.radiansToDegrees;
    if(initialAngle < 0)
        initialAngle = 360 - (-initialAngle);


    distance = initialPointer1.dst(pointer2);
    float deltaX = pointer2.x - initialPointer1.x;
    float deltaY = pointer2.y - initialPointer1.y;
    newAngle = (float)Math.atan2((double)deltaY,(double)deltaX) * MathUtils.radiansToDegrees;
    if(newAngle < 0)
        newAngle = 360 - (-newAngle);

    //Log.e("test", distance + " " + initialDistance);
    //Log.e("test", newAngle + " " + initialAngle);
    float ratio = initialDistance/distance;
    mapView.getCamera().fieldOfView = MathUtils.clamp(initialZoomScale * ratio, 1f, 100.0f);
    Log.e("zoom", String.valueOf(mapView.getCamera().fieldOfView));
    mapView.getCamera().update();
}


@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {

    zoom(initialPointer1, initialPointer2, pointer1, pointer2);

    float delta1X = pointer2.x - pointer1.x;
    float delta1Y = pointer2.y - pointer1.y;
    newAngle = (float)Math.atan2((double)delta1Y,(double)delta1X) * MathUtils.radiansToDegrees;
    if(newAngle < 0)
        newAngle = 360 - (-newAngle);

    System.out.println("new "+newAngle);

    if(newAngle - currentAngle >= 0.01000f)
    {
        System.out.println("Increasing");
        mapView.getCamera().rotate(0.5f,0,0,1);

    }
    else if(newAngle - currentAngle <= -0.010000f) {
        System.out.println("DEcreasing");

        mapView.getCamera().rotate(-0.5f,0,0,1);
    }
    if(Math.abs(newAngle - currentAngle) >= 0.01000f)
    {
        currentAngle = newAngle;
    }
    return true;
}

一切都很好,直到我不旋转相机为止,就像无法解决一样类似的问题旋转相机后,移动会受到旋转的影响.是否有帮助专门采样代码?

Everything is fine until as far as i don't rotate the camera, just like this unsolved similar question after rotating the camera, movements will be affected by applied rotation.Any help specially sample codes?

经过大量的努力,我终于解决了它, 正如Tenfour04在他的 answer 中所说,我必须使用两个单独的矩阵进行变换和旋转,最后设置它们相乘的结果使用以下方法查看相机矩阵:

After lots of efforts i finally solved it, As Tenfour04 said in his answer i had to use two separate matrices for transformation and rotations, and finally set the result of their multiplication to view matrix of camera using:

camera.view.set(position).mul(orientation);

最重要的是将我的批处理的转换矩阵设置为camera.view:

Also the most important thing is to set the Transformation Matrix of my batch to camera.view:

batch.setTransformationMatrix(camera.view)

推荐答案

不是将手势直接应用于相机,而是将它们应用于一对Matrix4,分别用于存储方向和位置.然后在render方法中,将两个矩阵相乘并将它们应用于相机的视图.

Instead of applying the gestures directly to the camera, apply them to a pair of Matrix4's that you use to store the orientation and position separately. Then in the render method, multiply the two matrices and apply them to your camera's view.

render()方法中:

camera.view.set(orientation).mul(position); //Might need to swap orientation/position--don't remember. 
camera.update();

您的缩放方法很好,因为视野会影响相机的投影矩阵而不是其视野矩阵.

Your zoom method is fine because field of view affects the camera's projection matrix rather than its view matrix.

这篇关于Libgdx:移动旋转的透视相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆