-3

私はゲームを作っています、そして私には私のヒーローと敵がいます。たとえば、敵同士の距離が400の場合、敵をヒーローに追いかけたいと思います。どうすればそれを機能させることができますか。これが私がこれまでに得たものですが、それは機能しません。Objects :: calcAngle(Objects * obj)-2つのオブジェクトの中心点間の角度を計算します。

float Objects::calcAngle(Objects* obj){
    float dx = obj->X - this->X; 
    float dy = obj->Y - this->Y;
    float angle=atan(dy/dx)*180/PI;
    return angle;
}

void Enemy::Attack(mainCar* car){
    float angle=0;
    angle=this->calcAngle(car);
    if(this->Distance(car)<400){
        this->attack=true;
        this->heading=this->calcAngle(car)+90; 
        this->Vtri=abs(this->Vtri);
    } else if (this->Distance(car)>400) {
        this->attack=false;
    }
}

Vtriは移動速度です。方位は度単位の方向です。

説明されている場所へのリンクを教えていただければ、またはここで教えていただければ幸いです。プロジェクトを提出するのに2日あります。

4

1 に答える 1

2

オブジェクトをソースポイントからデスティネーションポイントに移動するには、いくつかのことが必要です。

  1. 現在位置のX座標とY座標
  2. 宛先位置のX座標とY座標
  3. ソースから目的地に移動する迎え角
  4. 宛先からソースに移動する速度

擬似コード:

dy = PositionDestination.Y - PositionCurrent.Y
dx = PositionDestination.X - PositionCurrent.X
angle = atan2(dy/dx)

vx = v * cos(angle)
vy = v * sin(angle)

PositionCurrent += Vector(vx, vy)

C#:

 // angle is the arctan placed in the correct quadrant of the Y difference and X difference
 float angleOfAttack = 
      (float)Math.Atan2((double)(PositionDestination.Y - PositionCurrent.Y), 
      (double)(PositionDestination.X - PositionCurrent.X));

 // velocity is the cos(angle*speed), sin(angle*speed)
 Vector2 velocity = 
      new Vector2((float)Math.Cos(angleOfAttack) * projectileMoveSpeed,
      (float)Math.Sin(angleOfAttack) * projectileMoveSpeed);

 // new position is +velocity
 PositionCurrent += velocity;

したがって、最初にソース座標と宛先座標に基づいて角度を取得します。次に、角度と速度に基づいて速度の方向と距離を取得します。最後に、速度ベクトルに基づいて現在の位置を新しい場所に移動します。

于 2012-04-22T20:59:00.800 に答える