bearing + (PI/2)); //every turn move to circle strafe the enemy
//ÿһʱ¼äÖÜÆÚÒÔµÐÈËΪÖÐÐÄÈÆÔ²Ô˶¯
}
/*
* this scanner method allows us to make our scanner track our target.
* it will track to where our target is at the moment, and some further
* in case the target has moved. This way we always get up to the minute
* information on our target À×´ïËø¶¨Ä¿±ê
*/
void doScanner()
{
double radarOffset; //À×´ïÆ«ÒÆÁ¿
if (getTime() - target.ctime > 4) //???whyÀ´»ØÉ¨ÁË4¸ö»ØºÏ¶¼Ã»É¨µ½ÒâζʧȥÁËÄ¿±ê£¬ÔÙȫɨһ±é
{
//if we haven't seen anybody for a bit....
radarOffset = 360; //rotate the radar to find a target
}
else
{
//next is the amount we need to rotate the radar by to scan where the target is now
//ͨ¹ýɨÃè¾ö¶¨À×´ïÐýתµÄ»¡¶È£¬"¼û»ù±¾ÔÀí·½ÏòÆÊÎö¼°Ä¿±êËø¶¨www.robochina.org".À״ﻡ¶È-µÐÈ˽Ƕȵõ½Á½ÕßÏà²îΪÐýתֵ
radarOffset = getRadarHeadingRadians() - absbearing(getX(),getY(),target.x,target.y);
//this adds or subtracts small amounts from the bearing for the radar to produce the wobbling
//and make sure we don't lose the target
//Ôڵõ½µÄ½Ç¶ÈÖмӻò¼õÒ»µã½Ç¶È£¬ÈÃÀ×´ïºÜСµÄ·¶Î§ÄÚ°Ú¶ø²»Ê§È¥Ä¿±ê
if (radarOffset < 0)
radarOffset -= PI/8; //(0.375)
else
radarOffset += PI/8;
}
//turn the radar
setTurnRadarLeftRadians(NormaliseBearing(radarOffset)); //×óתµ÷Õûת¶¯½Ç¶Èµ½PIÄÚ
}
/*
* This simple method moves the gun to the bearing that we predict the
* enemy will be by the time our bullet will get there.
* the 'absbearing' method can be found in the helper functions section
* the nextX and nextY method can be found in the 'Enemy' class description
*/
void doGun()
{
//works out how long it would take a bullet to travel to where the enemy is *now*
//this is the best estimation we have
//¼ÆËã×Óµ¯µ½´ïÄ¿±êµÄʱ¼ä³¤speed = 20 - 3 * power;ÓмÆË㹫ʽ,¾àÀë³ýËÙ¶È=ʱ¼ä
long time = getTime() + (int)(target.distance/(20-(3*firePower)));
//offsets the gun by the angle to the next shot based on linear targeting provided by the enemy class
//ÒÔÖ±ÏßΪĿ±ê£¬Æ«ÒÆ×Óµ¯ÏÂÒ»´Î·¢ÉäµÄ½Ç¶È¡££¨ÕâÑùÈÃ×Óµ¯Éä¿ÕµÄ¼¸ÂʼõÉÙ¡£µ«¶Ô¸¶²»¶¯µÄºÍ×öÔ²Ô˶¯µÄ»úÆ÷ÈËÓÐÎÊÌ⣩
//target.guesssX(),target.guessY()ΪĿ±êÒÆ¶¯ºóµÄ×ø±ê
double gunOffset = getGunHeadingRadians() - absbearing(getX(),getY(),target.guessX(time),target.guessY(time));
setTurnGunLeftRadians(NormaliseBearing(gunOffset)); //µ÷ÕûÏà¶Ô½Ç¶Èµ½2PIÄÚ
}
/*
* This set of helper methods. You may find several of these very useful
* They include the ability to find the angle to a point.
*/
//if a bearing is not within the -pi to pi range, alters it to provide the shortest angle
double NormaliseBearing(double ang)
{
if (ang > PI)
ang -= 2*PI;
if (ang < -PI)
ang += 2*PI;
return ang;
}
//if a heading is not within the 0 to 2pi range, alters it to provide the shortest angle
double NormaliseHeading(double ang)
{
if (ang > 2*PI)
ang -= 2*PI;
if (ang < 0)
ang += 2*PI;
return ang;
}
//returns the distance between two x,y coordinates '**'
//ÒÔÁ½±ß³¤ÇóµÃÓë¶ÔÊÖÖ®¼äµÄ¾àÀë
public double getrange( double x1,double y1, double x2,double y2 )
{
double xo = x2-x1;
double yo = y2-y1;
double h = Math.sqrt( xo*xo + yo*yo );
return h;
}
//gets the absolute bearing between to x,y coordinates
//¸ù¾Ýx,yµÄ×ø±êÇó³ö¾ø¶Ô½Ç¶È£¬¼û"×ø±êËø¶¨"ÀûÓÃÖ±½Ç×ø±êϵÀ´·´Çó³ö½Ç¶È¡££¿£¿£¿
public double absbearing( double x1,double y1, double x2,double y2 )
{
double xo = x2-x1;
double yo = y2-y1;
double h = getrange( x1,y1, x2,y2 );
if( xo > 0 && yo > 0 )
{
//·´ÕýÏÒ¶¨Ò壬¶Ô±ß³ýб±ßµÃ»¡¶È.ÒÔrobocodeÖеľø¶Ô·½Ïòϵ¼°×ø±êϵ²ÎÕÕ
//x,yΪÕýÓÒÉϽÇΪ0-90,xÕýy¸ºÓÒϽÇΪ90-180,x,y¸º×óϽÇ180-270,x¸º£¬yÕýÓÒÉϽÇ270-360
//´Ë´¦ÒªÀí½ârobocodeÖеľø¶Ô½Ç¶ÈÊÇÉÏΪ0,ÏÂΪ180£

¡¾