public static void load(string filename, PhysicsWorld physics_world, Dictionary <string, UniqueMesh> meshes)
        {
            if (!File.Exists(filename))
            {
                throw new Exception("Physics File Not Found\n" + filename);
            }

            Debug.DebugHelper.logInfo(1, "Loading Physics File", Path.GetFileName(filename));

            List <string>  ids        = new List <string>();
            List <string>  mesh_ids   = new List <string>();
            List <string>  shapes     = new List <string>();
            List <Vector3> positions  = new List <Vector3>();
            List <Vector3> rotations  = new List <Vector3>();
            List <Vector3> dimensions = new List <Vector3>();
            List <Vector3> scales     = new List <Vector3>();
            List <bool[]>  flags      = new List <bool[]>();
            List <float[]> attributes = new List <float[]>();

            int num_bodies = 0;

            StreamReader sr = new StreamReader(filename);

            string line;

            while (!sr.EndOfStream)
            {
                line = sr.ReadLine();

                if (line.Length != 0)
                {
                    string   single_value = line.Substring(4);
                    string[] multi_value  = line.Substring(4).Split(' ');

                    switch (line.Substring(0, 4))
                    {
                    case "nam ":
                        ids.Add(single_value);
                        break;

                    case "mam ":
                        mesh_ids.Add(single_value);
                        break;

                    case "shp ":
                        shapes.Add(single_value);
                        break;

                    case "pos ":
                        Vector3 pos;
                        pos.X = float.Parse(multi_value[0]);
                        pos.Y = float.Parse(multi_value[1]);
                        pos.Z = float.Parse(multi_value[2]);
                        positions.Add(pos);
                        break;

                    case "rot ":
                        Vector3 rot;
                        rot.X = OpenTK.MathHelper.RadiansToDegrees(float.Parse(multi_value[0]));
                        rot.Y = OpenTK.MathHelper.RadiansToDegrees(float.Parse(multi_value[1]));
                        rot.Z = OpenTK.MathHelper.RadiansToDegrees(float.Parse(multi_value[2]));
                        rotations.Add(rot);
                        break;

                    case "dim ":
                        Vector3 dim;
                        dim.X = float.Parse(multi_value[0]);
                        dim.Y = float.Parse(multi_value[1]);
                        dim.Z = float.Parse(multi_value[2]);
                        dimensions.Add(dim);
                        break;

                    case "scl ":
                        Vector3 scl;
                        scl.X = float.Parse(multi_value[0]);
                        scl.Y = float.Parse(multi_value[1]);
                        scl.Z = float.Parse(multi_value[2]);
                        scales.Add(scl);
                        break;

                    case "flg ":
                        bool[] temp_flags = new bool[multi_value.Length];
                        for (int i = 0; i < multi_value.Length; i++)
                        {
                            temp_flags[i] = bool.Parse(multi_value[i]);
                        }
                        flags.Add(temp_flags);
                        break;

                    case "atr ":
                        float[] temp_attributes = new float[multi_value.Length];
                        for (int i = 0; i < multi_value.Length; i++)
                        {
                            temp_attributes[i] = float.Parse(multi_value[i]);
                        }
                        attributes.Add(temp_attributes);
                        break;

                    case "num ":
                        num_bodies = int.Parse(single_value);
                        break;
                    }
                }
            }

            sr.Close();

            Debug.DebugHelper.logInfo(2, "\tNumber of Physics Objects", num_bodies.ToString());



            for (int i = 0; i < num_bodies; i++)
            {
                string mesh_id    = mesh_ids[i].Replace('.', '_');
                string physics_id = ids[i].Replace('.', '_');

                UniqueMesh temp_unique_mesh;
                if (meshes.TryGetValue(physics_id, out temp_unique_mesh))
                {
                    Vector3 position  = positions[i];
                    Vector3 rotation  = rotations[i];
                    Vector3 scale     = scales[i];
                    Vector3 dimension = dimensions[i] / 2.0f;

                    bool[] flags_list = flags[i];
                    bool   dynamic    = flags_list[0];
                    bool   kinematic  = flags_list[1];

                    float[] attributes_list = attributes[i];
                    float   mass            = attributes_list[0];
                    float   friction        = attributes_list[1];
                    float   restitution     = attributes_list[2];


                    // Build transformation matrix
                    Matrix temp_matrix = EngineHelper.otk2bullet(
                        EngineHelper.blender2Kailash(
                            EngineHelper.createMatrix(
                                EngineHelper.bullet2otk(position),
                                EngineHelper.bullet2otk(rotation),
                                new OpenTK.Vector3(1.0f)
                                )));


                    CollisionShape shape;
                    string         collision_shape = shapes[i];
                    float          radius;
                    switch (collision_shape)
                    {
                    case "BOX":
                        shape = new BoxShape(dimension);
                        break;

                    case "SPHERE":
                        radius = (dimension.X + dimension.Y + dimension.Z) / 3.0f;
                        shape  = new SphereShape(radius);
                        break;

                    case "CONE":
                        radius = (dimension.X + dimension.Z) / 4.0f;
                        shape  = new ConeShape(radius, dimension.Y);
                        break;

                    case "CYLINDER":
                        shape = new CylinderShape(dimension);
                        break;

                    case "CONVEX_HULL":
                        shape = loadConvexHull(temp_unique_mesh.mesh, scale);
                        break;

                    case "MESH":
                        shape = loadConcaveMesh(temp_unique_mesh.mesh, scale);
                        break;

                    default:
                        //numBodies--;
                        Debug.DebugHelper.logError("\t" + collision_shape + " is not a supported collision shape", mesh_id);
                        continue;
                    }


                    RigidBodyObject rigid_body_object = PhysicsHelper.createLocalRigidBody(
                        physics_id,
                        physics_world,
                        dynamic, kinematic,
                        mass, restitution, friction,
                        temp_matrix,
                        shape, dimension, scale);

                    temp_unique_mesh.physics_object = rigid_body_object;
                }
            }


            ids.Clear();
            mesh_ids.Clear();
            shapes.Clear();
            positions.Clear();
            rotations.Clear();
            dimensions.Clear();
            scales.Clear();
            flags.Clear();
            attributes.Clear();
        }
