Exemplo n.º 1
0
    bool LoadRobot(string directory)
    {
        robotObject = new GameObject("Robot");
        robotObject.transform.position = robotStartPosition;

        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };

        List <RigidNode_Base> nodes = new List <RigidNode_Base>();

        rootNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        rootNode.ListAllNodes(nodes);

        foreach (RigidNode_Base n in nodes)
        {
            RigidNode node = (RigidNode)n;
            node.CreateTransform(robotObject.transform);

            if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
            {
                Debug.Log("Robot not loaded!");
                Destroy(robotObject);
                return(false);
            }

            node.CreateJoint();
        }

        return(true);
    }
Exemplo n.º 2
0
        // TODO: Something weird is going on with the spawn, at least with robots with manipulators. Reset is fine.

        /// <summary>
        /// Loads and initializes the physical manipulator object (used in Mix and Match mode)
        /// </summary>
        /// <param name="directory">Folder directory of the manipulator</param>
        /// <param name="robotGameObject">GameObject of the robot the manipulator will be attached to</param>
        public bool InitializeManipulator(string directory)
        {
            ManipulatorObject = new GameObject("Manipulator");
            ManipulatorObject.transform.position = robotStartPosition + manipulatorOffset;

            RigidNode_Base.NODE_FACTORY = delegate(Guid guid) { return(new RigidNode(guid)); };

            List <RigidNode_Base> nodes = new List <RigidNode_Base>();

            //TO-DO: Read .robot instead (from the new exporters if they are implemented). Maybe need a RobotSkeleton class

            manipulatorNode = BXDExtensions.ReadSkeletonSafe(directory + Path.DirectorySeparatorChar + "skeleton") as RigidNode;

            manipulatorNode.ListAllNodes(nodes);

            //Load node_0 for attaching manipulator to robot
            RigidNode node = (RigidNode)nodes[0];

            node.CreateTransform(ManipulatorObject.transform);
            if (!node.CreateMesh(directory + Path.DirectorySeparatorChar + node.ModelFileName))
            {
                Destroy(ManipulatorObject);
                return(false);
            }

            node.CreateManipulatorJoint(gameObject);
            node.MainObject.AddComponent <Tracker>().Trace = true;

            //Load other nodes associated with the manipulator
            for (int i = 1; i < nodes.Count; i++)
            {
                RigidNode otherNode = (RigidNode)nodes[i];
                otherNode.CreateTransform(ManipulatorObject.transform);

                if (!otherNode.CreateMesh(directory + Path.DirectorySeparatorChar + otherNode.ModelFileName))
                {
                    Destroy(ManipulatorObject);
                    return(false);
                }

                otherNode.MainObject.AddComponent <Tracker>().Trace = true;
            }

            RootNode.GenerateWheelInfo();

            for (int i = 1; i < nodes.Count; i++)
            {
                RigidNode otherNode = (RigidNode)nodes[i];
                otherNode.CreateJoint(this);
            }

            RotateRobot(robotStartOrientation);
            RobotHasManipulator = true;

            return(true);
        }
Exemplo n.º 3
0
    public bool LoadManipulator(string directory)
    {
        manipulatorObject = new GameObject("Manipulator");

        //Set the manipulator transform to match with the position of node_0 of the robot. THIS ONE ACTUALLY DOES SOMETHING:
        manipulatorObject.transform.position = GameObject.Find("Robot").transform.GetChild(0).transform.position;
        //manipulatorObject.transform.position = robotStartPosition;

        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };

        List <RigidNode_Base> nodes = new List <RigidNode_Base>();

        //TO-DO: Read .robot instead (from the new exporters if they are implemented). Maybe need a RobotSkeleton class
        manipulatorNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        manipulatorNode.ListAllNodes(nodes);

        //Load node_0 for attaching manipulator to robot
        RigidNode node = (RigidNode)nodes[0];

        node.CreateTransform(manipulatorObject.transform);
        if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
        {
            Debug.Log("Robot not loaded!");
            UnityEngine.Object.Destroy(manipulatorObject);
            return(false);
        }
        node.CreateManipulatorJoint();
        node.MainObject.AddComponent <Tracker>().Trace = true;
        Tracker t = node.MainObject.GetComponent <Tracker>();

        Debug.Log(t);

        //Load other nodes associated with the manipulator
        for (int i = 1; i < nodes.Count; i++)
        {
            RigidNode otherNode = (RigidNode)nodes[i];
            otherNode.CreateTransform(manipulatorObject.transform);
            if (!otherNode.CreateMesh(directory + "\\" + otherNode.ModelFileName))
            {
                Debug.Log("Robot not loaded!");
                UnityEngine.Object.Destroy(manipulatorObject);
                return(false);
            }
            otherNode.CreateJoint();
            otherNode.MainObject.AddComponent <Tracker>().Trace = true;
            t = otherNode.MainObject.GetComponent <Tracker>();
            Debug.Log(t);
        }

        RotateRobot(robotStartOrientation);
        return(true);
    }
