Draw a line between your bug and your target. At each point in the line, calculate the cell that contains the point. If the cell is different than the previous one, check to see if your bug can be in that cell without overlapping any other organism. If, when you run into something, it's your target, you're OK. Otherwise you are going to have to use a more sophisticated path finding algorithm (A* is the most popular choice).
Psuedo code.
ArrayList orgs = Scan(); OrganismState blockedBy = null; Point oldCell; Point newCell;
while( blockedBy == null) { // TODO: Get the next point in the line. // TODO: Calculate the cell position if( newCell != oldCell) { oldCell = newCell;
for each( OrganismState org in orgs) { if( Math.Max( Math.Abs( newCell.X - org.GridX), Math.Abs( newCell.Y - org.GridY)) <= State.CellRadius + org.CellRadius) { blockedBy = org; break; } } } }
The old animal farm had some line drawing algorithms. Perhaps Henrik Yllemo's .chm file has one of them (http://www.yllemo.com/terrarium). |