Circular Targeting/Walkthrough

From Robowiki
Jump to navigation Jump to search

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

public void onScannedRobot(ScannedRobotEvent e)
{
	double turnrate=(e.getHeadingRadians()-lastEnemyHeading)/(getTime()-scantime);
	scantime=getTime();
	lastEnemyHeading=e.getHeadingRadians();
	...
	...
	...
}

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 anti-clockwise is a positive angle.

	absoluteBearing = e.getBearingRadians() + getHeadingRadians();
	double relativeX = e.getDistance()*Math.sin(absoluteBearing);
	double relativeY = e.getDistance()*Math.cos(absoluteBearing);

Since we assume the target will keep turning at the same turnrate every turn, we can predict it's position at any time by calculating a new velocity-vector, and add that to it's position. If we call the time that you scanned the target t=0, then it's position at t=1 will be:

(this is not a java code snippet)
( 1 )	newX(1) = relativeX + enemyVelocity*sin(initialHeading + turnrate)
( 2 )	newY(1) = relativeX + enemyVelocity*cos(initialHeading + turnrate)

at time t=2, it will be

(this is not a java code snippet)
( 3 )	newX(2) = relativeX + enemyVelocity*sin(initialHeading + turnrate) + enemyVelocity*sin(initialHeading + turnrate + turnrate)
( 4 )	newY(2) = relativeX + enemyVelocity*cos(initialHeading + turnrate) + enemyVelocity*cos(initialHeading + turnrate + turnrate)

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.

(this is not a java code snippet)
( 5 )	newX(T) = relativeX + http://kcl.kwokkie.net/klli87/images/sum.gif enemyVelocity*sin(initialHeading + t*turnrate)
( 6 )	newY(T) = relativeY + http://kcl.kwokkie.net/klli87/images/sum.gif enemyVelocity*cos(initialHeading + t*turnrate)


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

(this is not a java code snippet)
( 7 )	newX(T) = relativeX + enemyVelocity * http://kcl.kwokkie.net/klli87/images/integral.gif sin(initialHeading + t*turnrate) dt
( 8 )	newY(T) = relativeY + enemyVelocity * http://kcl.kwokkie.net/klli87/images/integral.gif cos(initialHeading + t*turnrate) dt


integrating from t=0 to t=T wich results in: (watch the +/- signs!! and where you need cosine and sine)

(this is not a java code snippet)
( 9 )	newX(T) = relativeX - (enemyVelocity/turnrate) * (cos(initialHeading + T*turnrate) - cos(initialHeading))
( 10 )	newY(T) = relativeY + (enemyVelocity/turnrate) * (sin(initialHeading + T*turnrate) - sin(initialHeading))


Just fill in T in the above and you can predict your circular-moving target's position at time T! Now to predict at wich 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 formulae (9) and (10), 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 sittingDuck, 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, wich, in turn, can be used to calculate an even better approximation of the actual time T, etc...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 master" about circular targeting with iteration. You can find it here: http://www-106.ibm.com/developerworks/library/j-circular/

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 timeframes, 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 don't need formulae (9) and (10) and will be performing the summations (5) and (6) step by step as well. You can find an example from this in CodeSnippets section. It's the source-code from my bot Nano Circular Linear Predictor, a bot I built just to prove that a circular predictor fits into a NanoBot. :-) Note that the step-by-step method can be used for both CircularTargeting and LinearTargeting, whereas the integrated formulae (9) and (10) are not suited for linear targeting. (Linear targeting means turnrate = 0)