Ejemplo n.º 1
0
        private static void TestPhysics( string srcDir, string dstDir, string name )
        {
            float OneMeter = 1000;
            PhysicsData pd = new PhysicsData();
            Vector3 center = new Vector3( 10 * OneMeter, 1 * OneMeter, 1 * OneMeter );
            Vector3[] obbAxes = new Vector3[ 3 ];
            obbAxes[ 0 ] = new Vector3( 1, 0, -1 );
            obbAxes[ 0 ].Normalize();
            obbAxes[ 1 ] = Vector3.UnitY;
            obbAxes[ 2 ] = new Vector3( 1, 0, 1 );
            obbAxes[ 2 ].Normalize();
            Vector3 obbExtents = new Vector3( 2.5f * OneMeter, 1 * OneMeter, 1 * OneMeter );
            CollisionOBB obb = new CollisionOBB( center, obbAxes, obbExtents );
            pd.AddCollisionShape( "submesh01", obb );
            CollisionAABB aabb = new CollisionAABB( center - obbExtents, center + obbExtents );
            pd.AddCollisionShape( "submesh01", aabb );
            CollisionSphere sphere = new CollisionSphere( center, 2 * OneMeter );
            pd.AddCollisionShape( "submesh01", sphere );
            Vector3 capExtents = new Vector3( -1 * OneMeter, 0, 0 );
            CollisionCapsule capsule = new CollisionCapsule( center + capExtents, center - capExtents, .5f * OneMeter );
            pd.AddCollisionShape( "submesh01", capsule );
            PhysicsSerializer ps = new PhysicsSerializer();
            ps.ExportPhysics( pd, name );

            pd = new PhysicsData();
            ps.ImportPhysics( pd, name );
            foreach( string objectId in pd.GetCollisionObjects() )
            {
                log.InfoFormat( "Collision shapes for: {0}", objectId );
                List<CollisionShape> collisionShapes = pd.GetCollisionShapes( objectId );
                foreach( CollisionShape shape in collisionShapes )
                    log.InfoFormat( "Shape: {0}", shape );
            }
        }
        public static void ExtractCollisionShapes( Mesh mesh, string path )
        {
            PhysicsData physicsData = null;
            List<string> deleteEm = new List<string>();
            int count = mesh.SubMeshCount;

            for( int i = 0; i < count; i++ )
            {
                SubMesh subMesh = mesh.GetSubMesh( i );
                CollisionShape shape = null;
                string targetName = null;
                bool cv = String.Compare( subMesh.Name.Substring( 0, 5 ), "mvcv_", false ) == 0;
                bool rg = String.Compare( subMesh.Name.Substring( 0, 5 ), "mvrg_", false ) == 0;
                int firstIndex = 0;
                if( cv )
                    firstIndex = 5;
                else if( rg )
                {
                    string rest = subMesh.Name.Substring( 5 );
                    firstIndex = rest.IndexOf( "_" ) + 1 + 5;
                }
                if( cv || rg )
                {
                    // It's probably a collision volume - - check the
                    // shape type to make sure
                    if( String.Compare( subMesh.Name.Substring( firstIndex, 4 ), "obb_", false ) == 0 )
                    {
                        shape = ExtractBox( subMesh );
                    }
                    else if( String.Compare( subMesh.Name.Substring( firstIndex, 5 ), "aabb_", false ) == 0 )
                    {
                        shape = ExtractBox( subMesh );
                    }
                    else if( String.Compare( subMesh.Name.Substring( firstIndex, 7 ), "sphere_", false ) == 0 )
                    {
                        shape = ExtractSphere( subMesh );
                    }
                    else if( String.Compare( subMesh.Name.Substring( firstIndex, 8 ), "capsule_", false ) == 0 )
                    {
                        shape = ExtractCapsule( subMesh );
                    }
                    if( shape != null )
                        targetName = GetTargetSubmesh( mesh, subMesh.Name );
                }
                if( shape != null )
                {
                    deleteEm.Add( subMesh.Name );
                    if( physicsData == null )
                        physicsData = new PhysicsData();
                    physicsData.AddCollisionShape( targetName, shape );
                }
            }
            for( int i = 0; i < deleteEm.Count; i++ )
            {
                mesh.RemoveSubMesh( deleteEm[ i ] );
            }
            if( physicsData != null )
            {
                PhysicsSerializer serializer = new PhysicsSerializer();
                serializer.ExportPhysics( physicsData, path + ".physics" );
            }

            if( DoLog )
                CloseLog();
        }