3

I'd like to create a btConvexHullShape for my game with libgdx. When I tried to do so with the sample code from github:

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

this did not work. My 3D object was just an object which looked like a demolished cube with unnecessary vertices and lines (tested with debugdrawer).

Then I tried to enter the second item of the array and built a convex hull from it with

final Mesh mesh = model.meshes.get(1)

this did give me another part of my 3D object which was a gutter of a bowling alley. But also this was demolished and too small.

Finally I tried two other methods I found online: obtain a btCollisionShape via Bullet.obtainStaticNodeShape(model.nodes); which resulted in a perfect collision shape, but did not detect collision. Another problem with that is that it is only for static shapes, which are not intended to move and in my gamer are several objects which need to move.

The last possible and most cumbersome try I made was creating a .bullet file of my model with blender export and load it in libgdx (method can be seen in http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=15263).

This gives me the right CollisionShape and if you set the Gravity in blender to the right one even the objects fall the right way, from up to down the screen. The only problem with that is as soon as a collision is detected the program crashes.

Unfortunately I can not approximate my shape using primitives as it was suggested in multiple forums. Is there any possibility to create a correct btConvexHullShape in libgdx maybe by manually write a method to create a btConvexHullShape from a set of vertices?

I appreciate your help

EDIT: You are right for this one as the alley itself does not need to be dynamic. But unfortunately the collsion of the bowling ball (which has a sphere collision shape) was not detected with the static collision shape of the bowling alley I got with Bullet.obtainStaticNodeShape(...), so I thought I'd need another approach to that and I'd also like to know just for educational purposes how it is possible to create a btConvexHullShape from any object I enter.

Currently I'm trying to load an .obj, create a Mesh from it and then tried to create a btconvexHullShape with

final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());

At the moment I think, I'M close to the solution but I still don't got it right, as the cube I'm exporting from blender (as a test object) does not create the right collisionshape (it looks like a damaged cube again).

This method seems to work though, because when creating a mesh like in the link: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/gles2/Shapes.java#L24, and creating a btConvexHullShape from it, I get the correct collisionShape.

Ben
  • 2,314
  • 1
  • 19
  • 36
BigPenguin
  • 264
  • 2
  • 17
  • Why does a gutter of a bowling alley have to be a dynamic object? – Xoppa Apr 19 '16 at 20:51
  • I edited my post, you are right that it does not need to be dynamic for this 3d model, but I did not got a collision with that static Shape – BigPenguin Apr 20 '16 at 15:20
  • Looks you are misunderstanding how bullet/convex hull works. If your use-case is a bowling alley and ball then you should not use a convex hull shape. You can't just export your entire scene and expect to create a shape from it, each part of your scene has its own shape. If you still want to create a convex hull shape and have a correct model(part) for it, then you can create one as shown in the tests: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/ConvexHullTest.java (note: that is just an example, you will have to adjust it to fit your model). – Xoppa Apr 20 '16 at 16:05

0 Answers0