Exemplo n.º 4
0
    /// <summary>
    /// Initializes physical robot based off of robot directory.
    /// </summary>
    /// <param name="directory">folder directory of robot</param>
    /// <returns></returns>
    public bool InitializeRobot(string directory, MainState source)
    {
        //Deletes all nodes if any exist
        int childCount = transform.childCount;

        for (int i = 0; i < childCount; ++i)
        {
            Destroy(transform.GetChild(i).gameObject);
        }

        mainState          = source;
        transform.position = robotStartPosition;

        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };

        List <RigidNode_Base> nodes = new List <RigidNode_Base>();

        //Read .robot instead. Maybe need a RobotSkeleton class
        rootNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        rootNode.ListAllNodes(nodes);

        foreach (RigidNode_Base n in nodes)
        {
            RigidNode node = (RigidNode)n;
            node.CreateTransform(transform);

            if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
            {
                Debug.Log("Robot not loaded!");
                return(false);
            }

            node.CreateJoint();

            node.MainObject.AddComponent <Tracker>().Trace = true;
        }

        RotateRobot(robotStartOrientation);

        RobotName = new DirectoryInfo(directory).Name;

        isInitialized = true;

        robotCamera = GameObject.Find("RobotCameraList").GetComponent <RobotCamera>();
        //Attached to the main frame and face the front
        robotCamera.AddCamera(transform.GetChild(0).transform);
        //Attached to the first node and face the front
        robotCamera.AddCamera(transform.GetChild(1).transform);
        ////Attached to main frame and face the back
        robotCamera.AddCamera(transform.GetChild(0).transform, new Vector3(0, 0, 0), new Vector3(0, 180, 0));
        return(true);
    }
Exemplo n.º 5
0
    /// <summary>
    /// Loads and initializes the physical manipulator object (used in Mix and Match mode)
    /// </summary>
    /// <param name="directory">Folder directory of the manipulator</param>
    /// <param name="robotGameObject">GameObject of the robot the manipulator will be attached to</param>
    public bool InitializeManipulator(string directory, GameObject robotGameObject)
    {
        if (robotGameObject == null)
        {
            robotGameObject = GameObject.Find("Robot");
        }
        ManipulatorObject = new GameObject("Manipulator");

        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };

        List <RigidNode_Base> nodes = new List <RigidNode_Base>();

        //TO-DO: Read .robot instead (from the new exporters if they are implemented). Maybe need a RobotSkeleton class
        manipulatorNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        manipulatorNode.ListAllNodes(nodes);

        int   numWheels      = nodes.Count(x => x.HasDriverMeta <WheelDriverMeta>() && x.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL);
        float collectiveMass = 0f;

        //Load node_0 for attaching manipulator to robot
        RigidNode node = (RigidNode)nodes[0];

        node.CreateTransform(ManipulatorObject.transform);
        if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
        {
            Debug.Log("Robot not loaded!");
            UnityEngine.Object.Destroy(ManipulatorObject);
            return(false);
        }
        GameObject robot = robotGameObject;

        //Set the manipulator transform to match with the position of node_0 of the robot. THIS ONE ACTUALLY DOES SOMETHING: LIKE ACTUALLY



        Vector3 manipulatorTransform = robotStartPosition + offset;

        Debug.Log("Node Com Offset" + node.ComOffset);
        ManipulatorObject.transform.position = manipulatorTransform;

        node.CreateManipulatorJoint(robot);
        node.MainObject.AddComponent <Tracker>().Trace = true;
        Tracker t = node.MainObject.GetComponent <Tracker>();

        Debug.Log(t);

        //Load other nodes associated with the manipulator
        for (int i = 1; i < nodes.Count; i++)
        {
            RigidNode otherNode = (RigidNode)nodes[i];
            otherNode.CreateTransform(ManipulatorObject.transform);
            if (!otherNode.CreateMesh(directory + "\\" + otherNode.ModelFileName))
            {
                Debug.Log("Robot not loaded!");
                UnityEngine.Object.Destroy(ManipulatorObject);
                return(false);
            }
            otherNode.CreateJoint(numWheels, RobotIsMixAndMatch);
            otherNode.MainObject.AddComponent <Tracker>().Trace = true;
            t = otherNode.MainObject.GetComponent <Tracker>();
            Debug.Log(t);
        }

        foreach (BRaycastRobot r in ManipulatorObject.GetComponentsInChildren <BRaycastRobot>())
        {
            r.RaycastRobot.OverrideMass = collectiveMass;
        }

        RotateRobot(robotStartOrientation);

        this.RobotHasManipulator = true;
        return(true);
    }