Example #2
0
        public static RigidBodyObject createLocalRigidBody(
            string id,
            PhysicsWorld physics_world,
            bool dynamic, bool kinematic,
            float mass, float restitution, float friction,
            Matrix startTransform,
            CollisionShape shape, Vector3 dimensions, Vector3 scale)
        {
            Vector3 localInertia = Vector3.Zero;

            if (dynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }
            else
            {
                mass = 0.0f;
            }

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);


            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);

            rbInfo.Restitution = restitution;


            //rbInfo.LinearDamping = 0.8f;
            //rbInfo.AngularDamping = 0.1f;

            //rbInfo.LinearSleepingThreshold = rbInfo.LinearSleepingThreshold * 1000.0f;
            //rbInfo.AngularSleepingThreshold = rbInfo.AngularSleepingThreshold * 1000.0f;

            rbInfo.Friction        = friction;
            rbInfo.RollingFriction = 0.01f;


            RigidBody body = new RigidBody(rbInfo);

            rbInfo.Dispose();

            if (kinematic)
            {
                body.ActivationState = ActivationState.DisableDeactivation;
                body.CollisionFlags  = CollisionFlags.KinematicObject;
            }


            body.SetSleepingThresholds(0.2f, 0.9f);
            //body.Friction = 1.0f;
            //body.RollingFriction = 1.0f;


            //------------------------------------------------------
            // Create a rigid body object and all to world
            //------------------------------------------------------

            body.UserObject = id;
            RigidBodyObject rigid_body_object = new RigidBodyObject(id, body, scale, kinematic);

            physics_world.rigid_body_objects.Add(rigid_body_object);
            physics_world.collision_shapes.Add(shape);
            physics_world.world.AddRigidBody(body);


            return(rigid_body_object);
        }