[英]Robocode how to get the enemies co-ordinates
我一直試圖獲得一個敵人的坐標,以便我可以采取行動。 我使用的代碼似乎不起作用:
double absBearing = e.getBearingRadians() + e.getHeadingRadians();
double ex = getX() + e.getDistance() * Math.sin(absBearing);
double ey = getY() + e.getDistance() * Math.cos(absBearing);
我似乎得到奇怪的回報,給我的值大於場的大小甚至減去數字,有沒有人知道如何修改這段代碼來獲得敵人的X
和Y
與我的X
和Y
被退回?
public class MyRobot extends AdvancedRobot {
private RobotStatus robotStatus;
(...)
public void onStatus(StatusEvent e) {
this.robotStatus = e.getStatus());
}
public void onScannedRobot(ScannedRobotEvent e) {
double angleToEnemy = e.getBearing();
// Calculate the angle to the scanned robot
double angle = Math.toRadians((robotStatus.getHeading() + angleToEnemy % 360);
// Calculate the coordinates of the robot
double enemyX = (robotStatus.getX() + Math.sin(angle) * e.getDistance());
double enemyY = (robotStatus.getY() + Math.cos(angle) * e.getDistance());
}
(...)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.