📄 antigravitymove.cpp
字号:
#include <airobot/cpp/SimpleRobot.hpp>
#include <airobot/commons/Math.hpp>
class AntiGravityMove: public SimpleRobot{
public:
void onTick(TickAction* action) {
if(getFirstOpponent() == null){
return;
}
double min_force = Double.POSITIVE_INFINITY;
double prefer_heading = 0;
double seachDist = 80;
for(double h=0; h<Math::PI*2; h+=Math::toRadians(5)){
Point2D p = MathUtils::nextPoint(getLocation(),h, seachDist);
if(!inCourt(p, RADIUS + 5)){
continue;
}
double force = getGravity(p);
if(force < min_force){
min_force = force;
prefer_heading = h;
}
}
//找到势点相对自己的角度
double bearing = MathUtils::bearing(prefer_heading, getHeading());
double velocity = Math::abs(bearing)>Math::PI/2?1:MAX_MOVE_VELOCITY;
turn(bearing);
move(velocity);
}
double getGravity(Point2D& p){
vector<Bot*> enemies = getOpponents();
double force = 0;
//计算所有势点对自己的作用力force的衰减系数
//并分别计算各方向作用力的和
for(int i=0; i<enemies.size(); i++){
Bot bot = (Bot)enemies[i];
if(isAlive(bot))
force += 1/p->distance(getLocation());
}
Point2D center = new Point2D (getCourtWidth()/2, getCourtHeight()/2);
//下面的4行code的作用是避开墙.
//只有当机器人离墙足够近时才会发生作用
double wallWeigh = 0.1;
force += 1/center->distance(p) * wallWeigh;
force += 1/p->getX() * wallWeigh;
force += 1/( getCourtWidth() – p->getX()) * wallWeigh;
force += 1/p->getY() * wallWeigh;
force += 1/( getCourtHeight() – p->getY()) * wallWeigh;
return force;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -