Secrets from the Robocode masters

# Predictive targeting

All successful targeting and shooting of enemy robots requires an algorithm to fire bullets at the place where you predict that an enemy robot will be at a future point in time. This algorithm can be used for linear, circular, and oscillating predictive targeting. And if you have a function that returns the position of the enemy at a future point in time, you can use the algorithm to calculate the impact point, the firing angle, the impact position, and the impact time.

This algorithm implements the secant method to numerically solve the impact time. Once this impact time is known, our predictive function obtains the impact position. Then we fire at that position.

The `Intercept` class shown in Listing 1 assumes that the robot is traveling in a straight line from its current position at its current velocity.

##### Listing 1. Using the Intercept class
```public class Intercept {
public Coordinate impactPoint = new Coordinate(0,0);

protected Coordinate bulletStartingPoint = new Coordinate();
protected Coordinate targetStartingPoint = new Coordinate();
public double targetVelocity;
public double bulletPower;
public double angleThreshold;
public double distance;

protected double impactTime;

public void calculate (

// Initial bullet position x coordinate
double xb,
// Initial bullet position y coordinate
double yb,
// Initial target position x coordinate
double xt,
// Initial target position y coordinate
double yt,
// Target velocity
double vt,
// Power of the bullet that we will be firing
double bPower,
// Angular velocity of the target
double angularVelocity_deg_per_sec
)
{

bulletStartingPoint.set(xb, yb);
targetStartingPoint.set(xt, yt);

targetVelocity = vt;
bulletPower = bPower;
double vb = 20-3*bulletPower;

double dX,dY;

impactTime = getImpactTime(10, 20, 0.01);
impactPoint = getEstimatedPosition(impactTime);

dX = (impactPoint.x - bulletStartingPoint.x);
dY = (impactPoint.y - bulletStartingPoint.y);

distance = Math.sqrt(dX*dX+dY*dY);

angleThreshold = Math.toDegrees
}

protected Coordinate getEstimatedPosition(double time) {

double x = targetStartingPoint.x +
double y = targetStartingPoint.y +
return new Coordinate(x,y);
}

private double f(double time) {

double vb = 20-3*bulletPower;

Coordinate targetPosition = getEstimatedPosition(time);
double dX = (targetPosition.x - bulletStartingPoint.x);
double dY = (targetPosition.y - bulletStartingPoint.y);

return Math.sqrt(dX*dX + dY*dY) - vb * time;
}

private double getImpactTime(double t0,
double t1, double accuracy) {

double X = t1;
double lastX = t0;
int iterationCount = 0;
double lastfX = f(lastX);

while ((Math.abs(X - lastX) >= accuracy) &&
(iterationCount < 15)) {

iterationCount++;
double fX = f(X);

if ((fX-lastfX) == 0.0) break;

double nextX = X - fX*(X-lastX)/(fX-lastfX);
lastX = X;
X = nextX;
lastfX = fX;
}

return X;
}
}```

The great thing about the `Intercept` class is that it can be easily reused to calculate the firing angle for circular motion. To do this, write a `CircularIntercept` class that inherits from the `Intercept` class, and overwrite the `getEstimatedPosition()` method. Listing 2 shows the code for the `CircularIntercept` class:

##### Listing 2. CircularIntercept class
```public class CircularIntercept extends Intercept {
protected Coordinate getEstimatedPosition(double time) {
return super.getEstimatedPosition(time);
}

double x = targetStartingPoint.x - targetVelocity /
double y = targetStartingPoint.y - targetVelocity /
return new Coordinate(x,y);
}

}```

Listing 3 shows an example of using the `Intercept` class. It assumes that we calculated the current position, heading, and velocity of the target, as well as the power of the bullet that we will be firing.

##### Listing 3. Using the Intercept class
```Intercept intercept = new Intercept();
intercept.calculate
(
ourRobotPositionX,
ourRobotPositionY,
currentTargetPositionX,
currentTargetPositionY,
currentTargetVelocity,
bulletPower,
0 // Angular velocity
);

// Helper function that converts any angle into
// an angle between +180 and -180 degrees.
double turnAngle = normalRelativeAngle

// Move gun to target angle
robot.setTurnGunRight(turnAngle);

if (Math.abs(turnAngle) <= intercept.angleThreshold) {
// Ensure that the gun is pointing at the correct angle
if (
(intercept.impactPoint.x > 0) &&
(intercept.impactPoint.x < getBattleFieldWidth()) &&
(intercept.impactPoint.y > 0) &&
(intercept.impactPoint.y < getBattleFieldHeight())
) {
// Ensure that the predicted impact point is within
// the battlefield
fire(bulletPower);
}
}
}```

This firing strategy has proven very successful in both of my robots (JollyNinja and MadHatter). Encapsulating the intercept in its own class and subclassing it for different prediction algorithms allows me to usually hit other robots more than they hit me.