# Circular Targeting/Walkthrough

Circular targeting is used to hit bots that often move in circles or large arcs. The first step is to measure the turn rate of your target bot by subtracting its current heading with its previous one. If you do not get a fresh scan of your target, you need to divide this value by the time between current scan and your last scan to obtain an average turn rate:

Now we need to know the current position of the target bot. To keep things easy, we only calculate the position of your target relative to your position. Keep in mind that in Robocode, "north" is zero degrees (radians) and clockwise is a positive angle. This is exactly the inverse of unit-circle math, where the x-axis is zero degrees and counter-clockwise is a positive angle.

Since we __assume__ the target will keep turning at the same turn rate every turn, we can predict its position at any time by calculating a new velocity vector and add that to its position. If we call the time that you scanned the target *t=0*, then its position at *t=1* will be:

at time *t=2*, it will be:

You can see that you can calculate the new (relative) X/Y position at t=T by adding a newly calculated velocity vector to its position on every *t*.

Space and time is not continuous in Robocode, but the discrete steps are small enough that we can approximate the above summation with an integral:

Integrating from t=0 to t=T which results in: (__watch the +/- signs!! and where you need cosine and sine__)

Just fill in *t* in the above and you can predict your circular-moving target's position at time *t*!

Now to predict at which time your bullet is going to hit your target. The time it would take for your bullet to travel to the target's *current* position is a good guess to start with. So your first guess would be *t = enemyDistance/bulletSpeed*. If we plug this T in last two formulas, we have a prediction of the target's new (relative) position at time *t*. But since the target will be moving around, rather than playing Sitting Duck, this is a rather poor approximation of the time *t* we are after. We can improve our approximation by calculating the time it would take for your bullet to reach the __previously predicted__ position. This new *t* can then in turn be used to calculate a better predicted position, which, in turn, can be used to calculate an even better approximation of the actual time *t*, etc. I myself have never used this iteration technique, but I remember reading somewhere (RoboCodeRepository forums?) that 4 or 5 of these iterations should be good enough. Alisdair Owens, the author of Nicator and the [OtherPeoplesRobots SnippetBot tutorial], wrote an article for "Secrets of the Robocode Masters" about circular targeting with iteration. You can find it here.

Another method is to start at *t=0* and keep increasing with *t* with 1. Every time you increase your *t*, compare the distance your bullet would travel in *t* ticks with the distance between you and the predicted position of your target at time *t*. Stop when your bullet reaches your target, and you'll have a predicted position! Because the position of the target is calculated step-by-step anyway, we can use the formula in step *t=2* as well.

You can find an example from this in Code Snippets section. It's the source code from Dummy's bot Nano Circular Linear Predictor, a bot he built just to prove that a circular predictor fits into a NanoBot.

Note that the step-by-step method can be used for both circular targeting and linear targeting, whereas the integrated formulas are not suited for linear targeting (linear targeting means *turnRate = 0*).