我們只要對(duì)在 Tracking your opponents' movement 中做的機(jī)器人加以補(bǔ)充,就能將因數(shù)避墻法添加到現(xiàn)有的或討厭的移動(dòng)算法中。這種方法將預(yù)想的方向和根據(jù)機(jī)器人和墻之間距離遠(yuǎn)近確定的安全方向作為因數(shù)試圖找到最可能的方向。
這個(gè)方法的前面一半根據(jù)機(jī)器人和墻的靠近程度選擇安全的 x 和 y 的位置(機(jī)器人當(dāng)前的 x 或 y 位置,或者假如機(jī)器人靠近墻,則就是中心點(diǎn))。方法的后面一半則計(jì)算距離“安全點(diǎn)”的方位,并把這個(gè)方位和依機(jī)器人離墻遠(yuǎn)近得到的預(yù)想方向都作為因數(shù)考慮在內(nèi)。
// 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; } }