Exemplo n.º 6
0
    /// <summary>
    /// Initializes physical robot based off of robot directory.
    /// </summary>
    /// <param name="directory">folder directory of robot</param>
    /// <returns></returns>
    public bool InitializeRobot(string directory, MainState source)
    {
        RobotIsMecanum = false;

        if (RobotIsMixAndMatch)
        {
            wheelPath            = RobotTypeManager.WheelPath;
            wheelFriction        = RobotTypeManager.WheelFriction;
            wheelLateralFriction = RobotTypeManager.WheelLateralFriction;
            wheelRadius          = RobotTypeManager.WheelRadius;
            wheelMass            = RobotTypeManager.WheelMass;

            RobotIsMecanum = RobotTypeManager.IsMecanum;
        }

        #region Robot Initialization
        RobotDirectory = directory;

        //Deletes all nodes if any exist, take the old node transforms out from the robot object
        int childCount = transform.childCount;
        for (int i = childCount - 1; i >= 0; i--)
        {
            Transform child = transform.GetChild(i);

            //If this isn't done, the game object is destroyed but the parent-child transform relationship remains!
            child.parent = null;
            Destroy(child.gameObject);
        }

        //Detach and destroy all sensors on the original robot
        SensorManager sensorManager = GameObject.Find("SensorManager").GetComponent <SensorManager>();
        sensorManager.ResetSensorLists();



        //Removes Driver Practice component if it exists
        if (dpmRobot != null)
        {
            Destroy(dpmRobot);
        }

        mainState = source;                      //stores the main state object

        transform.position = robotStartPosition; //Sets the position of the object to the set spawn point

        if (!File.Exists(directory + "\\skeleton.bxdj"))
        {
            return(false);
        }

        //Loads the node and skeleton data
        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };
        List <RigidNode_Base> nodes = new List <RigidNode_Base>();
        rootNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        rootNode.ListAllNodes(nodes);

        //Initializes the wheel variables
        int   numWheels      = nodes.Count(x => x.HasDriverMeta <WheelDriverMeta>() && x.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL);
        float collectiveMass = 0f;


        //Initializes the nodes and creates joints for the robot
        if (RobotIsMixAndMatch && !RobotIsMecanum) //If the user is in MaM and the robot they select is not mecanum, create the nodes and replace the wheel meshes to match those selected
        {
            //Load Node_0, the base of the robot
            RigidNode node = (RigidNode)nodes[0];
            node.CreateTransform(transform);

            if (!node.CreateMesh(directory + "\\" + node.ModelFileName, true, wheelMass))
            {
                Debug.Log("Robot not loaded!");
                return(false);
            }

            node.CreateJoint(numWheels, RobotIsMixAndMatch);

            if (node.PhysicalProperties != null)
            {
                collectiveMass += node.PhysicalProperties.mass;
            }

            if (node.MainObject.GetComponent <BRigidBody>() != null)
            {
                node.MainObject.AddComponent <Tracker>().Trace = true;
            }

            //Get the wheel mesh data from the file they are stored in. They are stored as .bxda files. This may need to update if exporters/file types change.
            BXDAMesh mesh = new BXDAMesh();
            mesh.ReadFromFile(wheelPath + "\\node_0.bxda");

            List <Mesh>       meshList     = new List <Mesh>();
            List <Material[]> materialList = new List <Material[]>();

            RigidNode wheelNode = (RigidNode)BXDJSkeleton.ReadSkeleton(wheelPath + "\\skeleton.bxdj");

            Material[] materials = new Material[0];
            AuxFunctions.ReadMeshSet(mesh.meshes, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu)
            {
                meshList.Add(meshu);

                materials = new Material[meshu.subMeshCount];
                for (int i = 0; i < materials.Length; i++)
                {
                    materials[i] = sub.surfaces[i].AsMaterial(true);
                }

                materialList.Add(materials);
            }, true);


            //Loads the other nodes from the original robot
            for (int i = 1; i < nodes.Count; i++)
            {
                node = (RigidNode)nodes[i];
                node.CreateTransform(transform);

                if (!node.CreateMesh(directory + "\\" + node.ModelFileName, true, wheelMass))
                {
                    Debug.Log("Robot not loaded!");
                    return(false);
                }

                //If the node is a wheel, destroy the original wheel mesh and replace it with the wheels selected in MaM
                if (node.HasDriverMeta <WheelDriverMeta>())
                {
                    int chldCount = node.MainObject.transform.childCount;
                    for (int j = 0; j < chldCount; j++)
                    {
                        Destroy(node.MainObject.transform.GetChild(j).gameObject);
                    }

                    int k = 0;

                    Vector3?offset = null;
                    foreach (Mesh meshObject in meshList)
                    {
                        GameObject meshObj = new GameObject(node.MainObject.name + "_mesh");
                        meshObj.transform.parent = node.MainObject.transform;
                        meshObj.AddComponent <MeshFilter>().mesh = meshObject;
                        if (!offset.HasValue)
                        {
                            offset = meshObject.bounds.center;
                        }
                        meshObj.transform.localPosition = -offset.Value;

                        //Take out this line if you want some snazzy pink wheels
                        meshObj.AddComponent <MeshRenderer>().materials = materialList[k];

                        k++;
                    }
                    node.MainObject.GetComponentInChildren <MeshRenderer>().materials = materials;
                }

                //Create the joints that interact with physics
                node.CreateJoint(numWheels, RobotIsMixAndMatch, wheelFriction, wheelLateralFriction);

                if (node.HasDriverMeta <WheelDriverMeta>())
                {
                    node.MainObject.GetComponent <BRaycastWheel>().Radius = wheelRadius;
                }

                if (node.PhysicalProperties != null)
                {
                    collectiveMass += node.PhysicalProperties.mass;
                }

                if (node.MainObject.GetComponent <BRigidBody>() != null)
                {
                    node.MainObject.AddComponent <Tracker>().Trace = true;
                }
            }
        }
        else //Initialize the robot as normal
        {
            //Initializes the nodes
            foreach (RigidNode_Base n in nodes)
            {
                RigidNode node = (RigidNode)n;
                node.CreateTransform(transform);

                if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
                {
                    Debug.Log("Robot not loaded!");
                    return(false);
                }

                node.CreateJoint(numWheels, RobotIsMixAndMatch);

                if (node.PhysicalProperties != null)
                {
                    collectiveMass += node.PhysicalProperties.mass;
                }

                if (node.MainObject.GetComponent <BRigidBody>() != null)
                {
                    node.MainObject.AddComponent <Tracker>().Trace = true;
                }
            }
        }

        #endregion

        //Get the offset from the first node to the robot for new robot start position calculation
        //This line is CRITICAL to new reset position accuracy! DON'T DELETE IT!
        nodeToRobotOffset = gameObject.transform.GetChild(0).localPosition - robotStartPosition;

        foreach (BRaycastRobot r in GetComponentsInChildren <BRaycastRobot>())
        {
            r.RaycastRobot.OverrideMass  = collectiveMass;
            r.RaycastRobot.RootRigidBody = (RigidBody)((RigidNode)nodes[0]).MainObject.GetComponent <BRigidBody>().GetCollisionObject();
        }

        RotateRobot(robotStartOrientation);

        RobotName = new DirectoryInfo(directory).Name;

        isInitialized = true;

        //Initializes Driver Practice component
        dpmRobot = gameObject.AddComponent <DriverPracticeRobot>();
        dpmRobot.Initialize(directory);

        //Initializing robot cameras
        bool hasRobotCamera = false;
        //If you are getting an error referencing this line, it is likely that the Game Object "RobotCameraList" in Scene.unity does not have the RobotCameraManager script attached to it.
        robotCameraManager = GameObject.Find("RobotCameraList").GetComponent <RobotCameraManager>();

        //Loop through robotCameraList and check if any existing camera should attach to this robot
        foreach (GameObject robotCamera in robotCameraManager.GetRobotCameraList())
        {
            if (robotCamera.GetComponent <RobotCamera>().robot.Equals(this))
            {
                //Recover the robot camera configurations
                robotCamera.GetComponent <RobotCamera>().RecoverConfiguration();
                hasRobotCamera = true;
            }
        }
        //Add new cameras to the robot if there is none robot camera belong to the current robot (which means it is a new robot)
        if (!hasRobotCamera)
        {
            //Attached to the main frame and face the front
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform, new Vector3(0, 0.5f, 0), new Vector3(0, 0, 0));
            ////Attached to main frame and face the back
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform, new Vector3(0, 0.5f, 0), new Vector3(0, 180, 0));
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform);
        }

        //Reads the offset position for the manipulator
        if (RobotIsMixAndMatch)
        {
            offset = Vector3.zero;
            try
            {
                using (TextReader reader = File.OpenText(directory + "\\position.txt"))
                {
                    offset.x = float.Parse(reader.ReadLine());
                    offset.y = float.Parse(reader.ReadLine());
                    offset.z = float.Parse(reader.ReadLine());
                }
            } catch
            {
                offset = Vector3.zero;
            }
        }

        return(true);
    }
