gpt4 book ai didi

java - Robocode:onScannedRobot 的精度

转载 作者:行者123 更新时间:2023-12-02 08:46:44 27 4
gpt4 key购买 nike

我编写了一个相对简单的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);
}

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

Shot 1 Shot 2

最佳答案

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

由于延迟事件传递会导致各种问题,因此我建议永远不要在事件处理程序中执行命令。相反,事件处理程序应该简单地检查、思考并将信息存储在字段中,以便主循环使用react。这使得主循环能够在执行一系列操作之前查看所有可用信息,并根据收到的全部信息智能地选择最合适的操作。例如,它可以结合前方的机器人撞击事件以及对从后方接近的对手的雷达检测,并确定侧向躲避比通常的后方躲避更有希望......

关于java - Robocode:onScannedRobot 的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com