4

I am developing a simple application using libgdx and the Bullet API. I have completed the basic prototype of this application using shapes but now I want to load the real models in the application. I have used the .g3db file for loading the models through 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);
    }

Please tell how could I use the custom models for collision detection.

Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40

2 Answers2

3

In the ConvexHullTest you can see how you can create a Shape for your CollisionObject from a Mesh.

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;
}
noone
  • 19,520
  • 5
  • 61
  • 76
  • if i have to use rigid body can it be possible with the ConvexHullTest – Akshay Mukadam Jul 07 '14 at 09:31
  • A rigid body also needs a Shape, if you use the given code it will create a shape that fits your given Model mesh. You can use this with rigid bodies. – noone Jul 07 '14 at 09:53
  • will it detect the collision – Akshay Mukadam Jul 07 '14 at 10:14
  • its not detecting the collision – Akshay Mukadam Jul 07 '14 at 10:36
  • That might have many reasons. Different collision flags/filters/groups, maybe an overriden custom CollisionDispatcher... Did you use the debug drawing mechanism to see how your collision objects look like? – noone Jul 07 '14 at 11:56
  • I have the exact same problem. I have one box created using btBoxShape for a collision shape, and one with btConvexHullShape. The btBoxShape works how I need. They are added with the same flags. When I look at btConvexHullShape in debugDrawer, I see the exact shape I want to see but the collision only works in the center where its an unscaled cube. Maybe this has to do with scaling? – Mouscellaneous Oct 06 '14 at 17:06
0

For me, it helped to "bake" the scales in blender per this post

Scaling a ModelInstance in libgdx 3D and Bullet engine

After doing this, collision shapes worked for me with this method.

Community
  • 1
  • 1
Mouscellaneous
  • 2,584
  • 3
  • 27
  • 37