Robocode:onScannedRobot 的精度

huangapple go评论76阅读模式
英文:

Robocode: precision of onScannedRobot

问题

我编写了一个相对简单的AdvancedRobot,它会将雷达转动起来并记录所有敌人及其速度。最终,我注意到机器人在本不应该错过的情况下错过了。我从Robocode/Graphical Debugging wiki中复制了代码并进行了测试。以下是代码(Wiki目前不可用):

// 上次扫描到的机器人的坐标
int scannedX = Integer.MIN_VALUE;
int scannedY = Integer.MIN_VALUE;
 
// 当我们扫描到机器人时调用
public void onScannedRobot(ScannedRobotEvent e) {
    // 计算到扫描到的机器人的角度
    double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);
 
    // 计算机器人的坐标
    scannedX = (int)(getX() + Math.sin(angle) * e.getDistance());
    scannedY = (int)(getY() + Math.cos(angle) * e.getDistance());
}

// 事件处理器
public void onPaint(Graphics2D g) {
    // 将绘图颜色设置为半透明红色
    g.setColor(new Color(0xff, 0x00, 0x00, 0x80));
 
    // 从我们的机器人画一条线到扫描到的机器人
    g.drawLine(scannedX, scannedY, (int)getX(), (int)getY());
 
    // 在扫描到的机器人上绘制一个填充的正方形,覆盖它
    g.fillRect(scannedX - 20, scannedY - 20, 40, 40);
}

“填充的正方形”明显不在机器人上方。下面显示了一些屏幕截图。看起来精度取决于距离,但我不确定。这是否是预期的行为,还是我做错了什么?

Robocode:onScannedRobot 的精度 Robocode:onScannedRobot 的精度

英文:

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?

Robocode:onScannedRobot 的精度 Robocode:onScannedRobot 的精度

答案1

得分: 1

因为交付 onScannedRobot 事件的时机被延迟,直到更高优先级的事件处理完成,所以可能会出现这种情况。特别地,如果更高优先级的事件处理程序执行了一个旋转机体的命令,那么该命令将在 onScannedRobot 被调用之前执行,导致时间推进,机器人移动,并改变你的机器人航向。

由于延迟的事件交付会引发各种问题,我建议在事件处理程序中永远不要执行命令。相反,事件处理程序应该仅检查、思考并将信息存储在字段中,以供主循环做出反应。这使得主循环能够在承诺采取行动之前查看所有可用信息,并根据接收到的所有信息智能地选择最合适的行动。例如,它可以查看前方的机器人碰撞事件,结合从后方接近的对手的雷达检测,然后决定侧身回避比通常的向后回避更有希望...

英文:

One reason this may happen is that delivery of the onScannedRobot event is delayed until higher priority events have finished processing. In particular, if a higher priority event handler executes a command to rotate the body, that command will execute before onScannedRobot is called, causing time to advance, robots to move, and your robot's heading to be altered.

Since deferred event delivery causes a variety of problems, I recommend to never execute commands in event handlers. Instead, event handlers should simply inspect, think about, and store information in fields for the main loop to react to. This enables the main loop to look at all available information before committing to a course of action, and to intelligently select which action is most appropriate given the totality of information received. For instance, it could look at a robot hit event on the front combined with a radar detection of an opponent approaching from the rear, and decide that a sideways evasion is more promising than the usual evasion to the rear ...

答案2

得分: 0

我认为我知道问题出在哪里。似乎没有文档记录;如果我错了,请发布一个链接。ScannedRobotEvent 报告相对于机器人上次 StatusEvent 触发时的 前一个 方向的方位角。考虑到这一点,极大地提高了我的机器人的准确性。

英文:

I think I know what the issue is. It does not seem to be documented; if I am wrong, please post a link. ScannedRobotEvent reports bearing relative to previous heading of the robot, i.e., its heading when the last StatusEvent hit. Taking this into account greatly improved my bot's accuracy.

huangapple
  • 本文由 发表于 2020年4月5日 00:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61031205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定