Exemplo n.º 7
0
        /// <summary>
        /// Generates the robot from the list of <see cref="RigidNode_Base"/>s and the
        /// number of wheels, and updates the collective mass.
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="numWheels"></param>
        /// <param name="collectiveMass"></param>
        /// <returns></returns>
        protected override bool ConstructRobot(List <RigidNode_Base> nodes, ref float collectiveMass)
        {
            if (IsMecanum())
            {
                return(base.ConstructRobot(nodes, ref collectiveMass));
            }

            //Load Node_0, the base of the robot
            RigidNode node = (RigidNode)nodes[0];

            node.CreateTransform(transform);

            if (!node.CreateMesh(RobotDirectory + "\\" + node.ModelFileName, true, wheelMass))
            {
                return(false);
            }

            node.CreateJoint(this);

            if (node.PhysicalProperties != null)
            {
                collectiveMass += node.PhysicalProperties.mass;
            }

            if (node.MainObject.GetComponent <BRigidBody>() != null)
            {
                node.MainObject.AddComponent <Tracker>().Trace = true;
            }

            //Get the wheel mesh data from the file they are stored in. They are stored as .bxda files. This may need to update if exporters/file types change.
            BXDAMesh mesh = new BXDAMesh();

            mesh.ReadFromFile(wheelPath + "\\node_0.bxda");

            List <Mesh>       meshList     = new List <Mesh>();
            List <Material[]> materialList = new List <Material[]>();

            RigidNode wheelNode = (RigidNode)BXDJSkeleton.ReadSkeleton(wheelPath + "\\skeleton.bxdj");

            Material[] materials = new Material[0];
            Auxiliary.ReadMeshSet(mesh.meshes, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu)
            {
                meshList.Add(meshu);

                materials = new Material[meshu.subMeshCount];

                for (int i = 0; i < materials.Length; i++)
                {
                    materials[i] = sub.surfaces[i].AsMaterial(true);
                }

                materialList.Add(materials);
            }, true);

            //Loads the other nodes from the original robot
            for (int i = 1; i < nodes.Count; i++)
            {
                node = (RigidNode)nodes[i];
                node.CreateTransform(transform);

                if (!node.CreateMesh(RobotDirectory + "\\" + node.ModelFileName, true, wheelMass))
                {
                    return(false);
                }

                //If the node is a wheel, destroy the original wheel mesh and replace it with the wheels selected in MaM
                if (node.HasDriverMeta <WheelDriverMeta>())
                {
                    int chldCount = node.MainObject.transform.childCount;

                    for (int j = 0; j < chldCount; j++)
                    {
                        Destroy(node.MainObject.transform.GetChild(j).gameObject);
                    }

                    int k = 0;

                    Vector3?offset = null;
                    foreach (Mesh meshObject in meshList)
                    {
                        GameObject meshObj = new GameObject(node.MainObject.name + "_mesh");
                        meshObj.transform.parent = node.MainObject.transform;
                        meshObj.AddComponent <MeshFilter>().mesh = meshObject;

                        if (!offset.HasValue)
                        {
                            offset = meshObject.bounds.center;
                        }

                        meshObj.transform.localPosition = -offset.Value;

                        //Take out this line if you want some snazzy pink wheels
                        meshObj.AddComponent <MeshRenderer>().materials = materialList[k];

                        k++;
                    }
                    node.MainObject.GetComponentInChildren <MeshRenderer>().materials = materials;
                }
            }

            RootNode.GenerateWheelInfo();

            //Create the joints that interact with physics
            for (int i = 1; i < nodes.Count; i++)
            {
                node = (RigidNode)nodes[i];
                node.CreateJoint(this, wheelFriction, wheelLateralFriction);

                if (node.HasDriverMeta <WheelDriverMeta>())
                {
                    node.MainObject.GetComponent <BRaycastWheel>().Radius = wheelRadius;
                }

                if (node.PhysicalProperties != null)
                {
                    collectiveMass += node.PhysicalProperties.mass;
                }

                if (node.MainObject.GetComponent <BRigidBody>() != null)
                {
                    node.MainObject.AddComponent <Tracker>().Trace = true;
                }
            }

            return(true);
        }
