Difference between revisions of "Radar"
(Suggestions on radar placement) |
(Cleaning up formatting) |
||
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. | |
== Example code == | == Example code == | ||
Line 38: | Line 38: | ||
=== Perfect radar locks === | === Perfect radar locks === | ||
− | There are several "perfect" radar locks that will not slip once they have a lock. Here is | + | 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: | ||
− | |||
− | |||
<pre> | <pre> | ||
import robocode.util.Utils; | import robocode.util.Utils; | ||
Line 47: | Line 48: | ||
public void run() { | public void run() { | ||
// ... | // ... | ||
+ | |||
turnRadarRight(Double.POSITIVE_INFINITY); | turnRadarRight(Double.POSITIVE_INFINITY); | ||
do { | do { | ||
Line 54: | Line 56: | ||
public void onScannedRobot(ScannedRobotEvent e) { | public void onScannedRobot(ScannedRobotEvent e) { | ||
− | |||
double absoluteBearing = getHeadingRadians() + e.getBearingRadians(); | double absoluteBearing = getHeadingRadians() + e.getBearingRadians(); | ||
+ | |||
double radarTurn = absoluteBearing - getRadarHeadingRadians(); | double radarTurn = absoluteBearing - getRadarHeadingRadians(); | ||
setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn)); | setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn)); | ||
+ | |||
+ | // ... | ||
} | } | ||
</pre> | </pre> | ||
==== 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 in codesize. | |
+ | |||
<pre> | <pre> | ||
import robocode.util.Utils; | import robocode.util.Utils; | ||
− | int ticksSinceLastScan = 0; | + | private int ticksSinceLastScan = 0; |
+ | |||
public void run() { | public void run() { | ||
// ... | // ... | ||
+ | |||
do { | do { | ||
− | if (ticksSinceLastScan++ > 2) | + | if (ticksSinceLastScan++ > 2) { |
setTurnRadarRight(Double.POSITIVE_INFINITY); | setTurnRadarRight(Double.POSITIVE_INFINITY); | ||
+ | } | ||
execute(); | execute(); | ||
Line 84: | Line 92: | ||
double arcToScan = atan(36.0 / e.getDistance()); | double arcToScan = atan(36.0 / e.getDistance()); | ||
− | //Limit this so that we don't overshoot | + | // Limit this so that we don't overshoot |
if (arcToScan > PI / 4.0) arcToScan = PI / 4.0; | if (arcToScan > PI / 4.0) arcToScan = PI / 4.0; | ||
− | //We want to sent the radar even further in the direction | + | // We want to sent the radar even further in the direction it's moving |
radarTurn += (radarTurn < 0) ? -arcToScan : arcToScan; | radarTurn += (radarTurn < 0) ? -arcToScan : arcToScan; | ||
setTurnRadarRightRadians(radar); | setTurnRadarRightRadians(radar); |
Revision as of 09:03, 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.
Many bot authors place their radar adjustment code at the end of the onScannedRobot()
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 onScannedRobot()
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.
Contents
Example code
NOTE: 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);
Spinning radar
To just spin the radar, add this to your run()
method:
setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
The infinity lock
The infinity lock is the simplest radar lock, and is used frequently in Nano bots. It has the disadvantage of "slipping" and losing its lock frequently.
public void run() { // ... setTurnRadarRightRadians(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() { // ... turnRadarRight(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-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 in codesize.
import robocode.util.Utils; private int ticksSinceLastScan = 0; public void run() { // ... do { if (ticksSinceLastScan++ > 2) { setTurnRadarRight(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; // ... }