(AI-TANK)打击作圆周运动的机器人

/**
 * 这个例子机器人演示了如何打击作圆周运动的机器人。
 * 这个机器人用的迭代算法与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);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值