/**
* 这个例子机器人演示了如何打击作圆周运动的机器人。
* 这个机器人用的迭代算法与FireLine类似,不过它记录了对手上个单位时间的方向,
* 然后利用这个方向与对手当前的方向作比较,推算出对手每个单位时间的转动度数,
* 并以此来计算对手下个单位时间将出现的位置。
* @author xiemin
*/
#include <airobot/c/SimpleRobot.h>
//开火时的炮弹能量
#define POWER 0.5
//对手上个单位时间的方向
double lastHeading;
void onTick(struct TickAction* action)
{
double nextX, nextY, dis, bea, nextHeading;
long time;
struct Bot* bot = getFirstOpponent();
if(bot==NULL) return;
//计算机器人两个单位时间的方向的夹角
bea = bearing(bot->heading, lastHeading);
//初始化迭代
nextHeading = bot->heading;
nextX = bot->x;
nextY = bot->y;
time = 0;
//进入迭代
while(1)
{
//判断子弹在time时间后能否打到在next位置的对手
double dis = distance(nextX, nextY, getX(), getY());
if(dis/getBulletVelocity(POWER)<=time)
{
//能打到,迭代结束
fireOnPoint(nextX, nextY, POWER); //开火
break;
}
//进入下一次迭代
time ++;
nextHeading += bea;
//计算在下个时间对手的位置
nextPoint(nextX, nextY, nextHeading, bot->velocity, &nextX, &nextY);
}
lastHeading = bot->heading;
}
//启动机器人程序
int main(int argC, char* argV[])
{
tickHook = onTick;
return startup(argC, argV);
}
(AI-TANK)打击作圆周运动的机器人
最新推荐文章于 2020-12-18 10:45:51 发布