Robocode:onScannedRobot的精度

时间:2020-04-04 16:29:19

标签: java robocode

我写了一个相对简单的AdvancedRobot,它可以绕开雷达并记录所有敌人的速度。最终,我注意到在不应该错过的情况下机器人会错过。我从Robocode / Graphical Debugging Wiki复制了代码并进行了测试。这是代码(Wiki当前关闭):

// 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());
}

事件处理程序:

// 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);
}

“实心方块”绝对不在机器人顶部。下面显示了几个屏幕截图。看起来精度取决于距离,但是我不确定。这是预期的,还是我做错了什么?

Shot 1 Shot 2

2 个答案:

答案 0 :(得分:1)

可能发生这种情况的一个原因是onScannedRobot事件的传递被延迟,直到higher priority事件完成处理为止。特别是,如果优先级较高的事件处理程序执行了使身体旋转的命令,则该命令将在调用onScannedRobot之前执行,从而导致时间提前,机器人移动以及机器人的方向发生改变。

由于延迟的事件传递会导致各种问题,因此建议不要在事件处理程序中执行命令。相反,事件处理程序应该简单地检查,考虑并在字段中存储信息,以使主循环做出反应。这使主循环可以在执行某项操作之前查看所有可用信息,并根据给定的接收信息总量,智能地选择最适合的操作。例如,它可以查看前面发生的机器人撞击事件,再结合雷达探测到从后方接近的对手,并确定侧向躲避比向后方的通常躲避更有希望...

答案 1 :(得分:0)

我想我知道问题所在。它似乎没有记录在案;如果我错了,请发布链接。 ScannedRobotEvent报告相对于机器人上一个标题的方位,即最后一次StatusEvent命中时的标题。考虑到这一点,大大提高了我的机器人的准确性。

相关问题