public bool ParseFromFile(string fname)
        {

            BlenderPhmoReader reader = new BlenderPhmoReader(fname);
            fileStruct = reader.ReadFile();
            if (fileStruct == null)
            {
                Console.WriteLine("Could not parse file.");
                return false;
            }

            //create a rigidbody for the phmo
            var rigidbody = new PhysicsModel.RigidBody();
            rigidbody.Mass = 100f; //probably not important
            rigidbody.CenterOfMassK = 0.25f; //probably not important
            rigidbody.ShapeIndex = 0; //important
            rigidbody.MotionType = MotionTypeValue.Keyframed; // keyframed movement for now.

            var shapedefs = fileStruct.AsArray;
            if (shapedefs.Count < 1)
            {
                Console.WriteLine("No shapes!");
                return false;
            }
            else if (shapedefs.Count < 2)
            {
                //optimise the phmo by avoiding the use of the list-shape
                // as a level of indirection for multiple shapes.
                //Also for ease of shape encoding, AddShape returns the 
                //shape-type added. 'Unused0' is used to represent failure.
                if (AddShape(_phmo, shapedefs[0]) == ShapeTypes.Unused0)
                {
                    return false;
                }


                rigidbody.ShapeType = ShapeTypes.Polyhedron;


                //this field does not have any influence
                //rigidbody.BoundingSphereRadius = 0.5f;
            }
            else
            {
                rigidbody.ShapeType = ShapeTypes.List;
                //phmo needs to use shapelist and listelements reflexives
                _phmo.Lists = new List<PhysicsModel.List>();
                _phmo.ListShapes = new List<PhysicsModel.ListShape>();
                PhysicsModel.List shapeList = new PhysicsModel.List();

                Console.WriteLine("Loading multiple shapes");
                int amountAdded = 0;
                foreach (JSONNode listelem in fileStruct.AsArray)
                {
                    ShapeTypes typeAdded = AddShape(_phmo, listelem);
                    if (typeAdded == ShapeTypes.Unused0)
                    {
                        Console.WriteLine("Failed loading shape.");
                        return false;
                    }

                    PhysicsModel.ListShape shapeElem = new PhysicsModel.ListShape();
                    shapeElem.ShapeType = (PhysicsModel.ListShape.ShapeTypeValue)typeAdded;
                    //assumes the shape added should be at the end of the respected list.
                    shapeElem.ShapeIndex = (short)(getNumberOfShapes(_phmo, typeAdded) - 1);
                    _phmo.ListShapes.Add(shapeElem);
                    amountAdded++;


                }
                shapeList.Count = 128;
                shapeList.ChildShapesSize = amountAdded;
                shapeList.ChildShapesCapacity = (uint)(amountAdded + 0x80000000);
                _phmo.Lists.Add(shapeList);
                Console.WriteLine("Added {0} shapes.", amountAdded);
            }


            _phmo.RigidBodies.Add(rigidbody);

            return true;
        }
예제 #2
0
        public bool ParseFromFile(string fname)
        {
            BlenderPhmoReader reader = new BlenderPhmoReader(fname);

            fileStruct = reader.ReadFile();
            if (fileStruct == null)
            {
                Console.WriteLine("Could not parse file.");
                return(false);
            }

            //create a rigidbody for the phmo
            var rigidbody = new PhysicsModel.RigidBody();

            rigidbody.Mass          = 100f;                      //probably not important
            rigidbody.CenterOfMassK = 0.25f;                     //probably not important
            rigidbody.ShapeIndex    = 0;                         //important
            rigidbody.MotionType    = MotionTypeValue.Keyframed; // keyframed movement for now.

            var shapedefs = fileStruct.AsArray;

            if (shapedefs.Count < 1)
            {
                Console.WriteLine("No shapes!");
                return(false);
            }
            else if (shapedefs.Count < 2)
            {
                //optimise the phmo by avoiding the use of the list-shape
                // as a level of indirection for multiple shapes.
                //Also for ease of shape encoding, AddShape returns the
                //shape-type added. 'Unused0' is used to represent failure.
                if (AddShape(_phmo, shapedefs[0]) == ShapeTypes.Unused0)
                {
                    return(false);
                }


                rigidbody.ShapeType = ShapeTypes.Polyhedron;


                //this field does not have any influence
                //rigidbody.BoundingSphereRadius = 0.5f;
            }
            else
            {
                rigidbody.ShapeType = ShapeTypes.List;
                //phmo needs to use shapelist and listelements reflexives
                _phmo.Lists      = new List <PhysicsModel.List>();
                _phmo.ListShapes = new List <PhysicsModel.ListShape>();
                PhysicsModel.List shapeList = new PhysicsModel.List();

                Console.WriteLine("Loading multiple shapes");
                int amountAdded = 0;
                foreach (JSONNode listelem in fileStruct.AsArray)
                {
                    ShapeTypes typeAdded = AddShape(_phmo, listelem);
                    if (typeAdded == ShapeTypes.Unused0)
                    {
                        Console.WriteLine("Failed loading shape.");
                        return(false);
                    }

                    PhysicsModel.ListShape shapeElem = new PhysicsModel.ListShape();
                    shapeElem.ShapeType = (PhysicsModel.ListShape.ShapeTypeValue)typeAdded;
                    //assumes the shape added should be at the end of the respected list.
                    shapeElem.ShapeIndex = (short)(getNumberOfShapes(_phmo, typeAdded) - 1);
                    _phmo.ListShapes.Add(shapeElem);
                    amountAdded++;
                }
                shapeList.Count               = 128;
                shapeList.ChildShapesSize     = amountAdded;
                shapeList.ChildShapesCapacity = (uint)(amountAdded + 0x80000000);
                _phmo.Lists.Add(shapeList);
                Console.WriteLine("Added {0} shapes.", amountAdded);
            }


            _phmo.RigidBodies.Add(rigidbody);

            return(true);
        }