简体   繁体   中英

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?

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

    (...)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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