鉴于形状和效率,坦克模型使用外界盒(2d四边形)移动以及碰撞检测函数
简易的追踪Player ,有1/3概率横向追击 ,1/3概率纵向追击 ,1/6 横向调整位置 ,1/6纵向调整位置。切换方向的函数。
#define Random(x) (rand() % 6)
void ChangeDec(const int&x,const int &y)
{
switch (Random(x))
{
case 0: //对于x方向上的追击位移
case 1:
if (pointx < x&&pointx < BASEWIDTH)
CurrentDirect = D3DGraphics::RIGHT;
else if (pointx > x&&pointx>AIWIDTH)
{
CurrentDirect=D3DGraphics::LEFT;
}
break;
case 2: //对于y方向上的追击位移
case 3:
if (pointy < y&&pointy < BASEWIDTH)
CurrentDirect = D3DGraphics::DOWN;
else if (pointy > y&&pointy>AIHEIGHT)
{
CurrentDirect = D3DGraphics::UP;
}
break;
case 5: //对于x方向上的调整位移
if (pointx < x&&pointx < BASEWIDTH)
CurrentDirect = D3DGraphics::LEFT;
else if (pointx > x&&pointx > AIWIDTH)
{
CurrentDirect = D3DGraphics::RIGHT;
}
case 6: //对于y方向上的调整位移
if (pointy < y&&pointy < BASEWIDTH)
CurrentDirect = D3DGraphics::DOWN;
else if (pointy > y&&pointy > AIHEIGHT)
{
CurrentDirect = D3DGraphics::UP;
}
}
}
碰撞检测条件。正方形直接拿中心判断。 distance即中心距离。
case D3DGraphics::UP: //0 0 0
if (abs(this->getpointx()-temp[i].getpointx() ) < distance &&
this->getpointy() - temp[i].getpointy() < distance&&this->getpointy()>temp[i].getpointy())
temp是其他AI坦克数组 ,this是本AI。 32*32的四边形