0
我試圖計算鼠標懸停在Libgdx中的3D模型實例上。截至目前,我所有的模型實例都是完美的矩形。我從鼠標x和y座標創建了一個拾取光線,並使用Intersection類來嘗試計算交點,但似乎交點僅在鼠標位於位於模型實例邊界框的區域時才進行註冊屏幕的中心。實際上,模型實例的位置稍偏離一側。Libgdx - 桌面 - 鼠標懸停在3D模型實例
我的理論是,邊界框只存儲模型實例的維度而不是位置。但我還沒有證明這一點,是否有另一種方法來計算從鼠標座標和模型實例的邊界框所產生的光線的交集?
這是我目前的代碼。
// 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;
}
如果您可以提供任何可以幫助我的建議或代碼,我會永遠感激。另外,讓我知道如果我可以添加任何東西來澄清或幫助你,因爲你協助我。
看看http://blog.xoppa.com/interacting-with-3d-objects/ – Xoppa 2014-10-19 22:57:51
我已經讀過這個,我仍然不會看不到我做了什麼不同的事情,這會提供我所看到的錯誤。你能提出一些建議,告訴我如何編輯我的代碼來匹配文章的代碼嗎? – 2014-10-19 22:58:46
不,您的代碼太不同了(例如,計算每個鼠標移動的邊界框不是一個好主意,因爲這是一個非常昂貴的方法)。也許你可以編輯教程的代碼來匹配你的用例? – Xoppa 2014-10-19 23:04:10