Difference between revisions of "Linear Targeting"

From Robowiki
Jump to navigation Jump to search
m (forgot category)
m (fixing links and descriptions under "see also")
Line 54: Line 54:
 
== See Also ==
 
== See Also ==
  
* [[/BuggyImplementations]] - There are much faster algorithms to do this, but they don't seem to be correct all the time. If anyone can correct them it would be much appreciated.
+
* [[/NanoBot Linear Targeting|NanoBot Linear Targeting]] - An extremely small implementation of linear targeting, ideal for use in [[NanoBots]].
* [[/NanoLinearTargeting]]
+
* [[/Buggy Implementations|Buggy Implementations of Linear Targeting]] - There are much faster algorithms to do this, but they don't seem to be correct all the time. If anyone can correct them it would be much appreciated.
  
 
[[Category:Simple Targeting Strategies]]
 
[[Category:Simple Targeting Strategies]]

Revision as of 23:49, 11 November 2007

Description

A method of Targeting which assumes that the target will continue in the same direction at the same speed.

Example

This is the code used in WaveSurfing Challenge Bot B:

//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 enemyVelocity = e.getVelocity();


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;
	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(bulletPower);
  • NOTE: This algorithm assumes that bots will come to a stop when they reach a wall.
  • NOTE: There is a simpler way to do linear targeting with the help of trigonometry. It has the disadvantage of not knowing if is aiming at a spot off of the battlefield. It has the very real advantage of being non-iterative and does not need to estimate the time for the bullet to reach the enemy.

See Also