【问题标题】:Libgdx - Desktop - Mouse hovering over a 3D Model InstanceLibgdx - 桌面 - 鼠标悬停在 3D 模型实例上
【发布时间】:2014-10-19 22:46:09
【问题描述】:

我正在尝试计算鼠标悬停在 Libgdx 中的 3D 模型实例上的时间。截至目前,我所有的模型实例都是完美的矩形。我已经从鼠标 x 和 y 坐标创建了一条拾取射线,并使用 Intersection 类来尝试计算交点,但似乎只有当鼠标位于模型实例边界框的区域中时,交点才会注册屏幕的中心。实际上,模型实例的位置有些偏向一边。

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

这是我当前的代码。

// 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;
}

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

【问题讨论】:

  • 我已经阅读了这篇文章,但我仍然没有看到我正在做的不同的事情会提供我所看到的错误。您能否就如何编辑我的代码以匹配文章的代码提供任何建议?
  • 不,您的代码太不同了(例如,在每次鼠标移动时计算边界框并不是一个好主意,因为这是一种非常昂贵的方法)。也许您可以编辑教程的代码以匹配您的用例?

标签: 3d libgdx intersection


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2011-08-04
    • 2018-09-03
    • 2015-04-04
    相关资源
    最近更新 更多