Faced the problem of creating a bounding shape for a 3D object, the model of which consists of a set of Mesh. To create a bounding shape I use the method: (changed the method of the last publication)

/**Создает ограничивающую фигуру для динамичного объекта * @param model - 3D модель * @param optimize - флаг определяющий необходимость применения оптимизации (упрощения) * @param numMesh - более не используется (ограничивающая фигура строится по всем имеющимся Mesh)*/ public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize, int numMesh) { btConvexHullShape shape = null; int workBufferSize=0; int numVertex=0; for (int i = 0; i <= model.meshParts.size-1;i++) { final Mesh mesh = model.meshParts.get(i).mesh; numVertex += mesh.getNumVertices(); workBufferSize += mesh.getVerticesBuffer().capacity(); } FloatBuffer workBuffer = BufferUtils.newFloatBuffer(workBufferSize); for (int i = 0; i <= model.meshParts.size-1;i++) { Matrix4 transform = new Matrix4(model.nodes.get(0).localTransform); // transform.setToTranslation(model.meshParts.get(i).center.add(model.meshParts.get(i).halfExtents)); final Mesh mesh = model.meshParts.get(i).mesh; FloatBuffer tempWorkBuffer = BufferUtils.newFloatBuffer(mesh.getVerticesBuffer().capacity()); BufferUtils.copy(mesh.getVerticesBuffer(), tempWorkBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4); BufferUtils.transform(tempWorkBuffer, 3, mesh.getVertexSize(), mesh.getNumVertices(), transform); BufferUtils.copy(tempWorkBuffer, workBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4); } // Без оптимизации сверх точная ограничивающая фигура (но очень напряжно для CPU) // тут можно снять ! и будет видны точно все контуры ограничивающиз фигур if (!optimize) { shape = new btConvexHullShape(workBuffer, numVertex, model.meshes.get(0).getVertexSize()); return shape; } // First create a shape using all the vertices, then use the built in tool to reduce // the number of vertices to a manageable amount. btConvexShape convexShape = new btConvexHullShape(workBuffer, numVertex, model.meshes.get(0).getVertexSize()); btShapeHull hull = new btShapeHull(convexShape); hull.buildHull(convexShape.getMargin()); shape = new btConvexHullShape(hull); convexShape.dispose(); hull.dispose(); return shape; } 

Thus, I managed to build a bounding shape based on all of the existing bounding meshes (Mesh) of the 3D model, but one problem remained.

  • When creating a bounding shape for a model with a 1st mesh (Mesh) matrix, the transformation is obtained

    Matrix4 transform = new Matrix4 (model.nodes.get (0) .localTransform);

and applied later BufferUtils.transform works fine

  • but as soon as the model is more complex, all bounding grids are heaped

QUESTION: how to get their offsets correctly and spread them to the right positions

Fully source code can be found here.

class com.leganas.game.framework.graphics.engine3D.Phisics.java

  • Using the example with GitHub changed the method, I don’t know how to insert it here using post editing - Andrey Lehan
  • one
    model.nodes is what? You get a matrix at 0 node and then use it everywhere - vik_78
  • litter, as I understand it, I got confused in terminology, I mixed up Node and Vertices (Top). So Node is a figure for example a square or a ball or any other complex figure (one of the component parts of the whole model). Each Node has its own Mesh, which is actually a set of vertices from which an integral 3D figure is obtained, and a bounding figure for a solid body object is built on it (it will completely surround the Mesh for this Node) and, accordingly, the transformation matrix must be taken from each individual Node and applied to every mesh. - Andrey Lehan
  • One vidos of youtube.com/watch?v=K1D5zmq9CIs helped to figure out, there is also a hint how to correctly merge several Mesh into one, I understand you need to cycle through all Node and all their descendants merge all the meshes into one and make it bounding figure. Next will be the question of how this figure will work when implementing animation (movement of arms and legs) will it follow the contours of the model when the skeleton is displaced. But that's another story :) - Andrey Lehan
  • If the model is broken into several meshes, I do not think it is a good idea to combine meshes for convex. You get a convex that completely covers your model, while losing the accuracy of collision detection. Go ahead, while animating, individual meshes should move, while you have a convex on the model in the default state, so to say, if you raise your hand for example, it will come out of the convex, again the problem with collisions. - vik_78

0