Creating multiple rigibody instances from serialized data?

Physics APIs, Physics file formats, Maya, Max, XSI, Cinema 4D, Lightwave, Blender, thinkingParticles™ and other simulation tools, exporters and importers
Post Reply
Kanttori
Posts: 38
Joined: Thu Jul 08, 2010 12:52 am

Creating multiple rigibody instances from serialized data?

Post by Kanttori »

I'm working on some gamecode and I need a way to load serialized bullet-data and then spawn multiple instances of it. The assumption I'm going with is that you only need to have the rigidbodies and constraints as single user and the rest (like shapes etc) can be used by many.

I started coding a class that will extend the btBulletWorldImporter (inherited as private) and use it to store the multiuser data whilst the single user rigidbodies and constraints are copied as data into my class and "instanced" from there. The actual BulletFile will be deleted after it's been used to initialize the shapes etc. and had it's rigidbody etc. data copied for later use.

Is this even doable or is there some caveat I'm not seeing? Input would be appreciated.
Kanttori
Posts: 38
Joined: Thu Jul 08, 2010 12:52 am

Re: Creating multiple rigibody instances from serialized dat

Post by Kanttori »

Well, the code has been done for a while but I haven't had time to test it until now... seemed to work with a couple of ragdoll "bots" I spawned from it. Heavily based on btBulletWorldImporter as in the same code (hacked) for the most part so I retained the naming conventions (btBulletFileSpawn.h and .cpp) but please note that it's in no way official code. The main difference with world importer is that object and constraint data get "stolen" from the bulletfile to be spawned later and bvhMaps and collisionShapes get handled and stored only once when the bulletfile is originally loaded.

You need to manually add the spawn-results to your dynamic world which would go something like this (this code is just a concept, can't copy-paste my code since it's spread around and depends on other things):

Code: Select all

btBulletFileSpawn* bfs = new btBulletFileSpawn();

if(!bfs->loadBulletFile(filename))
{
  //Error
}

// Not using a testFnc to test whether or not to spawn an obj so params 0,0.
btSpawnResult* sr = bfs->spawn(0,0);

// This is how you'd add them to your dynamic world
for(unsigned int c = 0; c < sr->m_allocatedRigidBodies.size(); ++c)
{
  dynworld->addRigidBody((btRigidBody*)sr->m_allocatedRigidBodies[c]);
}

for(unsigned int c = 0; c < sr->m_allocatedConstraints.size(); ++c)
{
  dynworld->addConstraint(sr->m_allocatedConstraints[c],
      sr->m_disableCollisionsBetweenLinkedBodies[c]);
}
You could use the testFnc to skip spawning stuff you don't need, but beware that the constraint spawning in that case may cause problems though it should skip things that don't make sense.
bulletfilespawn.zip
(5.2 KiB) Downloaded 654 times
Post Reply