Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Secrets from the Robocode masters: Predictive targeting

Mr. Simon Parker (evilsimon@yahoo.com.au), Design Engineer, Neopraxis Pty. Ltd.
Simon Parker is a Design Engineer for Neopraxis Pty. Ltd., Sydney, Australia, and writes embedded code for biomedical systems.

Summary:  This tip was published in the "Cloak and turret: Learn secrets from the Robocode masters" article in the May 2002 issue of the IBM developerWorks journal.

Date:  May 2002
Level:  Introductory

Activity:  17108 views
Comments:  

All successful targeting and shooting of enemy robots requires an algorithm to fire bullets at the place where you predict that an enemy robot will be at a future point in time. This algorithm can be used for linear, circular, and oscillating predictive targeting. And if you have a function that returns the position of the enemy at a future point in time, you can use the algorithm to calculate the impact point, the firing angle, the impact position, and the impact time.

This algorithm implements the secant method to numerically solve the impact time. Once this impact time is known, our predictive function obtains the impact position. Then we fire at that position.

The Intercept class shown in Listing 1 assumes that the robot is traveling in a straight line from its current position at its current velocity.


Listing 1. Using the Intercept class
                
public class Intercept {
 public Coordinate impactPoint = new Coordinate(0,0);
 public double bulletHeading_deg;

 protected Coordinate bulletStartingPoint = new Coordinate();
 protected Coordinate targetStartingPoint = new Coordinate();
 public double targetHeading;
 public double targetVelocity;
 public double bulletPower;
 public double angleThreshold;
 public double distance;

 protected double impactTime;
 protected double angularVelocity_rad_per_sec;

 public void calculate (

 // Initial bullet position x coordinate 
 double xb, 
 // Initial bullet position y coordinate
 double yb, 
 // Initial target position x coordinate
 double xt, 
 // Initial target position y coordinate
 double yt, 
 // Target heading
 double tHeading, 
 // Target velocity
 double vt, 
 // Power of the bullet that we will be firing
 double bPower, 
 // Angular velocity of the target
 double angularVelocity_deg_per_sec 
)
{
angularVelocity_rad_per_sec = 
 Math.toRadians(angularVelocity_deg_per_sec);

bulletStartingPoint.set(xb, yb);
targetStartingPoint.set(xt, yt);

targetHeading = tHeading;
targetVelocity = vt;
bulletPower = bPower;
double vb = 20-3*bulletPower;

double dX,dY;

// Start with initial guesses at 10 and 20 ticks
impactTime = getImpactTime(10, 20, 0.01); 
impactPoint = getEstimatedPosition(impactTime);

dX = (impactPoint.x - bulletStartingPoint.x);
dY = (impactPoint.y - bulletStartingPoint.y);

distance = Math.sqrt(dX*dX+dY*dY);

bulletHeading_deg = Math.toDegrees(Math.atan2(dX,dY));
angleThreshold = Math.toDegrees
 (Math.atan(ROBOT_RADIUS/distance));
}

protected Coordinate getEstimatedPosition(double time) {

double x = targetStartingPoint.x + 
   targetVelocity * time * Math.sin(Math.toRadians(targetHeading));
double y = targetStartingPoint.y + 
   targetVelocity * time * Math.cos(Math.toRadians(targetHeading));
return new Coordinate(x,y);
}

private double f(double time) {

double vb = 20-3*bulletPower;

Coordinate targetPosition = getEstimatedPosition(time);
double dX = (targetPosition.x - bulletStartingPoint.x);
double dY = (targetPosition.y - bulletStartingPoint.y);

return Math.sqrt(dX*dX + dY*dY) - vb * time;
}

private double getImpactTime(double t0, 
  double t1, double accuracy) {

double X = t1;
double lastX = t0;
int iterationCount = 0;
double lastfX = f(lastX);

while ((Math.abs(X - lastX) >= accuracy) && 
  (iterationCount < 15)) {

iterationCount++;
double fX = f(X);

if ((fX-lastfX) == 0.0) break;

double nextX = X - fX*(X-lastX)/(fX-lastfX);
lastX = X;
X = nextX;
lastfX = fX;
}

return X;
}
}
    

