5

我一直在尝试获取敌人的坐标,以便根据他们的位置采取行动。我使用的代码似乎不起作用:

    double absBearing = e.getBearingRadians() + e.getHeadingRadians();
    double ex = getX() + e.getDistance() * Math.sin(absBearing);
    double ey = getY() + e.getDistance() * Math.cos(absBearing);

我似乎得到了奇怪的回报,这些回报给我的值大于字段的大小,甚至是负数,有没有人知道如何修改这段代码以获取敌人的XY以同样的方式X返回Y

4

1 回答 1

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

    (...)
}
于 2014-03-30T11:36:48.500 回答