如何在libGdx

时间:2015-05-26 00:13:26

标签: java 3d libgdx

我正在尝试创建一个类似于我的世界的3D世界,其中玩家可以360度观看,如果他试图点击一个点(3D世界中的X,Y,Z坐标),则绘制的模型被删除了。我对在LibGdx中编程3D世界非常陌生,所以任何帮助都是有用的。我用相机旋转相机:

float deltaX = -Gdx.input.getDeltaX() * player.degreesPerPixel;
float deltaY = -Gdx.input.getDeltaY() * player.degreesPerPixel;
    if(deltaX>0)
        player.camera.rotate(Vector3.Z, (float)1.5);
    else if(deltaX<0)
        player.camera.rotate(Vector3.Z, (float)-1.5);
player.tmp.set(player.camera.direction).crs(player.camera.up).nor();
player.camera.direction.rotate(player.tmp, deltaY);
player.setDir(player.camera.direction.x, player.camera.direction.y);

谢谢

1 个答案:

答案 0 :(得分:0)

在2D环境中,您通常只需使用Camera.unproject(...),它会占用一些屏幕空间点并将其转换回游戏世界坐标。

在3D中它并不那么容易,因为额外的尺寸为您的世界增添了一些深度。这就是为什么点击2D平面(你的屏幕)可以在3D世界中基本上无数点。在libgdx中,可能被点击的点的 ray 称为pick-ray。

如果你想在某个平面上的交叉点,代码可能看起来像他的:

public void hitSomething(Vector2 screenCoords) {
    // If you are only using a camera
    Ray pickRay = camera.getPickRay(screenCoords.x, screenCoords.y);
    // If your camera is managed by a viewport
    Ray pickRay = viewport.getPickRay(screenCoords.x, screenCoords.y);

    // we want to check a collision only on a certain plane, in this case the X/Z plane
    Plane plane = new Plane(new Vector3(0, 1, 0), Vector3.Zero);
    Vector3 intersection = new Vector3();
    if (Intersector.intersectRayPlane(pickRay, plane, intersection)) {
        // The ray has hit the plane, intersection is the point it hit
    } else {
        // Not hit
    }
}

在您的情况下,当您拥有类似Minecraft的世界时,您的代码可能如下所示:

public void hitSomething(Vector2 screenCoords) {
    Ray pickRay = ...;

    // A bounding box for each of your minecraft blocks
    BoundingBox boundingBox = new BoundingBox();
    Vector3 intersection = tmp;
    if (Intersector.intersectRayBounds(pickRay, boundingBox, intersection)) {
        // The ray has hit the box, intersection is the point it hit
    } else {
        // Not hit
    }
}

请注意,在一个我的世界中,有数千个这样的街区。采用这种简单的方法不会很快。您可能必须最终得到一个分层解决方案,首先检查大块(包含许多块的边界框)以获得可能的命中,然后开始检查各个块。