android – 通过.g3db文件处理Bullet中的冲突

我正在使用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);
            //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 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);
    }

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

解决方法

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...