Issues with serializing shapes

Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Issues with serializing shapes

Post by Mako_energy02 »

This is a continuation of this thread. Making a new thread since my issue has changed somewhat. By that I mean I have a new issue in addition to the old issue.

What I need is a bullet file with the compound shapes(crafted in blender by an artist) that have their names serialized, so I can just load the file, know exactly where each shape has to go and run the game. First I found out blender doesn't serialize names for shapes, only RigidBodies(As Erwin suggested in the other post). So I set out to add names to them myself. De-serializing the .bullet file provided by blender, registering names for the shapes and re-serializing them. Not only does this completely not work(both according to file inspector and according to tests attempting to de-serialize the new file), but when I try to use the compound shapes in the newly made bullet file, they are not there...only their children are. Which renders the file essentially useless. The compound shape data does appear in File Inspector, but the compound shape itself never gets de-serialized. Next I saw a forum post from Erwin a while back that said the importer is meant to only be used in conjunction with a full world Serialize. If you want to de-serialize individual things, you have to write your own code similar to that of the world importer. So the last two days I've done exactly that. However I get the exact same results as using the bullet world importer, PLUS the physics seems different. It's subtle but objects don't rotate as much, seem slower in general. I have no idea what in the collision shapes could be causing that.

I have even upgraded from Bullet 2.77 to 2.78 just because I was hoping some serialization improvement was made that would solve this issue. No such luck. So now I'm at a loss and to say I've frustrated at the amount of time I've spent trying to get something simple (a name on a shape ffs) would be an understatement.

Once again, here is the relevant code I am using:
Deserializing:

Code: Select all

        btBulletWorldImporter Importer;
        Ogre::DataStreamPtr Stream = Ogre::ResourceGroupManager::getSingleton().openResource(FileName,Group);
        char* buffer = new char[Stream->size()];
        Stream->read((void*)buffer, Stream->size());
        if(!Importer.loadFileFromMemory(buffer, Stream->size()))
        {
            [throw exception]
        }
        delete[] buffer;
        for( Whole X = 0 ; X < Importer.getNumCollisionShapes() ; ++X )
        {
            btCollisionShape* Shape = Importer.getCollisionShapeByIndex(X);
            const char* MaybeAName = Importer.getNameForPointer((void*)Shape);
            String Name;
            if(MaybeAName)
            {
                Name = String(MaybeAName);
                CollisionShapeManager::iterator it = CollisionShapes.find(Name);
                if(it != CollisionShapes.end())
                {
                    CollisionShape* NewShape = WrapShape(Name,Shape);
                    CollisionShapes[Name] = NewShape;
                }
            }else{
                static Whole NameCount = 0;
                Name = String("Unnamed")+=ToString(NameCount++);
                CollisionShape* NewShape = WrapShape(Name,Shape);
                UnnamedShapes.insert(NewShape);
            }
        }
Serializing:

Code: Select all

        btDefaultSerializer* BulletSerializer = new btDefaultSerializer(1024*1024*5);
        BulletSerializer->startSerialization();
        for( std::map<String,CollisionShape*>::iterator it = CollisionShapes.begin() ; it != CollisionShapes.end() ; it++ )
        {
            CollisionShape* Shape = (*it).second;
            BulletSerializer->registerNameForPointer((void*)Shape->GetBulletShape(),(*it).first.c_str());
            int len = Shape->GetBulletShape()->calculateSerializeBufferSize();
            btChunk* chunk = BulletSerializer->allocate(len,1);
            const char* structType = Shape->GetBulletShape()->serialize(chunk->m_oldPtr, BulletSerializer);
            BulletSerializer->finalizeChunk(chunk,structType,BT_SHAPE_CODE,Shape->GetBulletShape());
        }
        BulletSerializer->finishSerialization();
        FILE* f2 = fopen(FileName.c_str(),"wb");
        fwrite(BulletSerializer->getBufferPointer(),BulletSerializer->getCurrentBufferSize(),1,f2);
        fclose(f2);
