0

我有这个敌人阶级,我希望它

1) 在某个地方生成 Vector3 pos

2) 旋转面对我的球员位置

3) 前进

就像现在的代码一样,它将出现在指定的位置:pos,并尝试旋转以面对它的目标:targetShip。由于这种偏移,它无法正确面对它的目标。

如果我删除为平移分配值的代码,或者如果我对 pos 进行规范化,那么模型将出现在原点并旋转以正确面对它的目标。

如果我尝试使用任何关于翻译的注释掉代码来移动它,它似乎从其他地方开始,我永远找不到它。

但是,如果我删除与旋转相关的代码,并取消注释与翻译相关的代码,那么我可以让它继续前进。

诀窍是一起做。

class Enemy : BasicModel
{


    Matrix rotation = Matrix.Identity;
    Matrix translation = Matrix.Identity;
    public Vector3 pos, up, right, targetShip,dir;

    public Enemy(Model m, Vector3 pos)
        : base(m)
    {
        up = Vector3.Up;
        //sets the position to the Vector3 as it's spawn point.
        translation = Matrix.CreateTranslation(pos);

    }

    public override void Update()
    {
        //Glo is a global class, where I store the player world.
        targetShip = Glo.world.Translation;
        targetShip.Normalize();

      pos = transform.Translation;

      rotation = RotateToFace(targetShip, pos, Vector3.Up);
      //Attempt at moving the model forward. Causes it to go out of view
      //translation *= Matrix.CreateTranslation(this.GetWorld().Backward);
      //translation *= Matrix.CreateTranslation(pos);
    }

    public override Matrix GetWorld()
    {
        return rotation * world * translation ;

    }

    /*Params: O = Our target
     * P = Our position
     * U = up.
     * 
     * Code from some site I googled up.
      */
    Matrix RotateToFace(Vector3 O, Vector3 P, Vector3 U)
    {

        //The direction we're facing.
        Vector3 D = (O - P);
        //Our relative Right.
        Vector3 Right = Vector3.Cross(U, D);
        Vector3.Normalize(ref Right, out Right);
        //Our back
        Vector3 Backwards = Vector3.Cross(Right, U);
        Vector3.Normalize(ref Backwards, out Backwards);
        //Our relative up
        Vector3 Up = Vector3.Cross(Backwards, Right);
        //Make a matrix out of all of these.
        Matrix rot = new Matrix(Right.X, Right.Y, Right.Z, 0, Up.X, Up.Y, Up.Z, 0, Backwards.X, Backwards.Y, Backwards.Z, 0, 0, 0, 0, 1);
        return rot;
    }



}

}

----


我试着做

anathonline 的方法,这就是我现在拥有的它不起作用。我在任何地方都看不到我的模型。

public override void Update()
        {
            targetShip = Glo.world.Translation;
            targetShip.Normalize();

            //Translate it to the origin
            translation = Matrix.Identity;
            //Orient it
            rot = RotateToFace(targetShip, translation.Translation, Vector3.Up);
            //Move it back to where it was originally;
            translation = Matrix.CreateTranslation(offset);
            //Calculate the front of the object
            Vector3 front = Vector3.Forward * translation.Translation;
            //normalize it
            Vector3.Normalize(ref front, out front);
            //multiply it
            front *= 2;
            //translate the object
            translation *= Matrix.CreateTranslation(front);



        }

        public override Matrix GetWorld()
        {
            return world * rot *  translation;

        }

----


第二次修改:还是不行

public override void Update()
    {
        targetShip = Glo.world.Translation;
        targetShip.Normalize();


         translation = Matrix.CreateLookAt(offset, targetShip, Vector3.Up);
        var amountToMove = 0.2f;
        var finalPos = Vector3.Lerp(targetShip, offset, amountToMove);
        rot = translation * Matrix.CreateTranslation(finalPos); 


    }

    public override Matrix GetWorld()
    {
        return world * rot * translation ;

    }
4

1 回答 1

0

我修好了,别着急。无论如何,所有这些代码都已过时。

于 2012-05-05T03:48:32.967 回答