0

私が作成している 3D ゲームの敵クラスを書いており、敵がプレイヤーを追跡するように取り組んでいます。基本的に、敵はフレームごとにプレイヤーの方向に少しずつ回転し、フレームごとに少しずつ前進します。以下のコードに示すように、Lerping を使用してこれを達成しようとしましたが、うまく動作しないようです。遊んでいると敵が視界に入ったり、追いかけてきたりしません。これが私の敵のクラスからの私のコードです。

注: p は追跡しているプレイヤー オブジェクトへの参照、world は敵オブジェクトのワールド マトリックス、quaternion はこの敵オブジェクトのクォータニオンです。

私の現在の戦略は、敵の前方ベクトルとプレイヤーの位置 vector3 の間の方向ベクトルを見つけてから、速度変数によって決定される量だけそれを lerping することです。次に、敵の前方ベクトルと、midVector と呼ぶ新しい lerped ベクトルによって決定される平面に対する垂直ベクトルを見つけようとします。次に、プレイヤーがその垂直ベクトルを中心に回転するようにクォータニオンを更新します。以下にコードを示します。

 //here I get the direction vector in between where my enemy is pointing and where the player is located at
       Vector3 midVector = Vector3.Lerp(Vector3.Normalize(world.Forward), Vector3.Normalize(Vector3.Subtract(p.position,this.position)), velocity);
        //here I get the vector perpendicular to this middle vector and my forward vector
       Vector3 perp=Vector3.Normalize(Vector3.Cross(midVector, Vector3.Normalize(world.Forward)));

        //here I am looking at the enemy's quaternion and I am trying to rotate it about the axis (my perp vector) with an angle that I determine which is in between where the enemy object is facing and the midVector

       quaternion = Quaternion.CreateFromAxisAngle(perp, (float)Math.Acos(Vector3.Dot(world.Forward,midVector)));
        //here I am simply scaling the enemy's world matrix, implementing the enemy's quaternion, and translating it to the enemy's position
       world = Matrix.CreateScale(scale) * Matrix.CreateFromQuaternion(quaternion) * Matrix.CreateTranslation(position);


       //here i move the enemy forward in the direciton that it is facing
       MoveForward(ref position, quaternion, velocity);


    }

    private void MoveForward(ref Vector3 position, Quaternion rotationQuat, float speed)
    {
        Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), rotationQuat);
        position += addVector * speed;
    }

私の質問は、現在の戦略/実装の何が問題なのか、これを達成するためのより簡単な方法はありますか (最初の部分がより重要です) の両方です。

4

1 に答える 1