简体   繁体   English

Robocode如何让敌人坐标

[英]Robocode how to get the enemies co-ordinates

I've been trying to get an enemy's coordinates so I can act on where they are. 我一直试图获得一个敌人的坐标,以便我可以采取行动。 The code I use does not seem to work: 我使用的代码似乎不起作用:

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

I seem to be getting odd returns that are giving me values greater than the size of the field and even minus numbers, has anyone any idea on how to ammend this piece of code to get the enemy's X and Y in the same way my X and Y is returned? 我似乎得到奇怪的回报,给我的值大于场的大小甚至减去数字,有没有人知道如何修改这段代码来获得敌人的XY与我的XY被退回?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM