我正在制作一个敌人 AI 跟随玩家 3d 对象的游戏。然而,我的敌人有时会在跟随玩家时痉挛并吓坏了。我认为我的问题可能在我的点积或交叉积数学中。这是我的敌人班级的代码:
//here I get the direction vector in between where I am pointing and where my enemy is located at
Vector3 midVector = Vector3.Normalize(Vector3.Lerp(Vector3.Normalize(world.Forward), Vector3.Normalize(Vector3.Subtract(p.position,this.position)), angleVelocity));
//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 my 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 I am facing and the midVector
float angle=(float)Math.Acos(Vector3.Dot(world.Forward,midVector));
//Console.WriteLine(angle);
Quaternion quaternion2 = Quaternion.CreateFromAxisAngle(perp, angle);
quaternion = quaternion * quaternion2;
//here I am simply scaling my world matrix, implementing my quaternion, and translating it to my position
world = Matrix.CreateScale(scale) * Matrix.CreateFromQuaternion(quaternion) * Matrix.CreateTranslation(position);
//here i move myself forward in the direciton that I am 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;
}
public override void update()
{
base.update();
}
}