android – 如何在libgdx中获取模型实例的边界框?

android – 如何在libgdx中获取模型实例的边界框?,第1张

概述我创建了一个模型(一个盒子)和100个实例,在世界各地移动.它们会被渲染到预期的位置. 我现在正在尝试实现光线拾取,但我发现所有100个实例都返回原始模型的相同边界框.为什么不为每个实例计算实际的边界框? 创建模型和实例: public static void createModel() { ModelBuilder modelBuilder = new ModelBuilder(); 我创建了一个模型(一个盒子)和100个实例,在世界各地移动.它们会被渲染到预期的位置.

我现在正在尝试实现光线拾取,但我发现所有100个实例都返回原始模型的相同边界框.为什么不为每个实例计算实际的边界框?

创建模型和实例:

public static voID createModel() {   ModelBuilder modelBuilder = new ModelBuilder();   modelBuilder.begin();   MeshPartBuilder tileBuilder;   tileBuilder = modelBuilder.part("top",GL10.GL_TRIANGLES,Usage.position | Usage.normal | Usage.TextureCoordinates,new Material(colorAttribute.createDiffuse(color.WHITE)));   tileBuilder.rect(-0.45f,0.1f,0.45f,-0.45f,0f,1f,0f);   tileBuilder = modelBuilder.part("bottom",Usage.position | Usage.normal,...   modelTile = modelBuilder.end();}public voID createModelinstance(com.badlogic.gdx.assets.AssetManager assetManager) {  Texture texTile = assetManager.get("textures/" + texturefile,Texture.class);  Material mat = new Material(TextureAttribute.createDiffuse(texTile));  modelinstance = new Modelinstance(modelTile);   modelinstance.nodes.get(0).parts.get(0).material.set(mat);}

移动模型实例:

public voID setCoordinates(boolean animate,float x,float y,float z) {  modelinstance.transform.setToTranslation(x,y,z);  modelinstance.calculatetransforms();}

射线拣选尝试:

public boolean pickTile(int x,int y) {  cam.update();  Ray ray = cam.getPickRay(x,y);  BoundingBox bb = new BoundingBox();  for (int i=0;i<gameData.tiles.length;i++) {    Log.i("ltower","checking " + i);    gameData.tiles[i].getModelinstance().calculateBoundingBox(bb);Log.i("ltower","BB: " + bb.getCenter().x + "," + bb.getCenter().y + "," + bb.getCenter().z);    if (Intersector.intersectRayBoundsFast(ray,bb)) {      gameData.tiles[i].select();      return true;    }  }  return false;}

日志:

01-05 05:27:18.934: I/ltower(2175): checking 001-05 05:27:18.984: I/ltower(2175): BB: 0.0,0.05,0.001-05 05:27:19.024: I/ltower(2175): checking 101-05 05:27:19.024: I/ltower(2175): BB: 0.0,0.001-05 05:27:19.044: I/ltower(2175): checking 201-05 05:27:19.064: I/ltower(2175): BB: 0.0,0.001-05 05:27:19.064: I/ltower(2175): checking 301-05 05:27:19.064: I/ltower(2175): BB: 0.0,0.001-05 05:27:19.064: I/ltower(2175): checking 401-05 05:27:19.064: I/ltower(2175): BB: 0.0,0.001-05 05:27:19.064: I/ltower(2175): checking 501-05 05:27:19.104: I/ltower(2175): BB: 0.0,0.001-05 05:27:19.104: I/ltower(2175): checking 601-05 05:27:19.104: I/ltower(2175): BB: 0.0,0.0 ...
解决方法 由于您只是创建了同一模型的多个实例,因此它们也具有相同的BoundingBox.边界框将只是模型局部坐标系顶点周围的边界框.

这意味着您需要使用BoundingBox.mul()将模型实例的位置,比例和旋转应用于边界框.

总结

以上是内存溢出为你收集整理的android – 如何在libgdx中获取模型实例的边界框?全部内容,希望文章能够帮你解决android – 如何在libgdx中获取模型实例的边界框?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129365.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存