// Use this for initialization void Start() { agentIdx = this.transform.GetSiblingIndex(); GameObject gameController = GameObject.Find("GameController"); if (gameController != null) { WorldController worldController = gameController.GetComponent <WorldController> (); if (worldController != null) { world = worldController.world; vehicle_dt = world.vehicle.dt; // Check if this agent has the longest path and should update the timer simulationSpeedFactor = worldController.simulationSpeedFactor; motionModel = worldController.getMotionModel(); } GameObject agentsObj = GameObject.Find("Agents"); virtualStructure = agentsObj.GetComponent <VirtualStructure> (); if (virtualStructure != null) { agentHeight = virtualStructure.agentHeight; formationDecreasingGoalVelocity = virtualStructure.formationDecreasingGoalVelocity; Vector3 currentVelocity = new Vector3(world.currentVelocities [agentIdx - 1].x, 0f, world.currentVelocities [agentIdx - 1].y); lastPosInfo = new PointInfo(this.transform.position, currentVelocity, currentVelocity.normalized, 0f); nextPosInfo = getNextPosition(lastPosInfo, world, 1); boundingMinMaxes = VirtualStructure.getMinMaxes(world.boundingPolygon); } } }
IEnumerator UpdateStructurePos(VirtualStructure vs) { bool first = true; while (true) { vs.UpdatePos(); pos = vs.pos; int j = 0; foreach (PolyAgent agent in vs.agents) { if (agent.running) { agent.model.StopCoroutineMove(); } List <Vector3> path = new List <Vector3> (); path.Add(vs.pos[j]); agent.end = path[0]; agent.model.SetPath(path, agent, new List <Line> ()); agent.model.StartCoroutineMove(); j++; } first = false; yield return(new WaitForSeconds(updateSpeed)); } }
void Start() { // Choose model string modelType = "car"; string moveType = "dynamic"; vel = 5; updateSpeed = 1; List <PolyAgent> agents = new List <PolyAgent> (); numAgents = 4; for (int i = 0; i < numAgents; i++) { agents.Add(new PolyAgent(i + "", Vector3.zero, Vector3.zero, R, modelType)); agents [i].agent.renderer.material.color = Color.yellow; if (modelType == "point") { if (moveType == "dynamic") { agents[i].model = gameObject.AddComponent <DynamicPointModel> (); } if (moveType == "kinematic") { agents[i].model = gameObject.AddComponent <KinematicPointModel> (); } if (moveType == "differential") { agents[i].model = gameObject.AddComponent <DifferentialDriveModel>(); } } if (modelType == "car") { if (moveType == "dynamic") { agents[i].model = gameObject.AddComponent <DynamicCarModel> (); } if (moveType == "kinematic") { agents[i].model = gameObject.AddComponent <KinematicCarModel> (); } } } List <Vector3> start = new List <Vector3> (); start.Add(new Vector3(100, 1, 100)); start.Add(new Vector3(200, 1, 100)); start.Add(Vector3.zero); start.Add(new Vector3(0, 1, -100)); VirtualStructure vs = new VirtualStructure(start, agents); this.agents = vs.agents; // debug StartCoroutine(MoveStructure(vs)); StartCoroutine(UpdateStructurePos(vs)); }
IEnumerator MoveStructure(VirtualStructure vs) { int i = 0; while (i < vs.structureWP.Count) { vs.transform.position = Vector3.MoveTowards(vs.transform.position, vs.structureWP[i], vel * Time.deltaTime); if (Vector3.Distance(vs.transform.position, vs.structureWP[i]) < 5) { i = (i + 1) % vs.structureWP.Count; } //vs.UpdatePos (); yield return(null); } }
void Start() { Button btn = startButton.GetComponent <Button>(); btn.onClick.AddListener(TaskOnClick); if (data.name == "P25" || data.name == "P26") { world = World.FromJson(data.text, trajectoryData.text); } else { world = World.FromJson(data.text); } initializeVelocities(); spawnObstacle(world.boundingPolygon, "Bounding polygon", boundingPolygon); spawnObstacles(); spawnActors(); initializeMotionModel(); if (data.name == "P22") { solveVRP(world); } else if (data.name == "P25") { Visualizer.visualizeTrajectory(world.trajectory.x, world.trajectory.y); GameObject tmp = new GameObject("LeaderFormationController"); tmp.AddComponent <LeaderFormationController> (); tmp.GetComponent <LeaderFormationController> ().initializeController(agents, world.trajectory, world.formationPositions, agents [0].transform.localScale.y / 2, formationDecreasingGoalVelocity); } else if (data.name == "P26") { // Formation problems GameObject plane = GameObject.Find("Plane"); Renderer ren = plane.GetComponent <Renderer> (); ren.material = fieldMaterial; //for (int i = 0; i < world.trajectory.x.Length; i++) { //world.trajectory.x [i] += -10.0f; //world.trajectory.y [i] += 35.0f; //} Visualizer.visualizeTrajectory(world.trajectory.x, world.trajectory.y); agentParent.AddComponent <VirtualStructure> (); // Add virtual center to formation positions Vector2[] formationPositions = new Vector2[world.formationPositions.Length + 1]; for (int i = 0; i < formationPositions.Length - 1; i++) { formationPositions [i] = world.formationPositions [i]; } formationPositions [formationPositions.Length - 1] = new Vector2(agents [agents.Length - 1].transform.position.x, agents [agents.Length - 1].transform.position.z); //position of virtual center. Debug.Log(formationPositions [formationPositions.Length - 1]); agentParent.GetComponent <VirtualStructure> ().initializeController(agents, world.boundingPolygon, world.trajectory, formationPositions, agents [0].transform.localScale.y / 2, deltaX, deltaY, simulationSpeedFactor, world.vehicle.dt, formationDecreasingGoalVelocity); } else if (data.name == "P27") { float[] minMaxes = VirtualStructure.getMinMaxes(world.boundingPolygon); Vector2[] points = new Vector2[pivotSamples]; for (int i = 0; i < pivotSamples; i++) { do { points[i].x = (float)(rand.NextDouble() * (minMaxes [2] - minMaxes [0]) + minMaxes [0]); points[i].y = (float)(rand.NextDouble() * (minMaxes [3] - minMaxes [1]) + minMaxes [1]); } while ((!Raycasting.insidePolygon(points[i].x, points[i].y, world.boundingPolygon)) || Raycasting.insideObstacle(points[i].x, points[i].y, world.obstacles)); } List <Vector2> addPoints = new List <Vector2> (points); foreach (var point in world.startPositions) { addPoints.Add(point); } foreach (var point in world.enemyPositions) { addPoints.Add(point); } VisibilityGraph.initVisibilityGraph(world, addPoints); agentParent.AddComponent <ShooterController> (); List <ShootingPlanner.OneStepPlan> gamePlan = (new ShootingPlanner(world, weaponType)).getPlan(); agentParent.GetComponent <ShooterController> ().initializeController(gamePlan, agents); } }
private void spawnActors() { if (world.startPositions.Length != 0 && world.enemyPositions.Length != 0) { //Spawn actors in a way suitable for the shooting problem agents = new GameObject[world.startPositions.Length + world.enemyPositions.Length]; GameObject ourPrefab = null; GameObject enemyPrefab = null; if (weaponType == WeaponTypeEnum.Shotgun) { ourPrefab = agentPrefab; enemyPrefab = rifleSoldierPrefab; } else { ourPrefab = rifleSoldierPrefab; enemyPrefab = agentPrefab; } for (int i = 0; i < world.startPositions.Length; i++) { Vector3 orientation = Vector3.zero; spawnActor(world.startPositions [i], orientation, i, ourPrefab, true, false); } for (int i = 0; i < world.enemyPositions.Length; i++) { Vector3 orientation = Vector3.zero; GameObject actor = spawnActor(world.enemyPositions [i], orientation, world.startPositions.Length + i, enemyPrefab, true, false, 1.25f); //Renderer ren = actor.GetComponent<Renderer> (); //ren.material = fieldMaterial; } } else if (world.startPositions.Length != 0 && world.goalPositions.Length != 0) { //Spawn actors in a way suitable for all the other problems world.currentAngularVel = new float[world.startPositions.Length]; world.currentPositions = (Vector2[])world.startPositions.Clone(); agents = new GameObject[world.startPositions.Length]; for (int i = 0; i < world.startPositions.Length; i++) { Vector3 orientation = new Vector3(world.goalPositions [i].x, objectHeight, world.goalPositions [i].y).normalized; spawnActor(world.startPositions [i], orientation, i, agentPrefab, true, true); } } else if (world.startPositions.Length != 0 && world.formationPositions.Length != 0) { // Actor spawning for P25 and P26 problems if (data.name == "P25") { world.currentAngularVel = new float[world.startPositions.Length + 1]; world.currentPositions = new Vector2[world.startPositions.Length + 1]; agents = new GameObject[world.startPositions.Length + 1]; // Initialize leader Vector3 orientation = UtilityClass.rads2Vec(world.trajectory.theta [0]); Vector2 pos = new Vector2(world.trajectory.x [0], world.trajectory.y [0]); spawnActor(pos, orientation, 0, agentPrefab, true, true); world.currentPositions [0] = pos; // Initialize followers for (int i = 0; i < world.startPositions.Length; i++) { orientation = Vector3.left; //change so that they look towards their formation position spawnActor(world.startPositions [i], orientation, i + 1, agentPrefab, true, true); world.currentPositions [i + 1] = world.startPositions [i]; } } else if (data.name == "P26") { world.currentAngularVel = new float[world.startPositions.Length + 2]; world.currentPositions = new Vector2[world.startPositions.Length + 2]; agents = new GameObject[world.startPositions.Length + 2]; // Initialize opponent football player Vector3 orientation = UtilityClass.rads2Vec(world.trajectory.theta [0]); Vector2 pos = new Vector2(world.trajectory.x [0], world.trajectory.y [0]); spawnActor(pos, orientation, 0, agentPrefab, true, true); world.currentPositions [0] = pos; // Initialize players for (int i = 0; i < world.startPositions.Length; i++) { orientation = Vector3.left; //change so that they look towards their formation position spawnActor(world.startPositions [i], orientation, i + 1, agentPrefab, true, true); world.currentPositions [i + 1] = world.startPositions [i]; } // Initialize virtual structure center world.currentPositions[world.currentPositions.Length - 1] = VirtualStructure.getCenter(world.formationPositions); spawnActor(world.currentPositions [world.currentPositions.Length - 1], Vector3.forward, agents.Length - 1, agentPrefab, false, false); } } }