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());
}
(...)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With