Exemplo n.º 8
0
    /// <summary>
    /// Initializes physical robot based off of robot directory.
    /// </summary>
    /// <param name="directory">folder directory of robot</param>
    /// <returns></returns>
    public bool InitializeRobot(string directory, MainState source)
    {
        #region Robot Initialization
        RobotDirectory = directory;

        //Deletes all nodes if any exist, take the old node transforms out from the robot object
        int childCount = transform.childCount;
        for (int i = childCount - 1; i >= 0; i--)
        {
            Transform child = transform.GetChild(i);

            //If this isn't done, the game object is destroyed but the parent-child transform relationship remains!
            child.parent = null;
            Destroy(child.gameObject);
        }

        //Detach and destroy all sensors on the original robot
        SensorManager sensorManager = GameObject.Find("SensorManager").GetComponent <SensorManager>();
        sensorManager.ResetSensorLists();

        //Removes Driver Practice component if it exists
        if (dpmRobot != null)
        {
            Destroy(dpmRobot);
        }

        mainState = source;                      //stores the main state object

        transform.position = robotStartPosition; //Sets the position of the object to the set spawn point

        if (!File.Exists(directory + "\\skeleton.bxdj"))
        {
            return(false);
        }

        //Loads the node and skeleton data
        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };
        List <RigidNode_Base> nodes = new List <RigidNode_Base>();
        rootNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        rootNode.ListAllNodes(nodes);

        //Initializes the wheel variables
        int   numWheels      = nodes.Count(x => x.HasDriverMeta <WheelDriverMeta>() && x.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL);
        float collectiveMass = 0f;

        int isMixAndMatch = PlayerPrefs.GetInt("mixAndMatch");
        if (isMixAndMatch == 1 && !MixAndMatchMode.isMecanum)
        {
            //Load Node_0
            RigidNode node = (RigidNode)nodes[0];
            node.CreateTransform(transform);

            if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
            {
                Debug.Log("Robot not loaded!");
                return(false);
            }

            node.CreateJoint(numWheels);

            if (node.PhysicalProperties != null)
            {
                collectiveMass += node.PhysicalProperties.mass;
            }

            if (node.MainObject.GetComponent <BRigidBody>() != null)
            {
                node.MainObject.AddComponent <Tracker>().Trace = true;
            }

            //Load the other nodes (wheels)
            string   wheelDirectory = PlayerPrefs.GetString("simSelectedWheel");
            BXDAMesh mesh           = new BXDAMesh();
            mesh.ReadFromFile(wheelDirectory + "\\node_0.bxda");

            List <Mesh>       meshList     = new List <Mesh>();
            List <Material[]> materialList = new List <Material[]>();

            RigidNode wheelNode = (RigidNode)BXDJSkeleton.ReadSkeleton(wheelDirectory + "\\skeleton.bxdj");

            Material[] materials = new Material[0];
            AuxFunctions.ReadMeshSet(mesh.meshes, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu)
            {
                meshList.Add(meshu);

                materials = new Material[meshu.subMeshCount];
                for (int i = 0; i < materials.Length; i++)
                {
                    materials[i] = sub.surfaces[i].AsMaterial(true);
                }

                materialList.Add(materials);
                //meshObject.GetComponent<MeshRenderer>().materials = materials;
            }, true);

            for (int i = 1; i < nodes.Count; i++)
            {
                node = (RigidNode)nodes[i];
                node.CreateTransform(transform);

                if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
                {
                    Debug.Log("Robot not loaded!");
                    return(false);
                }

                if (node.HasDriverMeta <WheelDriverMeta>())
                {
                    int chldCount = node.MainObject.transform.childCount;
                    for (int j = 0; j < chldCount; j++)
                    {
                        Destroy(node.MainObject.transform.GetChild(j).gameObject);
                    }

                    int k = 0;
                    foreach (Mesh meshObject in meshList)
                    {
                        Debug.Log("Mesh Object" + meshObject);
                        GameObject meshObj = new GameObject(node.MainObject.name + "_mesh");
                        meshObj.transform.parent = node.MainObject.transform;
                        meshObj.AddComponent <MeshFilter>().mesh = meshObject;
                        meshObj.transform.localPosition          = -meshObject.bounds.center;

                        //Take out this line if you want some snazzy pink wheels
                        meshObj.AddComponent <MeshRenderer>().materials = materialList[k];
                        k++;
                    }
                    node.MainObject.GetComponentInChildren <MeshRenderer>().materials = materials;
                }

                //node.MainObject.transform.GetChild(0).localPosition = -node.MainObject.GetComponentInChildren<MeshFilter>().mesh.bounds.center;// -node.MainObject.transform.localPosition;
                //Bounds b = node.MainObject.GetComponentInChildren<MeshFilter>().mesh.bounds;
                // Debug.Log(b.center);
                //b.center = node.MainObject.transform.position;
                //node.MainObject.GetComponentInChildren<MeshFilter>().mesh.bounds = b;

                node.CreateJoint(numWheels);

                if (node.HasDriverMeta <WheelDriverMeta>())
                {
                    float radius = PlayerPrefs.GetFloat("wheelRadius");
                    node.MainObject.GetComponent <BRaycastWheel>().Radius = radius;
                }

                if (node.PhysicalProperties != null)
                {
                    collectiveMass += node.PhysicalProperties.mass;
                }

                if (node.MainObject.GetComponent <BRigidBody>() != null)
                {
                    node.MainObject.AddComponent <Tracker>().Trace = true;
                }
            }
        }
        else
        {
            //Initializes the nodes
            foreach (RigidNode_Base n in nodes)
            {
                RigidNode node = (RigidNode)n;
                node.CreateTransform(transform);

                if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
                {
                    Debug.Log("Robot not loaded!");
                    return(false);
                }

                node.CreateJoint(numWheels);

                if (node.PhysicalProperties != null)
                {
                    collectiveMass += node.PhysicalProperties.mass;
                }

                if (node.MainObject.GetComponent <BRigidBody>() != null)
                {
                    node.MainObject.AddComponent <Tracker>().Trace = true;
                }
            }
        }

        #endregion


        //Get the offset from the first node to the robot for new robot start position calculation
        //This line is CRITICAL to new reset position accuracy! DON'T DELETE IT!
        nodeToRobotOffset = gameObject.transform.GetChild(0).localPosition - robotStartPosition;

        foreach (BRaycastRobot r in GetComponentsInChildren <BRaycastRobot>())
        {
            r.RaycastRobot.OverrideMass  = collectiveMass;
            r.RaycastRobot.RootRigidBody = (RigidBody)((RigidNode)nodes[0]).MainObject.GetComponent <BRigidBody>().GetCollisionObject();
        }

        RotateRobot(robotStartOrientation);

        RobotName = new DirectoryInfo(directory).Name;

        isInitialized = true;

        //Initializes Driver Practice component
        dpmRobot = gameObject.AddComponent <DriverPracticeRobot>();
        dpmRobot.Initialize(directory);

        //Initializing robot cameras
        bool hasRobotCamera = false;
        //If you are getting an error referencing this line, it is likely that the Game Object "RobotCameraList" in Scene.unity does not have the RobotCameraManager script attached to it.
        robotCameraManager = GameObject.Find("RobotCameraList").GetComponent <RobotCameraManager>();

        //Loop through robotCameraList and check if any existing camera should attach to this robot
        foreach (GameObject robotCamera in robotCameraManager.GetRobotCameraList())
        {
            if (robotCamera.GetComponent <RobotCamera>().robot.Equals(this))
            {
                //Recover the robot camera configurations
                robotCamera.GetComponent <RobotCamera>().RecoverConfiguration();
                hasRobotCamera = true;
            }
        }
        //Add new cameras to the robot if there is none robot camera belong to the current robot (which means it is a new robot)
        if (!hasRobotCamera)
        {
            //Attached to the main frame and face the front
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform);
            //Attached to the first node and face the front
            if (transform.childCount > 1)
            {
                robotCameraManager.AddCamera(this, transform.GetChild(1).transform);
            }
            ////Attached to main frame and face the back
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform, new Vector3(0, 0, 0), new Vector3(0, 180, 0));
        }

        return(true);
    }