The great thing about the Intercept class is that it can be easily reused to calculate the firing angle for circular motion. To do this, write a CircularIntercept class that inherits from the Intercept class, and overwrite the getEstimatedPosition() method. Listing 2 shows the code for the CircularIntercept class:


Listing 2. CircularIntercept class
                
public class CircularIntercept extends Intercept {
 protected Coordinate getEstimatedPosition(double time) {
  if (Math.abs(angularVelocity_rad_per_sec) 
   <= Math.toRadians(0.1)) {
  return super.getEstimatedPosition(time);
 }

    double initialTargetHeading = Math.toRadians(targetHeading);
    double finalTargetHeading   = initialTargetHeading +  
     angularVelocity_rad_per_sec * time;
    double x = targetStartingPoint.x - targetVelocity /
     angularVelocity_rad_per_sec *(Math.cos(finalTargetHeading) - 
     Math.cos(initialTargetHeading));
    double y = targetStartingPoint.y - targetVelocity / 
     angularVelocity_rad_per_sec *
     (Math.sin(initialTargetHeading) - 
     Math.sin(finalTargetHeading));
    return new Coordinate(x,y);
}

}

Listing 3 shows an example of using the Intercept class. It assumes that we calculated the current position, heading, and velocity of the target, as well as the power of the bullet that we will be firing.


Listing 3. Using the Intercept class
                
Intercept intercept = new Intercept();
intercept.calculate
(
  ourRobotPositionX,
  ourRobotPositionY,
  currentTargetPositionX,
  currentTargetPositionY,
  curentTargetHeading_deg,
  currentTargetVelocity,
  bulletPower,
  0 // Angular velocity
);

// Helper function that converts any angle into  
// an angle between +180 and -180 degrees.
double turnAngle = normalRelativeAngle
 (intercept.bulletHeading_deg - robot.getGunHeading());

// Move gun to target angle
robot.setTurnGunRight(turnAngle);

if (Math.abs(turnAngle) <= intercept.angleThreshold) {
  // Ensure that the gun is pointing at the correct angle
  if (
    (intercept.impactPoint.x > 0) &&
 (intercept.impactPoint.x < getBattleFieldWidth()) &&
 (intercept.impactPoint.y > 0) &&
 (intercept.impactPoint.y < getBattleFieldHeight())
) {
    // Ensure that the predicted impact point is within 
    // the battlefield
    fire(bulletPower);
  }
}
}

This firing strategy has proven very successful in both of my robots (JollyNinja and MadHatter). Encapsulating the intercept in its own class and subclassing it for different prediction algorithms allows me to usually hit other robots more than they hit me.


Resources

  • Read all of the Secrets from the Robocode masters . This page will be updated as new tips become available.

  • Robocode's creator, Mathew Nelson, maintains the official Robocode site. This should be the first stop for anyone serious about Robocode.

  • RoboLeague by Christian Schnell is a league and season manager for Robocode. It ensures that all possible groupings indeed play their matches, manages the results, and produces HTML status reports.

  • "Rock 'em, sock 'em Robocode" (developerWorks, January 2002) disarms Robocode and starts you on your way to building your own customized lean, mean, fighting machine.

  • In "Rock 'em, sock 'em Robocode: Round 2" (developerWorks, May 2002), Sing Li looks at advanced robot construction and team play.

  • New to Java? Check out "Introduction to Java programming" (developerWorks, November 2004), a tutorial that steps you through the fundamentals of Java language programming.

  • developerWorks: Hundreds of articles about every aspect of Java programming.

About the author

Simon Parker is a Design Engineer for Neopraxis Pty. Ltd., Sydney, Australia, and writes embedded code for biomedical systems.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=242032
ArticleTitle=Secrets from the Robocode masters: Predictive targeting
publish-date=052002
author1-email=evilsimon@yahoo.com.au
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers