Difference between revisions of "Circular Targeting"

From Robowiki
Jump to navigation Jump to search
(adding category "Targeting")
(Removed a dead link and replaced another dead link with an updated one.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
== Description ==
 
== Description ==
  
A method of [[Targeting]] which assumes that the target will continue moving with the same turn rate and at the same speed.
+
A method of [[targeting]] which assumes that the target will continue moving with the same turn rate and at the same speed.
  
 
== Example ==
 
== Example ==
  
 
This is the code used in [[WaveSurfing Challenge Bot/C|WaveSurfing Challenge Bot C]]:
 
This is the code used in [[WaveSurfing Challenge Bot/C|WaveSurfing Challenge Bot C]]:
<pre>
+
<syntaxhighlight>
 
//Add import robocode.util.* for Utils and import java.awt.geom.* for Point2D
 
//Add import robocode.util.* for Utils and import java.awt.geom.* for Point2D
 
//This code goes in your onScannedRobot() event handler
 
//This code goes in your onScannedRobot() event handler
Line 50: Line 50:
 
     theta - getGunHeadingRadians()));
 
     theta - getGunHeadingRadians()));
 
fire(3);
 
fire(3);
</pre>
+
</syntaxhighlight>
  
 
== See also ==
 
== See also ==
  
* [[Circular Targeting/Walkthrough]] An in-depth example of creating a Circular Targeting algorithm in Robocode, courtesy of  [[Dummy]].
+
* [[Circular Targeting/Walkthrough]] - An in-depth example of creating a circular targeting algorithm in Robocode, authored by [[Dummy]].
* [[CodeSnippets/Nano Circular Linear Predictor | NanoBot sized Circular Targeting example code]]
+
* Alisdair Owen's [https://www.ibm.com/developerworks/library/j-circular/index.html circular targeting tutorial] with iteration.
* Alisdair Owen's [http://www-106.ibm.com/developerworks/library/j-circular/ circular targeting tutorial] with iteration.
 
  
 +
{{Targeting Navbox}}
 
[[Category:Simple Targeting Strategies]]
 
[[Category:Simple Targeting Strategies]]
 
[[Category:Code Snippets]]
 
[[Category:Code Snippets]]
[[Category:Targeting]]
 

Latest revision as of 12:46, 24 December 2017

Description

A method of targeting which assumes that the target will continue moving with the same turn rate and at the same speed.

Example

This is the code used in WaveSurfing Challenge Bot C:

//Add import robocode.util.* for Utils and import java.awt.geom.* for Point2D
//This code goes in your onScannedRobot() event handler

double bulletPower = Math.min(3.0,getEnergy());
double myX = getX();
double myY = getY();
double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
double enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing);
double enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing);
double enemyHeading = e.getHeadingRadians();
double enemyHeadingChange = enemyHeading - oldEnemyHeading;
double enemyVelocity = e.getVelocity();
oldEnemyHeading = enemyHeading;

double deltaTime = 0;
double battleFieldHeight = getBattleFieldHeight(), 
       battleFieldWidth = getBattleFieldWidth();
double predictedX = enemyX, predictedY = enemyY;
while((++deltaTime) * (20.0 - 3.0 * bulletPower) < 
      Point2D.Double.distance(myX, myY, predictedX, predictedY)){		
	predictedX += Math.sin(enemyHeading) * enemyVelocity;
	predictedY += Math.cos(enemyHeading) * enemyVelocity;
	enemyHeading += enemyHeadingChange;
	if(	predictedX < 18.0 
		|| predictedY < 18.0
		|| predictedX > battleFieldWidth - 18.0
		|| predictedY > battleFieldHeight - 18.0){

		predictedX = Math.min(Math.max(18.0, predictedX), 
		    battleFieldWidth - 18.0);	
		predictedY = Math.min(Math.max(18.0, predictedY), 
		    battleFieldHeight - 18.0);
		break;
	}
}
double theta = Utils.normalAbsoluteAngle(Math.atan2(
    predictedX - getX(), predictedY - getY()));

setTurnRadarRightRadians(Utils.normalRelativeAngle(
    absoluteBearing - getRadarHeadingRadians()));
setTurnGunRightRadians(Utils.normalRelativeAngle(
    theta - getGunHeadingRadians()));
fire(3);

See also