I wrote a relatively simple AdvancedRobot
that turns its radar around and records all enemies with their velocities. Eventually, I noticed that the robot misses in cases where it's not supposed to miss. I copied the code from Robocode/Graphical Debugging wiki and tested that. Here is the code (Wiki is currently down):
// The coordinates of the last scanned robot
int scannedX = Integer.MIN_VALUE;
int scannedY = Integer.MIN_VALUE;
// Called when we have scanned a robot
public void onScannedRobot(ScannedRobotEvent e) {
// Calculate the angle to the scanned robot
double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);
// Calculate the coordinates of the robot
scannedX = (int)(getX() + Math.sin(angle) * e.getDistance());
scannedY = (int)(getY() + Math.cos(angle) * e.getDistance());
}
And the event handler:
// Paint a transparent square on top of the last scanned robot
public void onPaint(Graphics2D g) {
// Set the paint color to a red half transparent color
g.setColor(new Color(0xff, 0x00, 0x00, 0x80));
// Draw a line from our robot to the scanned robot
g.drawLine(scannedX, scannedY, (int)getX(), (int)getY());
// Draw a filled square on top of the scanned robot that covers it
g.fillRect(scannedX - 20, scannedY - 20, 40, 40);
}
The "filled square" is definitely NOT on top of the robot. A couple of screenshots are shown below. It looks like the precision depends on the distance, but I'm not sure. Is this expected, or am I doing something wrong?