/**
* Name:MOVE LINE AND RETURN
* Written by pt
* Compiler:Microsoft Visual C++
*/
#include <airobot/c/SimpleRobot.h>
//运动的速度
double moveVelocity;
//运动的方向,headingTo的作用就是控制与自身的位置夹角为PI的奇数倍,也就是让tank最终比撞墙的速度方向相差PI
//即是转向的意思
double headingTo;
/**
* 每个单位时间都会触发
*/
void onTick(struct TickAction* action)
{
//求headingTo和tank自身位置的夹角
double angle = bearing(headingTo, getHeading());
//判断夹角是否趋近0
if (fabs(angle) < 0.0001)
{
//是,停止转动,直线移动
turn(0);
move(moveVelocity);
}
else
{
//否,停止移动,旋转angle这个夹角的度数
move(0);
turn(angle);
}
}
/**
* 当开始一轮新的比赛时触发
*/
void onRoundBegin(struct RoundBeginAction* action)
{
//初始化headingTo为0,移动速度为8
headingTo = 0;
moveVelocity = 8;
}
/**
* 当撞到墙时触发
*/
void onHitWall(struct HitWallAction* action)
{
//撞墙,一直让headingTo按PI递增
headingTo += PI;
}
/**
* 机器人程序入口
*/
int main(int argC, char* argV[])
{
tickHook = onTick;
roundBeginHook = onRoundBegin;
hitWallHook = onHitWall;
return startup(argC, argV);
}
(AI-TANK)走直线,撞墙折返
最新推荐文章于 2015-09-21 17:10:00 发布