Robo Home | Movement | Changes | Preferences | AllPages

Showing revision 11

A complete bot that moves between destinations (selected at random) on the battle field and shoots on sight of an enemy.

If you're to lazy to copy-paste the source; Download the bot at http://www.robocoderepository.com/BotDetail.jsp?id=1456
package wiki;
import robocode.*;
import java.awt.geom.Point2D;

// GoToBot, demonstrates using a goTo() function to move around
// $Id: GoToBot.java,v 1.2 2003/05/15 14:30:46 peter Exp $

public class GoToBot extends AdvancedRobot {
    private Point2D[] destinations = {
        new Point2D.Double(90, 90),
        new Point2D.Double(790, 790),
        new Point2D.Double(400, 400),
        new Point2D.Double(90, 540),
        new Point2D.Double(740, 90),
        new Point2D.Double(500, 200),
        new Point2D.Double(200, 500)

    public void run() {

    public void onScannedRobot(ScannedRobotEvent e) {
        setTurnGunRightRadians(0 - getGunTurnRemainingRadians());
        if (Math.abs(getDistanceRemaining()) < 5) {
            goTo(destinations[(int)(Math.random() * destinations.length)]);

    private void goTo(Point2D destination) {
        Point2D location = new Point2D.Double(getX(), getY());
        double distance = location.distance(destination);
        double angle = normalRelativeAngle(absoluteBearing(location, destination) - getHeading());
        if (Math.abs(angle) > 90) {
            distance *= -1;
            if (angle > 0) {
                angle -= 180;
            else {
                angle += 180;

    private double absoluteBearing(Point2D source, Point2D target) {
        return Math.toDegrees(Math.atan2(target.getX() - source.getX(), target.getY() - source.getY()));

    private double normalRelativeAngle(double angle) {
        angle = Math.toRadians(angle);
        return Math.toDegrees(Math.atan2(Math.sin(angle), Math.cos(angle))); 
Note that it uses the fancier goTo() function from /CodeSnippetGoTo. And I just had to make it shoot as well. =) -- PEZ

What the... HEY! You stole CircleBot's radar/aiming algorithm! (Except that he does it in a slightly different way...) j/k -- Kawigi

Ok, so something i have been desperately trying to work out and failing is what the difference is between fire(), and setFire(). I know that setFire() only works when execute() is called, but i dont see why you'd use it on a simple bot like this. I can see it's use on larger bots where you make it fire only after the gun has moved, but in this example the gun is constantly moving. Is it because if you call setFire() it doesnt interefere with any other actions as it is put in it's own thread??? -- Brainfade

I use setFire() out of habit. Sometimes when I started out with Robocode putting fire() in the wrong place made my bot stop to work completely. I have no clue as to why though. And as you mention it I don't know why the above works without a call to execute() either. =) -- PEZ

I changed the radar/aiming "algorithm" slightly so that it now doesn't look exactly like CircleBot's. =) -- PEZ

Ah - a "nano-gun-lock" - I should add that into CircleBot :-p -- Kawigi

It should function entirely without the while(true) there. -- Kuuran

Aha! If you're sure about it please feel free to remove that line. -- PEZ

I've removed the while(true) {} line, I tested it locally first and it worked fine. -- Kuuran

Robo Home | Movement | Changes | Preferences | AllPages
Edit revision 11 of this page | View other revisions | View current revision
Edited May 24, 2003 8:00 EST by Kuuran (diff)