public void createTrajectory(Junction initialJunction, List <Road> roads, string _carID) { transform.name = "Trj_" + _carID; source = initialJunction; tracks = new List <Road>(); AbsDirection currentDirection = AbsDirection.NA; int dir = 0; for (; dir < 4; dir++) { if (source.connectRoadIdx[dir] >= 0) { addRoad2Track(roads[source.connectRoadIdx[dir]]); currentDirection = roads[source.connectRoadIdx[dir]].direction; break; } } if (dir == 4 || currentDirection == AbsDirection.NA) { Debug.Log("ERROR : Trajectory - createTrajectory - No connected neighbor."); } Road nextRoad = null; for (int i = 0; i < 100; i++) { int result = getNextDestination(currentDirection, tracks[tracks.Count - 1].target, roads, ref nextRoad); if (result < 0) { break; } addRoad2Track(nextRoad); currentDirection = (AbsDirection)result; } initialize(_carID); }
public void initialize(Road givenRoad, int fromPos, int toPos, int fixedPos, int _index, Vector2 _center, AbsDirection givenDirection, bool _isHorizontal) { index = _index; road = givenRoad; center = _center; isHorizontal = _isHorizontal; direction = givenDirection; if (isHorizontal) { fromPoint = new Vector3((fromPos - 0.5f + 2 + (int)center.x) * 4, 1.0f, (fixedPos + (int)center.y) * 4); toPoint = new Vector3((toPos - 0.5f - 2 + (int)center.x) * 4, 1.0f, (fixedPos + (int)center.y) * 4); Vector2 tempDirection = new Vector2(toPoint.x - fromPoint.x, toPoint.z - fromPoint.z); //Debug.Log(tempDirection + "\t" + givenDirection); if (direction != Util.vec22AbsDirection(tempDirection.normalized)) { Vector3 temp = toPoint; toPoint = fromPoint; fromPoint = temp; } } else { fromPoint = new Vector3((fixedPos + (int)center.x) * 4, 1.0f, (fromPos + -0.5f + 2 + (int)center.y) * 4); toPoint = new Vector3((fixedPos + (int)center.x) * 4, 1.0f, (toPos - 0.5f - 2 + (int)center.y) * 4); Vector2 tempDirection = new Vector2(toPoint.x - fromPoint.x, toPoint.z - fromPoint.z); //Debug.Log(tempDirection + "\t" + givenDirection); if (direction != Util.vec22AbsDirection(tempDirection.normalized)) { Vector3 temp = toPoint; toPoint = fromPoint; fromPoint = temp; } } entryPoints = new List <Vector3>(); length = Mathf.Abs(toPos - fromPos); if (isHorizontal) { region = new Rect((float)(fromPos + center.x) * SimParameter.unitBlockSize, (float)(fixedPos + (int)center.y) * SimParameter.unitBlockSize, (float)(toPos - fromPos) * SimParameter.unitBlockSize, SimParameter.unitBlockSize); } else { region = new Rect((float)(fixedPos + center.x) * SimParameter.unitBlockSize, (float)(fromPos + (int)center.y) * SimParameter.unitBlockSize, SimParameter.unitBlockSize, (float)(toPos - fromPos) * SimParameter.unitBlockSize); } for (int i = 0; i < length; i++) { blocks.Add(Object.Instantiate((Resources.Load("Prefab/DummyObjects/DummyRoadBlock") as GameObject).GetComponent <RoadBlock>(), Vector3.zero, Quaternion.identity) as SCPAR.SIM.PVATestbed.RoadBlock); if (isHorizontal) { blocks[blocks.Count - 1].initialize(fromPos + i + (int)center.x, fixedPos + (int)center.y, ModelType.RoadNormal, AreaPosition.NA, isHorizontal); } else { blocks[blocks.Count - 1].initialize(fixedPos + (int)center.x, fromPos + i + (int)center.y, ModelType.RoadNormal, AreaPosition.NA, isHorizontal); } blocks[blocks.Count - 1].transform.parent = transform; } //generateEntryPoints(fromPos, toPos, fixedPos, center, isHorizontal); }
private int getNextDestination(AbsDirection prvDirection, Junction currentJunction, List <Road> roads, ref Road nextRoad) { if (currentJunction.isNullJunction) { return(-1); } int failedDirection = 0; bool[] failedDirIndex = new bool[3]; failedDirIndex[0] = false; failedDirIndex[1] = false; failedDirIndex[2] = false; while (failedDirection < 3) { // sample next destination RltDirection direction = getRandomDirection(failedDirIndex); if (direction == RltDirection.NA) { Debug.Log("ERROR : Trajectory - getNextDestination - Error in generating random direction"); return(-1); } if (direction == RltDirection.Straight) { if (currentJunction.connectRoadIdx[(int)prvDirection] >= 0) { nextRoad = roads[currentJunction.connectRoadIdx[(int)prvDirection]]; return((int)Util.addDirection(prvDirection, direction)); } else { failedDirIndex[(int)RltDirection.Straight] = true; failedDirection++; } } else if (direction == RltDirection.Left) { if (currentJunction.connectRoadIdx[((int)prvDirection + 3) % 4] >= 0) { nextRoad = roads[currentJunction.connectRoadIdx[((int)prvDirection + 3) % 4]]; return((int)Util.addDirection(prvDirection, direction)); } else { failedDirIndex[(int)RltDirection.Left] = true; failedDirection++; } } else if (direction == RltDirection.Right) { if (currentJunction.connectRoadIdx[((int)prvDirection + 1) % 4] >= 0) { nextRoad = roads[currentJunction.connectRoadIdx[((int)prvDirection + 1) % 4]]; return((int)Util.addDirection(prvDirection, direction)); } else { failedDirIndex[(int)RltDirection.Right] = true; failedDirection++; } } } return(-1); }