Exemplo n.º 1
0
    void Start()
    {
        _body            = GetComponent <ArticulationBody>();
        _parentBody      = _body.transform.parent.GetComponentInParent <ArticulationBody>();
        _debugController = FindObjectOfType <MarathonTestBedController>();
        _spawnableEnv    = GetComponentInParent <MLAgents.SpawnableEnv>();
        _mocapController = _spawnableEnv.GetComponentInChildren <MocapController>();
        var mocapBodyParts = _mocapController.GetComponentsInChildren <Rigidbody>().ToList();

        _target = mocapBodyParts.First(x => x.name == _body.name);
    }
Exemplo n.º 2
0
        /// <summary>
        /// Spawn a number of environments. The enviromentment must include SpawnableEnv
        /// </summary>
        public void SpawnSpawnableEnv(GameObject parent, int numInstances, GameObject envPrefab)
        {
            CreateSceneParameters csp = new CreateSceneParameters(LocalPhysicsMode.Physics3D);

            Vector3      spawnStartPos = parent.transform.position;
            SpawnableEnv spawnableEnv  = envPrefab.GetComponent <SpawnableEnv>();

            spawnableEnv.UpdateBounds();
            Vector3 step = new Vector3(0f, 0f, spawnableEnv.bounds.size.z + (spawnableEnv.bounds.size.z * spawnableEnv.paddingBetweenEnvs));

            if (spawnableEnv.CreateUniquePhysicsScene)
            {
                step = Vector3.zero;
            }

            for (int i = 0; i < numInstances; i++)
            {
                var agent = Agent.Instantiate(envPrefab, spawnStartPos, envPrefab.gameObject.transform.rotation);
                spawnStartPos += step;
                if (spawnableEnv.CreateUniquePhysicsScene)
                {
                    Scene        scene        = SceneManager.CreateScene($"SpawnedEnv-{i}", csp);
                    PhysicsScene physicsScene = scene.GetPhysicsScene();
                    SceneManager.MoveGameObjectToScene(agent, scene);
                    SpawnableEnv spawnedEnv = agent.GetComponent <SpawnableEnv>();
                    spawnedEnv.SetSceneAndPhysicsScene(scene, physicsScene);
                    // only render the 1st scene
                    if (i == 0)
                    {
                        // var cam = Camera.FindObjectOfType<Camera>();
                        // cam.scene = scene;
                        // // Camera.main.enabled = false;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override void AgentReset()
        {
            if (DistanceTraveled != float.MinValue)
            {
                var scorer = FindObjectOfType <Scorer>();
                scorer?.ReportScore(DistanceTraveled, "Distance Traveled");
            }
            if (_spawnableEnv == null)
            {
                _spawnableEnv = GetComponentInParent <SpawnableEnv>();
            }
            if (marathonSpawner == null)
            {
                marathonSpawner = GetComponent <MarathonSpawner>();
            }

            mphBuffer = new List <Vector3>();

            Transform[] allChildren = GetComponentsInChildren <Transform>();
            if (_hasValidModel)
            {
                // restore
                foreach (Transform child in allChildren)
                {
                    if (child.gameObject.name.Contains("OpenAIHumanoid"))
                    {
                        continue;
                    }

                    child.position = transformsPosition[child.gameObject];
                    child.rotation = transformsRotation[child.gameObject];
                    var childRb = child.GetComponent <Rigidbody>();
                    if (childRb != null)
                    {
                        childRb.angularVelocity = Vector3.zero;
                        childRb.velocity        = Vector3.zero;
                    }
                }

                marathonSpawner?.ApplyRandom();
                SetupMarathon();
                UpdateQ();
                return;
            }
            startPosition = transform.position;
            // HACK first spawned marathon agent should grab the camera
            var agentWithCamera = FindObjectsOfType <MarathonAgent>().FirstOrDefault(x => x.CameraTarget != null);

            if (agentWithCamera == null)
            {
                CameraTarget = FindObjectOfType <SmoothFollow>()?.gameObject;
                ShowMonitor  = true;
            }

            MarathonJoints  = null;
            MarathonSensors = null;
            var rbs = this.GetComponentsInChildren <Rigidbody>().ToList();

            foreach (var item in rbs)
            {
                if (item != null)
                {
                    DestroyImmediate(item.gameObject);
                }
            }

            Resources.UnloadUnusedAssets();

            marathonSpawner?.SpawnFromXml();
            allChildren        = GetComponentsInChildren <Transform>();
            transformsPosition = new Dictionary <GameObject, Vector3>();
            transformsRotation = new Dictionary <GameObject, Quaternion>();
            foreach (Transform child in allChildren)
            {
                transformsPosition[child.gameObject] = child.position;
                transformsRotation[child.gameObject] = child.rotation;
            }

            marathonSpawner?.ApplyRandom();
            SetupMarathon();
            UpdateQ();
            _hasValidModel = true;
            recentVelocity = new List <float>();
        }
Exemplo n.º 4
0
 /// <summary>
 /// Monobehavior function that dictates each environment step.
 /// </summary>
 void FixedUpdate()
 {
     EnvironmentStep();
     SpawnableEnv.TriggerPhysicsStep();
 }