副标题#e#
这篇小本了解让您深入领略圆周对准的事情道理。我们会从接头根基能力的事情道理开始,接着阐释一个简朴的迭代,它能显著提高精确性。我还提供 源 代码,它很容易适应在您本身的呆板人中事情。
事情道理
计较做圆周举动的呆板人的 change in x(x 偏向上的变革)和 change in y(y 偏向上的变革)的伪码相当简朴,假定您以弧度为单元举办计较:
change in x = cos(initialheading) * radius – cos(initialheading + changeinheading) * radius
change in y = sin(initialheading + changeinheading) * radius – sin (initialheading) * radius
式中 initialheading是敌方呆板人在初始位置的偏向,子弹航行期间的偏向 变革为 changeinheading,我们假定它以 radius为圆周半径举动。
计较须要的数据
图 1 说明白我们需要的大部门数据: r是呆板人举动所绕的圆周半径,偏向 变革为 a,而 v则是敌方呆板人举动的即时速度。
图 1. 沿圆周移动
为了要精确对准仇人,我们需要某些特定的数据:呆板人当前的偏向,每转 的偏向变革,当前的速度,我们的子弹达到的时刻。我们可以利用这些数据计较 出仇人转圈的圆半径,以及它最后的偏向(即,我们的子弹达到仇人的瞬间仇人 的偏向)。我们计较子弹击中位置的要领如下:
每转的偏向变革:我们用 headingchangeperturn = (heading2 – heading1)/time 获得这个值,个中 time是两次丈量的隔断时间。您还必需使结 果尺度化,如下面代码中所示。
子弹时间:我们利用简朴的 time = getTime()+(range/(20- (3*firepower))) 就可以满意需要了。个中 range是发射时我们和仇人之间的距 离,而 firepower是我们打算利用的射击火力。假定子弹击中时,方针到我方的 间隔稳定,这个假设并不符合,可是我们在本文后头的内容中开拓出迭代之前, 有它就足够了。
半径:我们用 radius = velocity/headingchangeperturn 得出这个值。
#p#副标题#e#
代码
圆周路径预测只需要清单 1。可是,请留意假如方针的偏向变革很小,那么 就要利用直线对准。由于一旦半径过上将导致存储它所用的 double 溢出,因而 我们利用这种方法来缓解这一风险。不外条件是偏向变革较量小,我们也就不必 太担忧了。
清单 1. 圆周对准代码
public Point2D.Double guessPosition(long when) {
/**time is when our scan data was produced. when is the time
that we think the bullet will reach the target. diff is the
difference between the two **/
double diff = when - time;
double newX, newY;
/**if there is a significant change in heading, use circular
path prediction**/
if (Math.abs(changehead) > 0.00001) {
double radius = speed/changehead;
double tothead = diff * changehead;
newY = y + (Math.sin(heading + tothead) * radius) -
(Math.sin(heading) * radius);
newX = x + (Math.cos(heading) * radius) -
(Math.cos(heading + tothead) * radius);
}
/**if the change in heading is insignificant, use linear
path prediction**/
else {
newY = y + Math.cos(heading) * speed * diff;
newX = x + Math.sin(heading) * speed * diff;
}
return new Point2D.Double(newX, newY);
}
改造功效
假如您已经试过清单 1 中的代码,那么您会发明搪塞 spinbot(沿圆周移动 的样本呆板人)的环境明明许多几何了,但您很大概还会留意到,呆板人发出的炮弹 中仍有不少没有掷中方针。这不光单是因为瞄的禁绝;还要归因于您对子弹要花 费多长时间才气达到方针的估算禁绝。要提高您的技能,可以通过利用一个很是 简朴的迭代来估算一下时间,然后把这个估算值送入对准系统获得当子弹达到目 标时您与方针之间的间隔的更准确的估算值。重复执行屡次这一步,您将获得近 乎完美的估值。
清单 2. 迭代代码
/**This function predicts the time of the intersection between the
bullet and the target based on a simple iteration. It then moves
the gun to the correct angle to fire on the target.**/
void doGun() {
long time;
long nextTime;
Point2D.Double p;
p = new Point2D.Double(target.x, target.y);
for (int i = 0; i < 10; i++){
nextTime =
(intMath.round((getRange(getX(),getY(),p.x,p.y)/(20- (3*firePower))));
time = getTime() + nextTime;
p = target.guessPosition(time);
}
/**Turn the gun to the correct angle**/
double gunOffset = getGunHeadingRadians() -
(Math.PI/2 - Math.atan2(p.y - getY(), p.x - getX()));
setTurnGunLeftRadians(normaliseBearing(gunOffset));
}
double normaliseBearing(double ang) {
if (ang > Math.PI)
ang -= 2*PI;
if (ang < -Math.PI)
ang += 2*Math.PI;
return ang;
}
public double getrange(double x1,double y1, double x2,double y2) {
double x = x2-x1;
double y = y2-y1;
double h = Math.sqrt( x*x + y*y );
return h;
}
改造圆周对准的机能
#p#分页标题#e#
作为对准系统,圆周对准根基成形了;也就是说,既在我们本领所及范畴内 又能在此基本上更进一步的事并不多。可是,能改进机能的小窜改有两处:
平均速度:您也许愿意保持一个赔偿加快效应的移动平平均速度,而不是根 据方针的最后速度举办计较。
每一回合偏向的平均变革:丈量在每一回合内方针偏向变革绝对量的平均值 有助于我们搪塞那些不如 spinbot 有纪律的方针。