AndresTraks / BulletSharpPInvoke

.NET wrapper for the Bullet physics library using Platform Invoke

Home Page:http://andrestraks.github.io/BulletSharp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BulletWorldImporter Prints "Failed to find DNA1+SDNA pair"

MichaelRyanGreer opened this issue · comments

Trying to find the simplest way to load/save a single collision shape, code below.

Saving:
`
DefaultSerializer serializer = new DefaultSerializer();
serializer.StartSerialization();

        convexShape.SerializeSingleShape(serializer);

        serializer.FinishSerialization();

        byte[] data = new byte[serializer.CurrentBufferSize];

        System.Runtime.InteropServices.Marshal.Copy(serializer.BufferPointer, data, 0, data.Length);

        using (var file = new System.IO.FileStream(path, System.IO.FileMode.Create))
        {
            file.Write(data, 0, data.Length);
        }

`

Loading:
`
BulletWorldImporter loader = new BulletWorldImporter();

        if (loader.LoadFile(path))
        {
            int numShape = loader.NumCollisionShapes;
            Console.WriteLine(numShape);
            if (numShape > 0)
            {
                return new (ConvexHullShape)loader.GetCollisionShapeByIndex(0);
            }
        }

`

While trying to load, the importer prints out "Failed to find DNA1+SDNA pair".

Would take any suggestions on better ways to load or save collision shapes as well if I'm going about this the wrong way.

Some data wasn't properly written to the file.
I made a quick fix, but I think the serialization code needs a bigger review.

Thank you!

What code are you using to test this? The code I linked is still having the loader output the DNA warning as it was. I'm on the most current version of the repo.

I tried serializing the world in BasicDemo:


and deserializing it in SerializeDemo:
https://github.com/AndresTraks/BulletSharpPInvoke/blob/master/BulletSharp/demos/SerializeDemo/SerializeDemo.cs#L61

No dice on my end, I'm still getting the DNA error even when I try writing it using the world serialization. Here's the full code I'm using:

            //Saving
            var convexShape = new ConvexHullShape(generateRandomVerticesSphere(5.0f, 100));

            var CollisionConfiguration = new DefaultCollisionConfiguration();
            var Dispatcher = new CollisionDispatcher(CollisionConfiguration);
            var Broadphase = new DbvtBroadphase();

            DiscreteDynamicsWorld World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConfiguration);

            DefaultMotionState ms = new DefaultMotionState(Matrix4x4.Identity);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(0.0f, ms, convexShape, Vector3.One);

            RigidBody body = new RigidBody(rbInfo);

            World.AddRigidBody(body);

            using (var serializer = new DefaultSerializer())
            {
                World.Serialize(serializer);

                var dataBytes = new byte[serializer.CurrentBufferSize];

                Marshal.Copy(serializer.BufferPointer, dataBytes, 0, dataBytes.Length);

                using (var file = new FileStream("test.bullet", FileMode.Create))
                {
                    file.Write(dataBytes, 0, dataBytes.Length);
                }
            }

            //Loading
            BulletWorldImporter loader = new BulletWorldImporter();

            if (loader.LoadFile("test.bullet"))
            {
                int numShape = loader.NumCollisionShapes;
                Console.WriteLine(numShape);
                if (numShape > 0)
                {
                    var shape = (ConvexHullShape)loader.GetCollisionShapeByIndex(0);
                    Console.WriteLine("Shape was loaded correctly");
                }
                else
                {
                    Console.WriteLine("Shape was not loaded correctly");
                }
            }

You can try setting the buffer size:

const int maxSerializeBufferSize = 1024 * 1024 * 5;
DefaultSerializer serializer = new DefaultSerializer(maxSerializeBufferSize);

If the size is not set, then the buffer is supposed to be allocated in chunks, but there is something wrong with joining those chunks in the end.

That did it! Thanks so much for your help with this.