//Try to merge the last node of the tree with the first possible node of the OTHER tree public ArrayList Merge(rrTree other) { ArrayList nodeList = other.nodes; bool find = false; rrtNode conn = null; foreach (rrtNode checkNode in nodeList) { if (freePath(last, checkNode)) { conn = checkNode; find = true; break; } } if (!find) { return(null); } ArrayList fwdPath = getPath(); ArrayList bwdPath = other.getPath(conn); bwdPath.Reverse(); foreach (Vector3 v in bwdPath) { fwdPath.Add(v); } return(fwdPath); }
private void inizializeTree(Vector3 dest) { tree = new rrTree(radius); rrtNode node = new rrtNode(transform.position, null); node.forward = transform.forward; tree.addNode(node); }
public ArrayList findBidirectionalPath(Vector3 source, Vector3 dest, int numPoints, int dim) { y = source.y; rrTree treeForward = new rrTree(); rrtNode s = new rrtNode(source, null); treeForward.addNode(s); rrTree treeBackward = new rrTree(); rrtNode d = new rrtNode(dest, null); treeBackward.addNode(d); bool end = false; createPoint(s.point); createPoint(d.point); // try to merge the two tree ArrayList path = treeForward.Merge(treeBackward); if (path != null) { return(path); } //ArrayList path2 = treeBackward.Merge (treeForward); //if (path2 != null ) return path2; for (int i = 0; i < numPoints && !end; i++) { //forward if (addRandomNode(source, dest, treeForward, dim)) { // try to merge the two tree ArrayList path3 = treeForward.Merge(treeBackward); if (path3 != null) { return(path3); } } //backward if (addRandomNode(dest, source, treeBackward, dim)) { // try to merge the two tree ArrayList path4 = treeBackward.Merge(treeForward); if (path4 != null) { path4.Reverse(); return(path4); } } } return(null); }
public ArrayList findPath(Vector3 source, Vector3 dest, int numPoints, int dim) { rrTree tree = new rrTree(); rrtNode s = new rrtNode(source, null); bool end = false; tree.addNode(s); rrtNode lastPoint = s; bool doCheck = true; for (int i = 0; i < numPoints && !end; i++) { if (doCheck) { if (freePath(lastPoint.point, dest)) { rrtNode d = new rrtNode(dest, lastPoint); lastPoint.addConnection(d); tree.addNode(d); //Debug.Log ("Point: "+point.point); end = true; } } if (!end) { //pick up a random point //Vector3 newPoint = new Vector3 (myRnd (-dim, dim), 0, myRnd (-dim, dim)); //Vector3 newPoint = new Vector3 ( myRnd (s.point.x, 1, dim), y, myRnd (s.point.z, 1, dim)); Vector3 newPoint = myRndVector(source, dest, dim, 30f, 0.3f); rrtNode point = tree.findClosestNode(newPoint); if (point != null && freePath(newPoint, point.point)) { //if it is possible to reach the point from the previous point //add the connection rrtNode p2 = new rrtNode(newPoint, point); point.addConnection(p2); tree.addNode(p2); //from the next iteration consider the new point lastPoint = p2; createPoint(p2.point); doCheck = true; } //else doCheck = false; } } return(tree.getPath()); }
private bool addRandomNode(rrTree tree, int dim) { Vector3 newPoint = new Vector3(myRnd(dim), y, myRnd(dim)); createPoint(newPoint); rrtNode point = tree.findClosestNode(newPoint); if (point != null && freePath(point.point, newPoint)) { //if it is possible to reach the point from the previous point //add the connection rrtNode p2 = new rrtNode(newPoint, point); point.addConnection(p2); tree.addNode(p2); myDrawLine(point.point, newPoint, Color.blue); return(true); } return(true); }
private bool addRandomNode(rrTree tree, int dim) { Vector3 newPoint = new Vector3(myRnd(dim), y, myRnd(dim)); createPoint(newPoint); rrtNode point = tree.findClosestNode(newPoint); ArrayList carPath = freePath(point.point, newPoint, point.forward); if (point != null && carPath.Count > 0) { //if it is possible to reach the point from the previous point //add the connection rrtNode p2 = new rrtNode(newPoint, point); p2.forward = ((Vector3)carPath[carPath.Count - 1] - (Vector3)carPath[carPath.Count - 2]).normalized; //point.addConnection(p2); tree.addNode(p2); myDrawLine(point.point, newPoint, Color.blue); return(true); } return(true); }
private bool addRandomNode(Vector3 source, Vector3 dest, rrTree tree, int dim) { //Vector3 newPoint = new Vector3 (myRnd (-dim, dim), 0, myRnd (-dim, dim)); //Vector3 newPoint = new Vector3 ( myRnd (tree.source.point.x, 1, dim), y, myRnd (tree.source.point.z, 1, dim)); Vector3 newPoint = myRndVector(source, dest, dim, 30f, 0.3f); createPoint(newPoint); Debug.Log("Rand point: " + newPoint); rrtNode point = tree.findClosestNode(newPoint); if (point != null) // && freePath(newPoint, point.point) ) { //if it is possible to reach the point from the previous point //add the connection { rrtNode p2 = new rrtNode(newPoint, point); point.addConnection(p2); tree.addNode(p2); return(true); } return(true); }
private void moveKinematic() { if (Input.GetMouseButtonDown(0)) { destination = DirectionUtility.getMouseDirection(); destination.y = transform.position.y; go = true; calculating = true; tree = new rrTree(); tree.addNode(new rrtNode(transform.position, null)); draw.clean(); } else { if (go && path.Count > 0) { destination = (Vector3)path[path_index]; float d = Vector3.Distance(transform.position, destination); if (d > 0.5) { Vector3 direction = (destination - transform.position).normalized; DirectionUtility.makeKinematicMove(rigidbody, direction, speed); } else { rigidbody.velocity = Vector3.zero; transform.position = destination; path_index++; if (path_index == path.Count) { go = false; path_index = 0; } } } } }