Someone out there has to have a fresh idea or ideally an answer as to why the compound shapes aren't de-serializing properly and why names don't ever get serialized.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Issues with serializing shapes

Post by Erwin Coumans »

Why can't you simply use object (rigid body) names?

As far as I know, saving and loading btCompoundCollisionShape should work fine. I'll see if I can try reproducing the problem.

Are you using the latest Blender 2.59 to export the Bullet world?
Thanks,
Erwin
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: Issues with serializing shapes

Post by Mako_energy02 »

According to File Inspector, the names being set in blender aren't being serialized to the rigid bodies either. I'm just not trying to fix that because I shouldn't need rigid bodies to move around the collision shapes. Plus I want to minimize the size of data I am moving around. If I must have rigid bodies in there to get it to work in a timely fashion, I'll be happy to leave them in there but then it goes back to what I said initially...they aren't being serialized.

My bullet files are still attached to my previous post if you want that data to try and reproduce the problem.

No, we are currently using the version of blender that was on the Bullet Physics Editor download page up until today. I believe it was 2.56.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Issues with serializing shapes

Post by Erwin Coumans »

It all works just fine, so you must do something wrong.

I just tried exporting a rigid body with a compound shape in Blender 2.59, and give it the name 'MyCompound'.
You don't need a custom Blender version anymore, .bullet export is built-in Blender nowadays.

When loading the .bullet file using btBulletWorldImporter, it finds a compound shape, and a rigid body pointing to it with the name 'MyCompound'.

Code: Select all

bool	btBulletWorldImporter::convertAllObjects(  bParse::btBulletFile* bulletFile2)
{
....

for (i=0;i<bulletFile2->m_rigidBodies.size();i++)
	{

...
	btRigidBodyFloatData* colObjData = (btRigidBodyFloatData*)bulletFile2->m_rigidBodies[i];
			if (colObjData->m_collisionObjectData.m_name)
			{
				printf("Found object with name: %s\n", colObjData->m_collisionObjectData.m_name);
			}
See attached .blend file, .bullet file. You can use the physics editor to load the .bullet file and show the names.
Thanks,
Erwin
You do not have the required permissions to view the files attached to this post.
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: Issues with serializing shapes

Post by Mako_energy02 »

I managed to resolve the issue. I first took a look at your .bullet files in FileInspector and it said there were no names on any objects, be it bodies or shapes. Considering I was already getting inconsistent results between the Editor and FileInspector with my original files, I concluded FileInspector is broken and/or untrustworthy. So I did some more tests disregarding any information from FileInspector. I de-serialized my file from blender, registered names and re-serialized just the shapes. When serializing only shapes I can't test in Bullet Editor cause it needs bodies to render the shapes, which makes some degree of sense. When my app went to read the newly created the file it detected the names on the shapes(which it wasn't doing before), but it still wasn't getting added to my shape container because of an error in my code which isn't related to serialization. I was performing a find on my collision shape map but was using "!=" instead of "==" in my code to determine if I should add the shape to the map. Fixed that and it's now working. I'm not sure what happened to make it start seeing the names.

As for the compound shape being de-serialized, I have no idea what fixed that either. But it's now properly de-serializing compounds. I did upgrade to Bullet 2.78 during all this, from 2.77, but as I understand it serialization didn't change much/enough to readily explain that. Maybe it was a mismatch of bullet versions between my app and blender?

I still have to de-seriailze, and then re-serialize every bullet file I get from blender myself, which is inconvenient, but at least it works.
jaocarsan
Posts: 5
Joined: Sat Nov 27, 2010 6:03 pm

Re: Issues with serializing shapes

Post by jaocarsan »

Erwin Coumans wrote: See attached .blend file, .bullet file. You can use the physics editor to load the .bullet file and show the names.
Thanks,
Erwin
After download, I've opened "compoundShapeExportBullet.blend" with Blender 2.56 and export to "TestFile.bullet".

Then I run again "AppSerializeDemo.exe" and get a world with differente rotation.

What am I doing wrong?
You do not have the required permissions to view the files attached to this post.