5

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?

Jason
  • 1,658
  • 3
  • 20
  • 51
Ronan
  • 583
  • 5
  • 20
  • There are plenty of open-source robocode tanks to take a look at. They all have this kind of logic in them. By examining the way others have approached the problem you may find a better approach to the one you're trying. – Software Engineer Feb 28 '14 at 22:54
  • I think you've got your sin & cos reversed, ie, use cos() for ex and use sin() for ey. You also have to make sure you've got your angels consistent, in that usually y increases in the up direction, but on computer screens, y increases in the down direction. Good luck! – Roy Mar 01 '14 at 01:04

1 Answers1

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

    (...)
}
Kris
  • 1,538
  • 2
  • 16
  • 27
  • 1
    To give some more information about these coördinates, they don't seem to be consistant. I've ran a test battle with 2 robots that scan to get the coördinates, and the interactive because that one doesn't move at all. So, if you have a robot that scans an robot that doesn't move at all, you'd expect the X and the Y to be the same all the time, right? Well, that's where i found that the X and Y can vary up to several pixels. First the Y is 277.96894881116384 , and a few scans later, the Y is 283.96894881116395 I would assume this is a problem with robocode itself. – stefan Mar 28 '17 at 10:57