📄 wrapkeepdis.c
字号:
/**
* 围绕对手作来回的移动,并在此基础上添加了与对手保持一定距离的功能
* @author xiemin
*/
#include <airobot/c/SimpleRobot.h>
//在一个方向上最大的移动时间
#define MOVE_TIME 25
//理想距离
#define PREFER_DISTANCE 300
//移动的方向,这个变量的值只能取1和-1,1表示前进,-1表示后退
int direction = 1;
//当前在一个方向上的移动时间
long moveTime = 0;
//设置移动的方向
void setDirection(void);
//执行移动
void doMove(void);
//执行转动
void doTurn(void);
//得到圆心位置
void getCenter(double* x, double* y);
//得到用于保持理想距离的修正角deltaAngle
double getDeltaAngle(double centerX, double centerY);
void onTick(struct TickAction* action)
{
setDirection();
doMove();
doTurn();
}
void setDirection(void)
{
moveTime++;
if(moveTime>MOVE_TIME)
{
direction *= -1; //变换移动方向
moveTime = 0;
}
}
void doMove(void)
{
move(10*direction);
}
void doTurn(void)
{
double centerX, centerY, lineHeading, headingTo, deltaAngle, bea;
getCenter(¢erX, ¢erY);
lineHeading = heading(getX(), getY(), centerX, centerY);
headingTo = lineHeading + PI/2;
//为了保持距离用deltaAngle对headingTo进行修正
deltaAngle = getDeltaAngle(centerX, centerY);
headingTo -= deltaAngle;
bea = bearing(headingTo, getHeading());
turn(bea);
}
void getCenter(double* x, double* y)
{
struct Bot* bot = getFirstOpponent();
if(bot==NULL)
{
*x = getCourtWidth()/2;
*y = getCourtHeight()/2;
}
else
{
*x = bot->x;
*y = bot->y;
}
}
double getDeltaAngle(double centerX, double centerY)
{
double dis = distance(centerX, centerY, getX(), getY());
double deltaDistance = dis-PREFER_DISTANCE;
//deltaDistance = deltaDistance/fmax(distance, PREFER_DISTANCE);
deltaDistance = deltaDistance/500;
return direction*PI/3*deltaDistance;
}
//启动机器人程序
int main(int argC, char* argV[])
{
tickHook = onTick;
return startup(argC, argV);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -