Difference between revisions of "Radar"

From Robowiki
Jump to navigation Jump to search
(Updating Technical Information)
(miscellaneous cleanup)
Line 1: Line 1:
The radar is one of the most vital components of your robot. Without it [[targeting]] is effectively impossible, and [[movement]] is purely random. Just as with movement and targeting, there are many simple and complex algorithms for radar control. In most robots the radar takes up the smallest portion of code.
+
The radar is one of the most vital components of your robot. Without it, [[targeting]] is effectively impossible and [[movement]] is purely random. Just as with movement and targeting, there are many simple and complex algorithms for radar control. In most robots the radar takes up the smallest portion of code.
  
Many [[:Category:Bot Authors|bot authors]] place their radar adjustment code at the end of the <code>onScannedRobot()</code> method; this by itself is not a mistake and generally has little effect. However in robots that do a lot of processing, it is best to place it at the beginning of the <code>onScannedRobot()</code> method or in the main loop (if there is one), as to allow the radar to reset if the robot skips its turn in a later method.
+
In robots that do a lot of processing, it is best to place the radar code near the beginning of the processing loop for each tick, as this will allow the radar to avoid slipping if the robot [[Skipped Turns|skips a turn]] due to too much processing.
  
  
 
== Technical Information ==
 
== Technical Information ==
A radar in robocode can only move a maximum of 45° or &pi;/4<sup>rad</sup> in a single tick. The distance moved between two ticks creates a radar arc. The radar scans a distance of 1200. Every robot detected within the arc is sent to the <code>onScannedRobot()</code> method in the order they we're encountered.
+
A radar in Robocode has a maximum of 45° or &pi;/4<sup>rad</sup> in a single tick. The distance moved between two ticks creates a radar arc. The radar scans a distance of 1200. Every robot detected within the arc is sent to the <code>onScannedRobot()</code> method in order of distance from the scanning bot - the closest bot is detected first, while the furthest bot is detected last. By default, the <code>onScannedRobot()</code> method has the lowest [[event priority]] of all the event handlers in Robocode, so it is the last one to be triggered each tick.
  
 
== 1-vs-1 Radars ==
 
== 1-vs-1 Radars ==
1-vs-1 radars are the smallest of the bunch and many can get scan in every turn, producing what is known as a Perfect Lock.
+
1-vs-1 radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock.
  
 
=== Spinning radar ===
 
=== Spinning radar ===
Line 19: Line 19:
  
 
     do {
 
     do {
         setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
+
         turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
     } while (true);
 
     } while (true);
 
}
 
}
Line 25: Line 25:
  
 
=== The infinity lock ===
 
=== The infinity lock ===
The infinity lock is the simplest radar lock, and is used frequently in [[NanoBot]]s. It has the disadvantage of "slipping" and losing its lock frequently.
+
The infinity lock is the simplest radar lock and it is used frequently in [[NanoBots]]. It has the disadvantage of "slipping" and losing its lock frequently.
  
 
Here is an example of this type of radar:
 
Here is an example of this type of radar:
Line 31: Line 31:
 
public void run() {
 
public void run() {
 
     // ...
 
     // ...
     setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
+
     turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
}
 
}
  
Line 53: Line 53:
 
     // ...
 
     // ...
  
     turnRadarRight(Double.POSITIVE_INFINITY);
+
     turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
     do {
 
     do {
 
         scan();
 
         scan();
Line 70: Line 70:
  
 
==== Wide lock ====
 
==== Wide lock ====
The wide-type radar locks are used to avoid the rare slipping of the thin lock. They scan a wider portion of the battlefield but still will not slip. This type of lock is generally the largest of the 1-vs-1 radars.
+
The wide radar locks are used to avoid the rare slipping of the thin lock. They scan a wider portion of the battlefield and will not slip. This type of lock is generally the largest of the 1-vs-1 radars.
  
 
Here is an example of this type of radar:
 
Here is an example of this type of radar:
Line 83: Line 83:
 
     do {
 
     do {
 
         if (ticksSinceLastScan++ > 2) {
 
         if (ticksSinceLastScan++ > 2) {
             setTurnRadarRight(Double.POSITIVE_INFINITY);
+
             setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
 
         }
 
         }
  
Line 110: Line 110:
 
</pre>
 
</pre>
  
== Melee Radars ==
+
== Melee radars ==
Melee radars are more complex and take up considerable more room inside a robot. There are few known ways to get a Perfect Lock in a melee radar.
+
Melee radars are more complex and take up considerable more room inside a robot. Since the field of opponents does not usually fall within a 45° area, compromises must be made between frequent data of one bot (e.g., the firing target) and consistently updated data of all bots.
  
 
=== Spinning radar ===
 
=== Spinning radar ===
Line 121: Line 121:
  
 
     do {
 
     do {
         setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
+
         turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
     } while (true);
 
     } while (true);
 
}
 
}
Line 127: Line 127:
  
 
==== Corner Arc ====
 
==== Corner Arc ====
A variation on the spinning radar, if the robot is in a corner it scans back and forth across the 90 degree arc away from the corner, as not to waste time scanning where there cannot be any robots. This is the closest to being a Perfect Melee Lock.
+
A variation on the spinning radar, if the robot is in a corner it scans back and forth across the 90 degree arc away from the corner, as not to waste time scanning where there cannot be any robots.
  
 
=== Oldest Scanned ===
 
=== Oldest Scanned ===

Revision as of 16:17, 12 November 2007

The radar is one of the most vital components of your robot. Without it, targeting is effectively impossible and movement is purely random. Just as with movement and targeting, there are many simple and complex algorithms for radar control. In most robots the radar takes up the smallest portion of code.

In robots that do a lot of processing, it is best to place the radar code near the beginning of the processing loop for each tick, as this will allow the radar to avoid slipping if the robot skips a turn due to too much processing.


Technical Information

A radar in Robocode has a maximum of 45° or π/4rad in a single tick. The distance moved between two ticks creates a radar arc. The radar scans a distance of 1200. Every robot detected within the arc is sent to the onScannedRobot() method in order of distance from the scanning bot - the closest bot is detected first, while the furthest bot is detected last. By default, the onScannedRobot() method has the lowest event priority of all the event handlers in Robocode, so it is the last one to be triggered each tick.

1-vs-1 Radars

1-vs-1 radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock.

Spinning radar

A simple spin of the radar, this is very ineffective in one on one, but still used in NanoBots.

Here is an example of this type of radar:

public void run() {
    // ...

    do {
        turnRadarRightRadians(Double.POSITIVE_INFINITY);
    } while (true);
}

The infinity lock

The infinity lock is the simplest radar lock and it is used frequently in NanoBots. It has the disadvantage of "slipping" and losing its lock frequently.

Here is an example of this type of radar:

public void run() {
    // ...
    turnRadarRightRadians(Double.POSITIVE_INFINITY);
}

public void onScannedRobot(ScannedRobotEvent e) {
    // ...
    setTurnRadarLeftRadians(getRadarTurnRemainingRadians());
}

Perfect radar locks

There are several "perfect" radar locks that will not slip once they have a lock.

Narrow lock

This type of lock is thin and follows the robot around the battlefield.

Here is an example of this kind of radar:

import robocode.util.Utils;

public void run() {
    // ...

    turnRadarRightRadians(Double.POSITIVE_INFINITY);
    do {
        scan();
    } while (true);
}

public void onScannedRobot(ScannedRobotEvent e) {
    double absoluteBearing = getHeadingRadians() + e.getBearingRadians();

    double radarTurn = absoluteBearing - getRadarHeadingRadians();
    setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn));

    // ...
}

Wide lock

The wide radar locks are used to avoid the rare slipping of the thin lock. They scan a wider portion of the battlefield and will not slip. This type of lock is generally the largest of the 1-vs-1 radars.

Here is an example of this type of radar:

import robocode.util.Utils;

private int ticksSinceLastScan = 0;

public void run() {
    // ...

    do {
        if (ticksSinceLastScan++ > 2) {
            setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
        }

        execute();
    } while (true);
}

public void onScannedRobot(ScannedRobotEvent e) {
    double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
    double radarTurn = Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians());

    // Width of the bot, plus twice the arc it can move in a tick
    double arcToScan = atan(36.0 / e.getDistance());

    // Limit this so that we don't overshoot
    if (arcToScan > PI / 4.0) arcToScan = PI / 4.0;

    // We want to sent the radar even further in the direction it's moving
    radarTurn += (radarTurn < 0) ? -arcToScan : arcToScan;
    setTurnRadarRightRadians(radar);

    ticksSinceLastScan = 0;

    // ...
}

Melee radars

Melee radars are more complex and take up considerable more room inside a robot. Since the field of opponents does not usually fall within a 45° area, compromises must be made between frequent data of one bot (e.g., the firing target) and consistently updated data of all bots.

Spinning radar

Just as with one on one, there is the generic spinning radar. This is the most used melee radar as it is by far the easiest to implement.

public void run() {
    // ...

    do {
        turnRadarRightRadians(Double.POSITIVE_INFINITY);
    } while (true);
}

Corner Arc

A variation on the spinning radar, if the robot is in a corner it scans back and forth across the 90 degree arc away from the corner, as not to waste time scanning where there cannot be any robots.

Oldest Scanned

This type of melee radar spins towards the robot it hasn't seen in the longest amount of time.


Notes

For most of these radar locks, you will need to add one of the following to your run() method:

setAdjustRadarForRobotTurn(true);
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);