Exemplo n.º 9
0
    /// <summary>
    /// Initializes physical robot based off of robot directory.
    /// </summary>
    /// <param name="directory">folder directory of robot</param>
    /// <returns></returns>
    public bool InitializeRobot(string directory, MainState source)
    {
        //Deletes all nodes if any exist, take the old node transforms out from the robot object
        int childCount = transform.childCount;

        for (int i = childCount - 1; i >= 0; i--)
        {
            Transform child = transform.GetChild(i);

            //If not do this the game object is destroyed but the parent-child transform relationship remains!
            child.parent = null;
            Destroy(child.gameObject);
        }
        SensorManager sensorManager = GameObject.Find("SensorManager").GetComponent <SensorManager>();

        sensorManager.ResetSensorLists();

        mainState          = source;
        transform.position = robotStartPosition;

        RigidNode_Base.NODE_FACTORY = delegate(Guid guid)
        {
            return(new RigidNode(guid));
        };

        List <RigidNode_Base> nodes = new List <RigidNode_Base>();

        //Read .robot instead. Maybe need a RobotSkeleton class
        rootNode = BXDJSkeleton.ReadSkeleton(directory + "\\skeleton.bxdj");
        rootNode.ListAllNodes(nodes);

        int   numWheels      = nodes.Count(x => x.HasDriverMeta <WheelDriverMeta>() && x.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL);
        float collectiveMass = 0f;

        foreach (RigidNode_Base n in nodes)
        {
            RigidNode node = (RigidNode)n;
            node.CreateTransform(transform);

            if (!node.CreateMesh(directory + "\\" + node.ModelFileName))
            {
                Debug.Log("Robot not loaded!");
                return(false);
            }

            node.CreateJoint(numWheels);

            if (node.PhysicalProperties != null)
            {
                collectiveMass += node.PhysicalProperties.mass;
            }

            if (node.MainObject.GetComponent <BRigidBody>() != null)
            {
                node.MainObject.AddComponent <Tracker>().Trace = true;
            }
        }

        foreach (BRaycastRobot r in GetComponentsInChildren <BRaycastRobot>())
        {
            r.RaycastRobot.EffectiveMass = collectiveMass;
        }

        RotateRobot(robotStartOrientation);

        RobotName = new DirectoryInfo(directory).Name;

        isInitialized = true;

        bool hasRobotCamera = false;

        robotCameraManager = GameObject.Find("RobotCameraList").GetComponent <RobotCameraManager>();

        foreach (GameObject robotCamera in robotCameraManager.GetRobotCameraList())
        {
            if (robotCamera.GetComponent <RobotCamera>().robot.Equals(this))
            {
                robotCamera.GetComponent <RobotCamera>().RecoverConfiguration();
                hasRobotCamera = true;
                Debug.Log(hasRobotCamera);
            }
        }
        if (!hasRobotCamera)
        {
            //Attached to the main frame and face the front
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform);
            //Attached to the first node and face the front
            if (transform.childCount > 1)
            {
                robotCameraManager.AddCamera(this, transform.GetChild(1).transform);
            }
            ////Attached to main frame and face the back
            robotCameraManager.AddCamera(this, transform.GetChild(0).transform, new Vector3(0, 0, 0), new Vector3(0, 180, 0));
        }

        //sensorManager.AddBeamBreaker(transform.GetChild(0).gameObject, new Vector3(0, 0, 1), new Vector3(0, 90, 0), 1);
        //sensorManager.AddUltrasonicSensor(transform.GetChild(0).gameObject, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
        //sensorManager.AddGyro(transform.GetChild(0).gameObject, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
        return(true);
    }