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