我正在制作一个游戏,其中敌人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();
}
}