BulletSharp binary serialization

From Physics Simulation Wiki

Jump to: navigation, search

This is a C#-ified version of some of the code on Bullet binary serialization.

Saving a single object, shape or acceleration structure

	DefaultSerializer serializer = new DefaultSerializer();
	serializer.StartSerialization();
	// shape = your collision shape
	shape.SerializeSingleShape(serializer);
	serializer.FinishSerialization();
	var stream = serializer.LockBuffer();
 
	// replace this with your own filename of choice. Mine uses the associated ogre node's name
	using (var filestream = System.IO.File.Create("media/" + dslNode.Name + ".bullet", serializer.CurrentBufferSize)) {
		stream.CopyTo(filestream);
		filestream.Close();
	}
	stream.Close();


I don't know how to serialise rigid bodies yet.

Loading a .bullet file, creating objects and adding them to the world

  • Make sure to add BulletSharp.Serialize to your "using" section!


	CollisionShape shape;
 
	// right, so what we do is test to see if this shape has a .bullet file, and if it doesn't, create one
	if (System.IO.File.Exists("media/" + dslNode.Name + ".bullet")) {
		// so it has a file
		BulletWorldImporter importer = new BulletWorldImporter(world);
		// load that file
		if (importer.LoadFile("media/" + dslNode.Name + ".bullet")) {
			// I have mine set up so my files only have one collision shape in them, but you should be able to
			// figure out what to do if you have multiple ones here. It's pretty straightforward.
			shape = importer.GetCollisionShapeByIndex(0);
		}
		else
			// if the file wasn't able to be loaded, throw an exception
			throw new IOException("media/" + dslNode.Name + ".bullet was unable to be imported!");
	}
	// then create your rigid body as normal
Personal tools