// this is the function you would need to define for your own environment void createWorld() { // create world: nodes in a grid, set walkable at random for (int j = 0; j < numRows; j++) { for (int i = 0; i < numCols; i++) { float x = (i*nodeWidth) + (nodeWidth*0.5); float y = (j*nodeHeight) + (nodeHeight*0.5); boolean w = (random(1.0) < 0.8); nodes.add(new Node(new Point(x,y),w)); } } // assign valid neighbours for each node for (int j = 0; j < numRows; j++) { for (int i = 0; i < numCols; i++) { int index = (j * numCols) + i; Node n = (Node)nodes.get(index); if (i < numCols-1) { // right n.neighbours.add(nodes.get(index+1)); if (j < numRows - 1) { // below right n.neighbours.add(nodes.get(index+numCols+1)); } if (j > 0) { // above right n.neighbours.add(nodes.get(index-numCols+1)); } } if (i > 0) { // left n.neighbours.add(nodes.get(index-1)); if (j < numRows - 1) { // below left n.neighbours.add(nodes.get(index+numCols-1)); } if (j > 0) { // above left n.neighbours.add(nodes.get(index-numCols-1)); } } if (j < numRows - 1) { // below n.neighbours.add(nodes.get(index+numCols)); } if (j > 0) { // above n.neighbours.add(nodes.get(index-numCols)); } } } // strip diagonal unwalkables // // ie. for node N, with walkable neighbourhood W // and unwalkable neighbourhood // we want to prune inaccessible neighbours: // WWW WWW // WNU --> WNU // WUW WU for (int j = 0; j < numRows; j++) { for (int i = 0; i < numCols; i++) { int index = (j * numCols) + i; Node n = (Node)nodes.get(index); Node above = j > 0 ? (Node)nodes.get(index-numCols) : null; Node below = j < numRows-1 ? (Node)nodes.get(index+numCols) : null; Node left = i > 0 ? (Node)nodes.get(index-1) : null; Node right = i < numCols-1 ? (Node)nodes.get(index+1) : null; if (above != null && left != null) { if (!above.walkable && !left.walkable) { Node aboveleft = (Node)nodes.get(index-numCols-1); n.neighbours.remove(aboveleft); } } if (above != null && right != null) { if (!above.walkable && !right.walkable) { Node aboveright = (Node)nodes.get(index-numCols+1); n.neighbours.remove(aboveright); } } if (below != null && left != null) { if (!below.walkable && !left.walkable) { Node belowleft = (Node)nodes.get(index+numCols-1); n.neighbours.remove(belowleft); } } if (below != null && right != null) { if (!below.walkable && !right.walkable) { Node belowright = (Node)nodes.get(index+numCols+1); n.neighbours.remove(belowright); } } } } // finally, strip unwalkable neighbours // ie: // WWW WWW // WNU --> WN // WU W for (Iterator iterator = nodes.iterator(); iterator.hasNext();) { Node n = (Node)iterator.next(); for (Iterator niterator = n.neighbours.iterator(); niterator.hasNext();) { Node n2 = (Node)niterator.next(); if (!n2.walkable) { niterator.remove(); } } } }