[英]Robocode: precision of onScannedRobot
我編寫了一個相對簡單的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);
}
“實心方塊”絕對不在機器人的頂部。 下面顯示了幾個屏幕截圖。 看起來精度取決於距離,但我不確定。 這是預期的,還是我做錯了什么?
可能發生這種情況的一個原因是 onScannedRobot 事件的傳遞被延遲,直到更高優先級的事件完成處理。 特別是,如果更高優先級的事件處理程序執行一個命令來旋轉身體,該命令將在 onScannedRobot 被調用之前執行,從而導致時間提前,機器人移動,並且你的機器人的航向被改變。
由於延遲事件傳遞會導致各種問題,我建議不要在事件處理程序中執行命令。 相反,事件處理程序應該簡單地檢查、思考並將信息存儲在字段中,以便主循環做出反應。 這使主循環能夠在采取行動之前查看所有可用信息,並智能地 select 考慮到收到的全部信息,哪個行動最合適。 例如,它可以查看前面的機器人撞擊事件,結合雷達探測到的對手從后面接近,並確定側向躲避比通常的后方躲避更有希望......
我想我知道問題是什么。 它似乎沒有記錄在案; 如果我錯了,請發布鏈接。 ScannedRobotEvent
報告相對於機器人先前航向的方位,即最后一次StatusEvent
命中時的航向。 考慮到這一點,大大提高了我的機器人的准確性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.