【问题标题】:Handling collision in Bullet through the .g3db files通过 .g3db 文件处理 Bullet 中的碰撞
【发布时间】:2014-08-27 15:36:54
【问题描述】:

我正在使用 libgdx 和 Bullet API 开发一个简单的应用程序。我已经使用形状完成了这个应用程序的基本原型,但现在我想在应用程序中加载真实模型。我使用 .g3db 文件 通过 AssetManager

加载模型
private void doneLoading() {
        // TODO Auto-generated method stub
        Model model = manager.get("ping1.g3db", Model.class);
        int index = 0;
        for (int i = 0; i < model.nodes.size; i++) {
            String id = model.nodes.get(i).id;
            ModelInstance instance = new ModelInstance(model, id);
            Node node = instance.getNode(id);
            instance.transform.set(node.globalTransform);
            //node.translation.set(0, 0, 0);
            //node.scale.set(1, 1, 1);
            //node.rotation.idt();
            instance.calculateTransforms();

            if (id.equals("ball4") || id.equals("bat")) {
                instance_array.add(instance);
                index += 1;
            }

            if (id.equals("ball4")) {
                ball = instance;
                Gdx.app.log("Ball Index", " " + index);
                instance_map.put("ball", new GameObject.Constructor(model, id, new btSphereShape(0.165f), 1.0f));
                createBall();
                //ball.transform.setToTranslation(0f, 10f, 0f);
            } else if (id.equals("bat")) {
                Gdx.app.log("Bat Index", " " + index);
                bat = instance;
                instance_map.put("bat",new GameObject.Constructor(model, id, new btSphereShape(1.897f), 0.0f));
                createBat();        
            }
        }
        loading = false;
    }


private void createBat() {
        // TODO Auto-generated method stub
        GameObject object=instance_map.get("bat").construct();
        object.body.setCollisionFlags(object.body.getCollisionFlags()|btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
        instances_gameobject.add(object);
        dynamicWorld.addCollisionObject(object.body);
    }


    private void createBall() {
        // TODO Auto-generated method stub
        GameObject object=instance_map.get("ball").construct();
        Gdx.app.log("Ball", "Ball created");
        object.moving=true;
        object.body.setWorldTransform(object.transform);
        object.body.setCollisionFlags(object.body.getCollisionFlags()|btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
        instances_gameobject.add(object);
        dynamicWorld.addCollisionObject(object.body);
    }

请告诉我如何使用自定义模型进行碰撞检测。

【问题讨论】:

    标签: android libgdx game-physics bulletphysics


    【解决方案1】:

    对我来说,这篇文章有助于在搅拌机中“烘烤”秤

    Scaling a ModelInstance in libgdx 3D and Bullet engine

    完成此操作后,碰撞形状使用此方法对我有用。

    【讨论】:

      【解决方案2】:

      ConvexHullTest 中,您可以看到如何从Mesh 为您的CollisionObject 创建Shape

      public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize) {
          final Mesh mesh = model.meshes.get(0);
          final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
          if (!optimize) return shape;
          // now optimize the shape
          final btShapeHull hull = new btShapeHull(shape);
          hull.buildHull(shape.getMargin());
          final btConvexHullShape result = new btConvexHullShape(hull);
          // delete the temporary shape
          shape.dispose();
          hull.dispose();
          return result;
      }
      

      【讨论】:

      • 如果我必须使用刚体,是否可以使用 ConvexHullTest
      • 刚体也需要一个形状,如果您使用给定的代码,它将创建一个适合您给定模型网格的形状。您可以将其用于刚体。
      • 它会检测到碰撞
      • 它没有检测到碰撞
      • 这可能有很多原因。不同的碰撞标志/过滤器/组,可能是覆盖的自定义 CollisionDispatcher...您是否使用调试绘图机制来查看碰撞对象的外观?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多