Robocode – Predictive Targeting Implementation
When I was building my bot, the most interesting aspect is implementing predictive targeting. To tackle this problem, I first define two aspects I can use for prediction: Velocity Change and Angular Change per turn.
Here is how I capture these two parameters:
angularChange = (targetHeading – lastTargetHeading) / turnsElapsed;
velocityChange = (targetVelocity – lastTargetVelocity) / turnsElapsed;
After capturing these two values, I can calculate an angle in addition to the current bearing such that the bullet shot from that new angle can arrive within 5 pixels of the target in some future turn.
Here is how I calculate future bullet and target positions at futuren turn:
double estTargetX = targetX;
double estTargetY = targetY;for (double time = 1; time < 100; time++) {
estTargetX = estTargetX + Math.sin(Math.toRadians((targetHeading + time * angularChange))) *(targetVelocity + time * velocityChange);
estTargetY = estTargetY + Math.cos(Math.toRadians((targetHeading + time * angularChange))) * (targetVelocity + time * velocityChange);estBulletX = myX + Math.sin(Math.toRadians(myAngle)) * (time – timeTurnGun) * bulletSpeed;
estBulletY = myY + Math.cos(Math.toRadians(myAngle)) * (time – timeTurnGun) * bulletSpeed;
}
Once the distance between the target and bullet is less than 5 pixels, I will turn the gun to myAngle and fire a shot.
I’ve attached my robocode file for reference.