3d Libgdx-桌面-鼠标悬停在三维模型实例上

3d Libgdx-桌面-鼠标悬停在三维模型实例上,3d,libgdx,intersection,3d,Libgdx,Intersection,我试图计算鼠标何时悬停在Libgdx中的三维模型实例上。到目前为止,我所有的模型实例都是完美的矩形。我已经从鼠标x和y坐标创建了一个拾取光线,并使用Intersection类尝试并计算了一个交点,但似乎只有当鼠标位于屏幕中心的model instances边界框区域时,才会注册交点。实际上,模型实例位于稍微偏侧的位置 我的理论是边界框只存储模型实例的尺寸,而不存储位置。但我还没有证明这一点,有没有其他方法可以从鼠标坐标和模型实例的边界框生成的光线计算交点 这是我目前的代码 // This cla

我试图计算鼠标何时悬停在Libgdx中的三维模型实例上。到目前为止,我所有的模型实例都是完美的矩形。我已经从鼠标x和y坐标创建了一个拾取光线,并使用Intersection类尝试并计算了一个交点,但似乎只有当鼠标位于屏幕中心的model instances边界框区域时,才会注册交点。实际上,模型实例位于稍微偏侧的位置

我的理论是边界框只存储模型实例的尺寸,而不存储位置。但我还没有证明这一点,有没有其他方法可以从鼠标坐标和模型实例的边界框生成的光线计算交点

这是我目前的代码

// This class is correctly registered as an input processor
public boolean mouseMoved(int x, int y) {
    // Get the ray from the mouse's location
    Ray ray = pCam.getPickRay(x, y);

    // Get the model's bounding box
    BoundingBox box = new BoundingBox();
    box2.calculateBoundingBox(box);

    /* Partial 'box2' initialization code
        mb.node().id = "box2";
        mb.part("box", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, new Material(ColorAttribute.createDiffuse(Color.GREEN))).box(4f, 1f, 4f);
        box2 = new ModelInstance(model, "box2");
        box2.transform.setToTranslation(2, 0, -2); 
     */

    // If the ray resides in the bounding box
    if (Intersector.intersectRayBoundsFast(ray, box)) {
        // Change the model instance color to red
        box2.materials.get(0).set(ColorAttribute.createDiffuse(Color.RED));
    }
    // Otherwise
    else {
        // Set the model instance color back to green
        box2.materials.get(0).set(ColorAttribute.createDiffuse(Color.GREEN));
    }
    // Default return false
    return false;
}

如果您能提供任何可以帮助我的建议或代码,我将永远感激。另外,请告诉我是否可以添加任何内容来澄清或帮助您,因为您可以帮助我。

方法Intersector.intersectRayBoundsFast返回相对于光线原点的交点。您只需添加光线的原点即可获得交点

请看一看,我已经阅读了这篇文章,但我仍然看不到我所做的不同之处,这将提供我所看到的错误。你能就我如何编辑代码以匹配文章的代码提供一些建议吗?不,你的代码太不同了,例如,每次鼠标移动时计算边界框不是一个好主意,因为这是一个非常昂贵的方法。也许您可以编辑教程的代码以匹配您的用例?