IAmNoone
IAmNoone

Reputation: 1011

Using btSoftBodyHelpers::CreateFromTriMesh with trimesh

I've been trying for a while to get support for softbodies in my project, I have already added all primitives, including static triangle meshes as you can see below:

enter image description here I've now been trying to implement the softbodies. I do have triangle shapes as I mentioned, and I thought I could re-use the triangulation code to create softbody objects with the function:

btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(.....);

I successfully did this with the bunny mesh that's hardcoded, but now I want to insert any trinangulated mesh into this function. But I'm a bit lost figuring out exactly what parameters to send in (how to get the right parameters from my triangulated mesh).

Do anyone of you have a example of this? (not a hardcoded one, but from a

btTriangleMesh *mTriMesh = new btTriangleMesh();

type object? )

It does work with the predefined type shapes that bullet has, so my update loop and all that works fine.

Upvotes: 4

Views: 815

Answers (1)

Shank
Shank

Reputation: 330

This is for version 2.81 (assuming vertices are stored as PHY_FLOAT and indices as PHY_INTEGER):

btTriangleMesh *mTriMesh = new btTriangleMesh();

// ...

const btVector3 meshScaling = mTriMesh->getScaling();

btAlignedObjectArray<btScalar> vertices;
btAlignedObjectArray<int> triangles;

for (int part=0;part< mTriMesh->getNumSubParts(); part++)
{
    const unsigned char * vertexbase;
    const unsigned char * indexbase;

    int indexstride;
    int stride,numverts,numtriangles;
    PHY_ScalarType type, gfxindextype;

    mTriMesh->getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part);

    for (int gfxindex=0; gfxindex < numverts; gfxindex++)
    {
        float* graphicsbase = (float*)(vertexbase+gfxindex*stride);
        vertices.push_back(graphicsbase[0]*meshScaling.getX());
        vertices.push_back(graphicsbase[1]*meshScaling.getY());
        vertices.push_back(graphicsbase[2]*meshScaling.getZ());
    }

    for (int gfxindex=0;gfxindex < numtriangles; gfxindex++)
    {
        unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
        triangles.push_back(tri_indices[0]);
        triangles.push_back(tri_indices[1]);
        triangles.push_back(tri_indices[2]);
    }
}

btSoftBodyWorldInfo worldInfo;

// Setup worldInfo...
// ....

btSoftBodyHelper::CreateFromTriMesh(worldInfo, &vertices[0], &triangles[0], triangles.size()/3 /*, randomizeConstraints = true*/);

A slower, more general approach is to iterate the mesh using mTriMesh->InternalProcessAllTriangles() but that will make your mesh a soup.

Upvotes: 1

Related Questions