Difference between revisions of "Radar"

From Robowiki
Jump to navigation Jump to search
m (fixing wiki link)
m (Higher-quality image)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
[[Image:Radar.png|thumb|right|300px|Radar in [[Robocode]]]]
 +
 
'''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.
 
'''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.
  
[[Image:Radar.jpg|thumb|right|300px|Radar in [[Robocode]]]]
+
== Technical information ==
 +
A radar in Robocode can turn a maximum of 45° or &pi;/4 rad in a single tick. The radar scans robots up to 1200 units away. The angle that the radar rotates between two ticks creates what is called a radar arc, and 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.
 +
 
 +
== Prerequisites ==
 +
Before you start implementing a radar, you should have:
 +
# Followed the [[Robocode/My First Robot|My First Robot Tutorial]], and created a robot.
 +
# Read the [[Robocode/FAQ|FAQ]], understood non-blocking calls, and switched your robot to an <code>AdvancedRobot</code>.
 +
# Gotten a good understanding of Java and at least a basic familiarity of the [http://robocode.sourceforge.net/docs/robocode/ Robocode API].
  
== Technical Information ==
+
If you haven't done all of these, do them first. Otherwise, this article will only make you more confused.
A radar in Robocode can turn a maximum of 45° or &pi;/4<sup>rad</sup> in a single tick. The radar scans robots up to 1200 units away. The angle that the radar rotates between two ticks creates what is called a radar arc, and 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.
 
  
== Initial Scan Direction ==
+
== Display scan arcs ==
The optimal direction to scan at the beginning of the round is generally considered to be the one with the shortest rotational distance to the angle to the center of the field. However there is likely many robots that have a more complex initial scan setup. Such variations include rotating the gun and robot to get a larger scan arc to find the enemy faster.
+
By default, Robocode hides radar arcs, to prevent visual overload. This is a good idea in large [[melee]] battles, but not when debugging your radar.
  
== 1-vs-1 Radars ==
+
Scan arcs can be enabled in Robocode's Preferences:
[[One on One Radar|One on one radars]] are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock. The most common types of radar in [[One on One|one on one]] are:
 
  
* Spinning radar
+
[[File:EnableScanArcs.png]]
* The Infinity Lock
 
* Perfect radar locks
 
:* Narrow lock
 
:* Wide lock
 
  
== Melee radars ==
+
== Configure the radar ==
[[Melee Radar|Melee radars]] are more complex and take up considerably 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. Common melee radars include:
+
Your first action in <code>run()</code> should '''always''' be:
* Spinning Radar
 
* Oldest Scanned Radar
 
* Gun Heat Lock
 
== Notes ==
 
For most of these radar locks, you will need to add one of the following to your <code>run()</code> method:
 
  
 
<syntaxhighlight>
 
<syntaxhighlight>
setAdjustRadarForRobotTurn(true);
 
 
setAdjustGunForRobotTurn(true);
 
setAdjustGunForRobotTurn(true);
 
setAdjustRadarForGunTurn(true);
 
setAdjustRadarForGunTurn(true);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
[[Category:Code Snippets]]
+
This allows your robot's base, gun, and radar to rotate independently. It is practically essential for radar locks and any form of accurate targeting.
[[th:เรดาร์]]
+
 
 +
== At round start ==
 +
One of the first actions your robot performs should be to turn the radar as much as possible. A simple implementation would be:
 +
 
 +
<syntaxhighlight>
 +
setTurnRadarRight(Double.POSITIVE_INFINITY);
 +
</syntaxhighlight>
 +
 
 +
This causes the radar to begin turning clockwise, forever. However, it may not be wise to always turn the radar clockwise. Sometimes, turning it counterclockwise might provide more information, faster. The optimal scan direction is the one with the shortest rotational difference to the angle between the robot and the battlefield center.
 +
 
 +
For even faster information collection, you should turn the gun (or even the robot base as well) in the same direction as the radar. Due to [[Robocode/Game Physics|Robocode game physics]], spinning the gun and radar at the same time will give your robot a 65° (20° + 45°) scan arc, instead of a 45° arc.
 +
 
 +
== 1-vs-1 radar ==
 +
: ''Main article: [[One on One Radar]]
 +
One on one radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock. This simplest lock is:
 +
 
 +
<syntaxhighlight>
 +
public void onScannedRobot(ScannedRobotEvent e) {
 +
    setTurnRadarRight(2.0 * Utils.normalRelativeAngleDegrees(getHeading() + e.getBearing() - getRadarHeading()));
 +
}
 +
</syntaxhighlight>
 +
 
 +
Other types of [[One on One Radar|1v1 radar]] include the [[One on One Radar#Width Lock|width lock]] and the [[One on One Radar#The Infinity Lock|infinity lock]].
 +
 
 +
== Melee radars ==
 +
: ''Main article: [[Melee Radar]]
 +
Melee radars are more complex and take up considerably 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. Common melee radars include:
 +
* Spinning radar ‒ Simple but inefficient.
 +
* Oldest scanned radar ‒ Scan all bots, and then reverse (unless it would be more efficient to not do that). Probably good enough.
 +
* Optimal radar ‒ Present in all top melee bots. Left as an exercise to the reader.
 +
* Gun heat lock ‒ Lock on a target before firing, spin otherwise. Some bots do this.

Latest revision as of 02:01, 28 August 2017

Radar in Robocode

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.

Technical information

A radar in Robocode can turn a maximum of 45° or π/4 rad in a single tick. The radar scans robots up to 1200 units away. The angle that the radar rotates between two ticks creates what is called a radar arc, and 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.

Prerequisites

Before you start implementing a radar, you should have:

  1. Followed the My First Robot Tutorial, and created a robot.
  2. Read the FAQ, understood non-blocking calls, and switched your robot to an AdvancedRobot.
  3. Gotten a good understanding of Java and at least a basic familiarity of the Robocode API.

If you haven't done all of these, do them first. Otherwise, this article will only make you more confused.

Display scan arcs

By default, Robocode hides radar arcs, to prevent visual overload. This is a good idea in large melee battles, but not when debugging your radar.

Scan arcs can be enabled in Robocode's Preferences:

EnableScanArcs.png

Configure the radar

Your first action in run() should always be:

setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);

This allows your robot's base, gun, and radar to rotate independently. It is practically essential for radar locks and any form of accurate targeting.

At round start

One of the first actions your robot performs should be to turn the radar as much as possible. A simple implementation would be:

setTurnRadarRight(Double.POSITIVE_INFINITY);

This causes the radar to begin turning clockwise, forever. However, it may not be wise to always turn the radar clockwise. Sometimes, turning it counterclockwise might provide more information, faster. The optimal scan direction is the one with the shortest rotational difference to the angle between the robot and the battlefield center.

For even faster information collection, you should turn the gun (or even the robot base as well) in the same direction as the radar. Due to Robocode game physics, spinning the gun and radar at the same time will give your robot a 65° (20° + 45°) scan arc, instead of a 45° arc.

1-vs-1 radar

Main article: One on One Radar

One on one radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock. This simplest lock is:

public void onScannedRobot(ScannedRobotEvent e) {
    setTurnRadarRight(2.0 * Utils.normalRelativeAngleDegrees(getHeading() + e.getBearing() - getRadarHeading()));
}

Other types of 1v1 radar include the width lock and the infinity lock.

Melee radars

Main article: Melee Radar

Melee radars are more complex and take up considerably 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. Common melee radars include:

  • Spinning radar ‒ Simple but inefficient.
  • Oldest scanned radar ‒ Scan all bots, and then reverse (unless it would be more efficient to not do that). Probably good enough.
  • Optimal radar ‒ Present in all top melee bots. Left as an exercise to the reader.
  • Gun heat lock ‒ Lock on a target before firing, spin otherwise. Some bots do this.