Search code examples
c++serializationgame-enginegame-physicsbulletphysics

Bullet: Load/Save collisionshape


I am trying to prefent looping all triangles and add each one to the btTriangleMesh. (Only loading has to be fast, saving speed can be ignored.)

So what is the fastest method for loading collision data from a file. How about these two:

  1. Saving a Vertex(bt3Vector) & Index(DWORD) array and on loading just resize the btTriangleMesh and set the data at once.

  2. Using the serializeSingleShape() for saving and for loading something like the ReadBulletSample (or init a new btDynamicsWorld, read the file with the BulletWorldImporter, get the collision object and cleanup the btDynamicsWorld var)

If there are any other methods, please tell me. The model geometry has these two buffers:

Vertex = vector<float[3]>
Index = vector<DWORD>

Solution

  • I used serialization code from bullet. I believe it is already optimized and do no see reason why you should re-invent it.

    bt_col - is bullet collision object

        int maxSerializeBufferSize = 1024*1024*5;
        btDefaultSerializer*    serializer = new btDefaultSerializer(maxSerializeBufferSize);
    
        serializer->startSerialization();
        bt_col->serializeSingleShape(serializer);
        serializer->finishSerialization();
    
        FILE* file = fopen(filename, "wb");
        fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1, file);
        fclose(file);
    
        delete serializer;