Does anyone know how I could make a spiral motion following the Fibonacci pattern around a point in Robocode? I have methods like setTurnRight (double), setAhead (double), getX () and getY ().
I tried to make a simple spiral, without the required standard, that way, but it did not work ... It was more like a circle.
this.setAhead(this.direction * Double.POSITIVE_INFINITY);
if (this.direction == 1) {
this.setTurnRight(Utils.normalRelativeAngleDegrees(this.enemy.getBearing() + 60));
} else {
this.setTurnRight(Utils.normalRelativeAngleDegrees(this.enemy.getBearing() + 120));
}
physics of the game: http://robowiki.net/wiki/Robocode/Game_Physics
Here is a working run method to make a bot follow a logarithmic spiral and I believe it is a close approximation of the golden spiral (which is the spiral that can be approximated with the Fibonacci numbers).
public void run() {
double v = 5;
double c = Math.PI*2;
double a = .1;
double b = .0053468;
setMaxVelocity(v);
setAhead(100*999);
setTurnRight(360*999);
while(true)
{
double t = getTime();
double f = a*Math.pow(Math.E,b*t);
double w = v/(c*f);
setMaxTurnRate(w);
execute();
System.out.println(t+"\t"+w);
}
}
To move in a circle (trivial spiral), you keep constant speed (how fast the bot is moving) and constant revolution speed (how fast the bot is turning). There are several ways to go from this trivial spiral movement to something more interesting. The simplest way to move in a spiral is to keep constant speed and vary the revolution speed. This answer from the game development exchange gives a good walk through on how to get an approximate equation for revolution speed.
w = v / (2*pi*t)
or w = v / (2*pi*f(t))
where:
This equation gives a way to move along a spiral and we can choose any spiral we want by specifying a f(t). To get the correct radius function for the golden spiral, check out this wiki page about the golden spiral. It gives this equation:
r = a*e^(b*theta)
or in other words f(t) = a*e^(b*t)
where:
All that is left is to incorporate this code into your bot and choose your own values for a and v. v will determine the speed of the bot, so a larger v is a good idea (max is 10) and since the max for w is 8, you should scale a accordingly so that w stays between 0 and 8 for as long as possible (which is why I've included the println).
[NOTE: I couldn't think of an easy way to superimpose the golden spiral on the bot's path to check it's accuracy. So while it is clearly a logarithmic spiral, I am unsure to what degree it approximates the desired golden spiral ]