public void SerializeTest()
        {
            var fileLoader = new BulletWorldImporter(_world);
            var objects = _world.CollisionObjectArray;

            Assert.True(fileLoader.LoadFile("data\\bsp.bullet"));
            Assert.AreEqual(127, objects.Count);
            Assert.AreEqual(127, fileLoader.NumCollisionShapes);
            Assert.True(objects.All(o => o.CollisionShape is ConvexHullShape));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\concaveCompound.bullet"));
            Assert.AreEqual(1, fileLoader.NumBvhs);
            Assert.AreEqual(6, fileLoader.NumCollisionShapes);
            Assert.AreEqual(11, objects.Count);
            Assert.AreEqual(10, objects.Count(o => o.CollisionShape is CompoundShape));
            var triangleMeshShape =
                objects.Select(o => o.CollisionShape).FirstOrDefault(s => s is BvhTriangleMeshShape) as BvhTriangleMeshShape;
            Assert.NotNull(triangleMeshShape);
            Assert.False(triangleMeshShape.OwnsBvh);
            Assert.NotNull(triangleMeshShape.OptimizedBvh);
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\constraints.bullet"));
            Assert.AreEqual(10, fileLoader.NumConstraints);
            Assert.AreEqual(10, _world.NumConstraints);
            Assert.AreEqual(17, objects.Count);
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\convex_decomposition.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\cylinders.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\multibody.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);
            /*
            Assert.True(fileLoader.LoadFile("data\\r2d2_multibody.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);
            */
            Assert.True(fileLoader.LoadFile("data\\ragdoll_6dof.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\ragdoll_conetwist.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\slope.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\spider.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);

            Assert.True(fileLoader.LoadFile("data\\testFile.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);
            /*
            Assert.True(fileLoader.LoadFile("data\\testFileFracture.bullet"));
            fileLoader.DeleteAllData();
            Assert.AreEqual(0, objects.Count);
            */
        }
예제 #2
0
        protected override void OnInitializePhysics()
        {
            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher = new CollisionDispatcher(CollisionConf);

            Broadphase = new DbvtBroadphase();

            World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf);
            World.Gravity = new Vector3(0, -10, 0);

            GImpactCollisionAlgorithm.RegisterAlgorithm(Dispatcher);

            string bulletFile;
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length == 1)
            {
                bulletFile = "testFile.bullet";
            }
            else
            {
                bulletFile = args[1];
            }

            fileLoader = new CustomBulletWorldImporter(World);
            if (!fileLoader.LoadFile(bulletFile))
            {
                CollisionShape groundShape = new BoxShape(50);
                CollisionShapes.Add(groundShape);
                RigidBody ground = LocalCreateRigidBody(0, Matrix.Translation(0, -50, 0), groundShape);
                ground.UserObject = "Ground";

                // create a few dynamic rigidbodies
                float mass = 1.0f;

                Vector3[] positions = new Vector3[2] { new Vector3(0.1f, 0.2f, 0.3f), new Vector3(0.4f, 0.5f, 0.6f) };
                float[] radi = new float[2] { 0.3f, 0.4f };

                CollisionShape colShape = new MultiSphereShape(positions, radi);

                //CollisionShape colShape = new CapsuleShapeZ(1, 1);
                //CollisionShape colShape = new CylinderShapeZ(1, 1, 1);
                //CollisionShape colShape = new BoxShape(1);
                //CollisionShape colShape = new SphereShape(1);
                CollisionShapes.Add(colShape);

                Vector3 localInertia = colShape.CalculateLocalInertia(mass);

                float start_x = StartPosX - ArraySizeX / 2;
                float start_y = StartPosY;
                float start_z = StartPosZ - ArraySizeZ / 2;

                int k, i, j;
                for (k = 0; k < ArraySizeY; k++)
                {
                    for (i = 0; i < ArraySizeX; i++)
                    {
                        for (j = 0; j < ArraySizeZ; j++)
                        {
                            Matrix startTransform = Matrix.Translation(
                                2 * i + start_x,
                                2 * k + start_y,
                                2 * j + start_z
                            );

                            // using motionstate is recommended, it provides interpolation capabilities
                            // and only synchronizes 'active' objects
                            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
                            RigidBodyConstructionInfo rbInfo =
                                new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
                            RigidBody body = new RigidBody(rbInfo);
                            rbInfo.Dispose();

                            // make it drop from a height
                            body.Translate(new Vector3(0, 20, 0));

                            World.AddRigidBody(body);
                        }
                    }
                }

                DefaultSerializer serializer = new DefaultSerializer();

                serializer.RegisterNameForObject(ground, "GroundName");

                for (i = 0; i < CollisionShapes.Count; i++)
                    serializer.RegisterNameForObject(CollisionShapes[i], "name" + i.ToString());

                Point2PointConstraint p2p = new Point2PointConstraint((RigidBody)World.CollisionObjectArray[2], new Vector3(0, 1, 0));
                World.AddConstraint(p2p);

                serializer.RegisterNameForObject(p2p, "constraintje");

                World.Serialize(serializer);

                BulletSharp.DataStream data = serializer.LockBuffer();
                byte[] dataBytes = new byte[data.Length];
                data.Read(dataBytes, 0, dataBytes.Length);

                FileStream file = new FileStream("testFile.bullet", FileMode.Create);
                file.Write(dataBytes, 0, dataBytes.Length);
                file.Close();
            }
        }
        /// <summary>
        /// Imports a collision shape from a .bullet file.
        /// </summary>
        /// <param name="bulletfile">Part of the filename. "media/physics/" + name + ".bullet"</param>
        /// <remarks>
        /// This only imports the first collision shape from the file. If it has multiple, they will be ignored.
        /// </remarks>
        public CollisionShape ImportCollisionShape(string bulletfile)
        {
            BulletWorldImporter importer = new BulletWorldImporter(LKernel.GetG<PhysicsMain>().World);

            string filename;
            if (bulletFiles.TryGetValue(bulletfile, out filename)) {
                // load that file
                if (importer.LoadFile(filename)) {
                    Launch.Log(string.Concat("[PhysicsMain] Importing ", bulletfile, "..."));
                    // these should only have one collision shape in them, so we'll just use that
                    return importer.GetCollisionShapeByIndex(0);
                }
                else {
                    // if the file wasn't able to be loaded, throw an exception
                    throw new IOException(bulletfile + " was unable to be imported!");
                }
            }
            else
                throw new FileNotFoundException("That .bullet file was not found!", bulletfile);
        }