// If we are too close to a wall, calculate a course toward // the center of the battlefield. if ((y < WALL_AVOID_DISTANCE) ((fieldHeight - y) < WALL_AVOID_DISTANCE)) { desiredY = centerY; nearWall = true; } else { desiredY = y; } if ((x < WALL_AVOID_DISTANCE) ((fieldWidth - x) < WALL_AVOID_DISTANCE)) { desiredX = centerX; nearWall = true; } else { desiredX = x; }
// Determine the safe heading and factor it in with the desired // heading if the bot is near a wall if (nearWall) { double desiredBearing = calculateBearingToXYRadians(x, y, currentHeading, desiredX, desiredY); double distanceToWall = Math.min( Math.min(x, (fieldWidth - x)), Math.min(y, (fieldHeight - y))); int wallFactor = (int)Math.min((distanceToWall / WALL_AVOID_INTERVAL), WALL_AVOID_FACTORS); return ((((WALL_AVOID_FACTORS - wallFactor) * desiredBearing) + (wallFactor * heading)) / WALL_AVOID_FACTORS); } else { return heading; } }